Protocol issues….
- I have a Rover 5 managed by an Arduino Uno and a remote with a Arduino Pro as a brain. They communicate via radio APC220.
- The remote has a joystick, and a few pushbuttons. Till now communication was one way. From the remote to the rover: joystick to control the rover and the push buttons to switch from operation mode and to switch features on and off (e.g. headlights). No probs till here.
- I want to expand my project and added a lcd and a SD-card to the remote. I want to display Rover–status-info on the display (e.g. operating mode (autonomous, remote driving, remote gripper, battery voltage…) and I want to log Rover-data to the SD-card (e.g. heading and distance).
- This requires bidirectional communication. I've experimented a bit but the results are disappointing. I let the Uno send three strings to the Pro, with a an second interval, preceded by a start byte ‘# ‘and terminated with a stop byte ‘@’.
if(millis() - Timer >= 1000)
Timer = millis();
if (a == 0) {Serial.println("#string1@");}
else if (a == 1) {Serial.println("#string2@");}
else {Serial.println("#string3");}
a = a + 1;
if (a == 3) {a=0;}
}
- I want the Pro to receive the strings and display them to the lcd. I am using the code below.
#define STRING_MAX_LENGTH 10
byte inByte;
char inBytes[STRING_MAX_LENGTH];
int i = 0;
boolean retrieveData = false;
byte startByte = '#';
byte stopByte = '@';
void loop() {
while(Serial.available() > 0) {
inByte = Serial.read();
if(inByte == stopByte){
inBytes[i] = '\0';
retrieveData = false;
executeCommand(inBytes);
}
else {
if(retrieveData){
inBytes[i] = inByte;
++i;
if(i >= STRING_MAX_LENGTH - 1) {i = STRING_MAX_LENGTH - 1;}
}
}
if(inByte == startByte){
for(i = 0; i < STRING_MAX_LENGTH; i++)
inBytes[i] = '\0';
i = 0;
retrieveData = true;
}
} // End while (Serial.available)
if(millis() - time1 > = 50){ // execute each 50 ms
time1 = millis();
GetJoyStickStatus ();
GetPushButtonStatus();
}
}
void executeCommand(char * commandString)
{
lcd.clear();
lcd.print(commandString);
lcd.setCursor(0,1);
}
- If I only let the Pro receives data from the Uno everything goes well (yellow code lines left out). If I give the Pro other assignments such as sending data to the other side (e.g. joystick data, yellow code lines included) the receipt at the Pro side becomes unreliable and erratic.
- I've been searching for clues but am still not sure how to proceed from here? Should I develop a a protocol (checksums, handshakes, ...)? Are these problems related to timing issues? Or something entirely different? I expected to find lots of info and examples on this topic but the was not the (my) case. Therefore I like your help. What is the best way to proceed?
Thanks in advance.
Best regards, Ko