I'm having trouble getting two Arduino Unos to correctly respond to each other. The code is for displaying flight information of a quadcopter running AeroQuad software v3.0.1.
Arduino Uno 1 - receive call for data, send data. Ignore the softwareSerial declarations, I was using those for a bit and just didn't remove them. This works over Serial Monitor - if I type 'a', it responds with data in the format of 'Z(voltage)Z(state)' where voltage is a 4 digit number (mV) and state is either 0 or 1, corresponding to acrobatic and stable modes.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // rx, tx
int i = 980;
char buffer = 0;
void setup() {
Serial.begin(115200);
mySerial.begin(115200); // opens serial port, sets data rate
}
void loop() {
if (Serial.available() > 0) {
buffer = Serial.read();
if (buffer == 'a') {
//for (int i = 980; i < 1300; ++i) {
Serial.print('Z'); // send checkbit
if (i < 1000) {
Serial.print('0'); }
Serial.print(i); // send valid battery level
Serial.print('Z'); // send checkbit again
if (i < 1150) {
Serial.println('0'); // send rate mode
} else {
Serial.println(1); // send stable mode
}
++i;
delay(50);
if (i > 1300) {
i = 980; }
//}
}
}
}
Arduino Uno 2 - connected to a GLCD to display data. I'm guessing there's a better way to encode/decode the data but I was getting tons of errors on my GLCD without multiple checks on each code, no errors with the checks. This code works when the first Uno is simply spitting out data, not waiting for a request for it. If I remove the Serial.available() and Serial.read() from the code on the first Uno, it works, constantly sending data to the GLCD Uno and the GLCD correctly displays it. It stops working when set up for call and response. I can confirm in Serial Monitor that this code is sending an 'a' approximately every second.
void loop() {
currentTime = millis();
counter++;
// check incoming bits for correctly formatted data, fill appropriate buffers
if (Serial.available() > 0) {
for (int i = 0; i < 6; ++i) {
if (i == 1) { // first, fill check buffer and confirm correct format
checkBuff[0] = Serial.read();
delay(2); // if first bit is 'Z', continue on, else, don't update buffers
} if (checkBuff[0] == 'Z') {
if ((i > 0) && (i < 5)) { // fill battery buffer (bits 1-4)
battBuff[i - 1] = Serial.read();
delay(2);
} else if (i == 5) { // fill second check buffer
checkBuff[1] = Serial.read();
delay(2);
} else if (checkBuff[1] == 'Z') {
flightMode = Serial.read();
delay(2);
Serial.flush(); // flush serial buffer to make room for more data
checkBuff[0] = '0'; // reset check buffer
checkBuff[1] = '0';
}
}
}
}
// display information
if (counter > 20) {
GLCD.ClearScreen();
counter = 0; }
displayBattery(battBuff, 0, 0);
displayMode(flightMode, 0, 1);
if ((currentTime - previousTime) > 1000) {
Serial.print('a');
previousTime = currentTime;
delay(20);
}
}
I didn't bother including the setup() and display functions as they aren't relevant to the problem. So, both Unos work independently through Serial Monitor, but when connected to each other, nothing happens. If Uno 1 just spits out data constantly, the GLCD will display it fine. Nothing changes on the GLCD with the call and response code.
Any help would be appreciated, I'm guessing the two Arduinos aren't syncing up correctly but I don't know how to fix it...