I'm having difficulty getting the multiple serial ports on my Arduino Mega to read in properly. I have the current setup:
RX0/TX0 - HC-05 (slave mode, sending the data from the other serial ports to my computer to be displayed in the serial port)
RX1/TX1 - HC-05 (master mode, receiving data from a separate MC to be eventually pushed to my computer)
RX2/TX2 - JY901 Gyroscope (gathering the angle of pitch to determine the angle my eventual device is being held at)
Not connected yet
RX3/TX3 - RS400 (Rangefinder to determine the distance from my device to point "B") - I have not connected this in yet as I want to integrate one element at a time.
~~ ~~
I am able to get both HC-05's to read in, however I'm not having success with reading in my angle from the JY901. When I connect the JY901 directly to RX0/TX0 with no other elements and run the basic code, it reads in fine. However, I'm making some error in the implementation phase and I'm not sure where it is.
Here is the code I'm currently using to read two serial devices and sending one serial device to my computer:
#include <JY901.h>
#include <Wire.h>
#include <EasyTransfer.h>
//#include <avr/power.h>
//#include <RFM12B_arssi.h>
//#include <SPI.h>
//#include <SPIFlash.h>
#define range 1
char temperatureChar[10];
char humidityChar [10];
char windChar [10];
struct Weather_Data_Structure
{
float temperature;
float humidity;
float wind;
};
struct Acknowledge
{
boolean received = false;
};
Weather_Data_Structure data;
Acknowledge acknowledge;
EasyTransfer ETin, ETout;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
ETin.begin(details(data), &Serial1);
ETout.begin(details(acknowledge), &Serial1);
}
void loop() {
Serial.print("Angle: ");
Serial.println((float)JY901.stcAngle.Angle[1]/32768*180); //Call the value stored in this function and print it out Serial
if(ETin.receiveData()){
//Serial.print ("data received");
String temperatureString = String(data.temperature,1);
temperatureString.toCharArray(temperatureChar,10);
Serial.print(temperatureChar); Serial.println(" C Degrees");
String humidString = String(data.humidity,1);
humidString.toCharArray(humidityChar,10);
Serial.print(humidityChar); Serial.println("% Humidity");
String windString = String(data.wind,1);
windString.toCharArray(windChar,10);
Serial.print(windChar); Serial.println(" m/s");
Serial.println(" ");
//ETout.sendData();
}
delay(10000);
acknowledge.received = 0;
}
void serialEvent()
{
while (Serial2.available())
{
JY901.CopeSerialData(Serial2.read()); //Call JY901 data cope function
}
}
Here's the code I use when only running the JY901 while collecting all the data it collects:
#include <Wire.h>
#include <JY901.h>
/*
Test on Uno R3.
JY901 UnoR3
TX <---> 0(Rx)
*/
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
//print received data. Data was received in serialEvent;
Serial.print("Time:20");Serial.print(JY901.stcTime.ucYear);Serial.print("-");Serial.print(JY901.stcTime.ucMonth);Serial.print("-");Serial.print(JY901.stcTime.ucDay);
Serial.print(" ");Serial.print(JY901.stcTime.ucHour);Serial.print(":");Serial.print(JY901.stcTime.ucMinute);Serial.print(":");Serial.println((float)JY901.stcTime.ucSecond+(float)JY901.stcTime.usMiliSecond/1000);
Serial.print("Acc:");Serial.print((float)JY901.stcAcc.a[0]/32768*16);Serial.print(" ");Serial.print((float)JY901.stcAcc.a[1]/32768*16);Serial.print(" ");Serial.println((float)JY901.stcAcc.a[2]/32768*16);
Serial.print("Gyro:");Serial.print((float)JY901.stcGyro.w[0]/32768*2000);Serial.print(" ");Serial.print((float)JY901.stcGyro.w[1]/32768*2000);Serial.print(" ");Serial.println((float)JY901.stcGyro.w[2]/32768*2000);
Serial.print("Angle:");Serial.print((float)JY901.stcAngle.Angle[0]/32768*180);Serial.print(" ");Serial.print((float)JY901.stcAngle.Angle[1]/32768*180);Serial.print(" ");Serial.println((float)JY901.stcAngle.Angle[2]/32768*180);
Serial.print("Mag:");Serial.print(JY901.stcMag.h[0]);Serial.print(" ");Serial.print(JY901.stcMag.h[1]);Serial.print(" ");Serial.println(JY901.stcMag.h[2]);
Serial.print("Pressure:");Serial.print(JY901.stcPress.lPressure);Serial.print(" ");Serial.println((float)JY901.stcPress.lAltitude/100);
Serial.print("DStatus:");Serial.print(JY901.stcDStatus.sDStatus[0]);Serial.print(" ");Serial.print(JY901.stcDStatus.sDStatus[1]);Serial.print(" ");Serial.print(JY901.stcDStatus.sDStatus[2]);Serial.print(" ");Serial.println(JY901.stcDStatus.sDStatus[3]);
Serial.print("Longitude:");Serial.print(JY901.stcLonLat.lLon/10000000);Serial.print("Deg");Serial.print((double)(JY901.stcLonLat.lLon % 10000000)/1e5);Serial.print("m Lattitude:");
Serial.print(JY901.stcLonLat.lLat/10000000);Serial.print("Deg");Serial.print((double)(JY901.stcLonLat.lLat % 10000000)/1e5);Serial.println("m");
Serial.print("GPSHeight:");Serial.print((float)JY901.stcGPSV.sGPSHeight/10);Serial.print("m GPSYaw:");Serial.print((float)JY901.stcGPSV.sGPSYaw/10);Serial.print("Deg GPSV:");Serial.print((float)JY901.stcGPSV.lGPSVelocity/1000);Serial.println("km/h");
Serial.println("");
delay(500);
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent()
{
while (Serial1.available())
{
JY901.CopeSerialData(Serial1.read()); //Call JY901 data cope function
}
}
I only need to access the second value stored for the Angle in the above code, so I created this reduced sketch and it runs and outputs the needed value perfectly fine when it is the only device connected (i.e. RX0/TX0 and the Mega is connected via USB to my computer):
#include <Wire.h>
#include <JY901.h>
/*
Test on Uno R3.
JY901 UnoR3
TX <---> 0(Rx)
*/
void setup()
{
Serial.begin(9600);
}
void loop()
{
//print received data. Data was received in serialEvent;
Serial.print("Angle:");
//Serial.print((float)JY901.stcAngle.Angle[0]/32768*180);
//Serial.print(" ");
Serial.print((float)JY901.stcAngle.Angle[1]/32768*180);
//Serial.print(" ");
//Serial.println((float)JY901.stcAngle.Angle[2]/32768*180);
Serial.println("");
delay(2000);
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent()
{
while (Serial.available())
{
JY901.CopeSerialData(Serial.read()); //Call JY901 data cope function
}
}
I know my mistake is happening in the implementation of the JY901 as I'm able to get my serial monitor to print:
Angle 0.00
22.4 C Degrees
46.7% Humidity
0.0 m/s
I'm just not understanding why the value stored for the angle is not being properly called from the respective serial bus as defined in the void function "serialEvent" in the first code posted above.
Thanks for any and all help.