I have an Arduino speaking to a Pi over a USB serial connection. The Pi tells the Arduino what to do, and the Arduino will give the PI some data if requested (like sensor data) ending with the line "ok". This Let's the Pi know the command has been executed.
I was starting to use strings for more complicated command parameters, but when i try to read the incoming serial data from the Pi via the Arduino, I am instead getting the last thing the Arduino sent to the PI, and not what the Pi is sending. Because unrelated data is getting read, the Arduino thinks that there is a constant throughput of invalid commands which are echos of the strings that it is sending out.
Here is a bit of code:
String readString; //raw output from the serial connection
String command; //command to be executed
bool execute = 0; //triggers command
void setup() {
Serial.begin(9600);
}
void doneOk () {
Serial.print(ok);
}
void loop() {
while (Serial.available()) {
delay(10);
if (Serial.available() >0) {
char c = Serial.read(); //read each character
readString += c; //add each character to the string
if (c == '\r') {
//set command to read string
command = readString;
execute = 1; //enables the execution of a command
readString = ""; //reset readString to blank
}
//execute the command
if (execute == 1){
if (command == "f\r") {
moveForward();
doneOk();
.. blah blah more code
Here is the whole thing on gist if that helps: https://gist.github.com/jillytot/e21fcebcbe6b4ac66ad6
command
andreadString
are useful to know. – Nick Gammon♦ Sep 10 '15 at 20:47execute
become a value other than1
for example? However what @Majenko said may explain it. – Nick Gammon♦ Sep 10 '15 at 21:15