Hey,
I´m trying to control a robotic arm, mounted on a track vehicle with an Arduino Esplora.
The servo-arm and the vehicle are connected with an Arduino Mega wich recieves its data from the Esplora via bluetooth (using the HC05 bluetooth shield). For that I need to transmit 8 different variables.
Most of the time that works perfectly well, however when I'm trying to parse the data I will sometimes recieve wrong values (like every 5th or 6th time).
I'm pretty sure that the wiring is correct, so I think my code is wrong. It would be great if you guys could check it for mistakes
Here is the code (shortened) of the Master (Esplora):
#include <SoftwareSerial.h>
SoftwareSerial mySerial (11, 3); //RX, TX
int Xpos1 = 90; //Servo Starting position (90° at the beginning)
int Ypos2 = 90;
int Ypos3 = 90;
int Ypos4 = 90;
int Xpos5 = 90;
int Ypos6 = 90;
int X=0; // Variables for the vehicle
int Y=0;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
mySerial.print("<");
mySerial.print(Xpos1);
mySerial.print(",");
mySerial.print(Ypos2);
mySerial.print(",");
mySerial.print(Ypos3);
mySerial.print(",");
mySerial.print(Ypos4);
mySerial.print(",");
mySerial.print(Xpos5);
mySerial.print(",");
mySerial.print(Ypos6);
mySerial.print(",");
mySerial.print(X);
mySerial.print(",");
mySerial.print(Y);
mySerial.print(">");
}
and this is the code of the Slave (Mega):
int Xpos1 = 90;
int Ypos2 = 90;
int Ypos3 = 90;
int Ypos4 = 90;
int Xpos5 = 90;
int Ypos6 = 90;
int X=0
int Y=0
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
parseData();
showParsedData();
newData = false;
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial1.available() > 0 && newData == false) {
rc = Serial1.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // get the first part - the string
Xpos1 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
Ypos2 = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
Ypos3 = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
Ypos4 = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
Xpos5 = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
Ypos6 = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
X = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
Y = atoi(strtokIndx);
}
If you can spot any mistake it would be great if you could help me erasing it.
As you may notice I used Example 5 of the Arduino Serial "Tutorial" here in this forum, so I don't 100% know what I'm doing actually, sorry for that.
Thanks in advance for your efforts!