Hello, i am currently developing a program to read multiple sensor with different interface and the wrap it into a string and then print it to be used by other microcontroller system and log it to SD Card. I planned to use all 4 of the UART available in Arduino Mega. Uart Mapping is like:
Serial for communicating with PC,9600 bps
Serial1 for sending all sensor output to other system,9600bps
Serial2 for receiving GPS data,4800bps
Serial3 for receiving IMU 9DOF Razor, 4800bps
other sensor that i used is pressure sensor via I2C and Analog pressure sensor.
My arduino program is 80% now, and i still have difficulties on reading the IMU Data because of this:
if i use this program
void setup() {
Serial.begin(9600);
Serial3.begin(4800);
}
String IMURead ="";
void loop(){
if (Serial3.available() > 0 ) {
// read incoming character from IMU
int inChar = Serial3.read();
if(isDigit(inChar)); // convert byte ke char and add to string
{
IMURead +=(char)inChar;
}
if (inChar == '\n') // if you got newline
{
Serial.println (IMURead);
delay(50);
IMURead = "";
}
}
}
i could read the data from the IMU and it works fine, but the problem is, i can't put the IMU Read on the main loop, because i have my own section to print and store the data to SD Card, so i plan to made the IMU reading as separated function then call it on the main loop.
I have tried to modify the code above to be like this :
void setup() {
Serial.begin(9600);
Serial3.begin(4800);
}
int inChar;
String IMURead ="";
void loop(){
ReadIMU()
Serial.println (IMURead);
delay(50);
}
String ReadIMU() {
if (Serial3.available() > 0 ) {
inChar = Serial3.read();
do
{
// read incoming character from IMU
inChar = Serial3.read();
if(isDigit(inChar)); // convert byte ke char and add to string
{
IMURead +=(char)inChar;
}
}
while (inChar != '\n');
}
return IMURead;
IMURead="";
}
could somebody help my in this issue?
the bottom line, when the main loop calls the ReadIMU function, it will read serial3 data and stop when there is '\n' or ending character ';' being read from the serial3. Then it will return the value of IMURead to the main loop, so it could be printed and stored in SD card
Thanks in advance for the help