I am trying to control a robotic arm using the VarSpeedServo library. Im using a JY-MCU BT module with an UNO. I'm also using App Inventor to send the BT data. AI seems to be sending the data fine and motors work ok for a time. The problem Im having is sometimes when the 1st byte of data in the buffer is not that initial variable, its usually the last byte from previous packet. (Example; sometimes the Serial Monitor will show the dec value for the variable 'lastbyte' then jointservo, xval, etc down to speedval) So then I find myself in a cycle where the data the UNO is receiving according to the Serial monitor is 7,1,2,3,4,5,6 and the only way to fix is to restart the UNO. Can someone chime in what Im doing wrong?
void setup() {
pinMode(ledpin, OUTPUT);
PanServo.attach(10);
TiltServo.attach(11);
BaseServo.attach(9);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 6) {
if (Serial.available() == 7){
jointservo=Serial.read();
xval=Serial.read();
delimiter1=Serial.read();
yval=Serial.read();
delimiter2=Serial.read();
speedval=Serial.read();
lastbyte=Serial.read();
//Serial.flush();
Serial.println(jointservo);
Serial.println(xval);
Serial.println(delimiter1);
Serial.println(yval);
Serial.println(delimiter2);
Serial.println(speedval);
Serial.println(lastbyte);
Serial.println("\n");
if (jointservo == 'W' && lastbyte == 'E')
PanServo.slowmove(xval, speedval);
TiltServo.slowmove(yval, speedval);
}
else if (jointservo == 'B' && lastbyte == 'E') {
BaseServo.slowmove(yval, speedval);
digitalWrite(ledpin, HIGH);
}
else if (jointservo == 'C' && lastbyte == 'E'){
PanServo.slowmove(yval, speedval);
}
else{
Serial.flush();
}
}
Serial.flush();
}