Is there any good way of sending values from two joysticks from arduino A to arduino B? I tried sending using if statements, so if the Y- or X-axis' value changed then it would send that, but the servo motor on Arduino B went a little bit crazy.
Here's what I've done so far:
//Master Code
void loop()
{
yVal = analogRead(yAxis); // read the value of the Right Joystick (value between 0 and 1023)
yMap = map(yVal, 0, 1023, 0, 255);
xVal = analogRead(xAxis); // read the value of the left Joystick (value between 0 and 1023)
xMap = map(xVal, 0, 1023, 0, 110);
if(Serial.available() && !(xVal > 505 && xVal < 510)){
Serial.print('X');
delay(10);
Serial.write(xMap);
}
if(Serial.available() && !(yVal > 500 && yVal < 520)){
Serial.print('Y');
delay(10);
Serial.write(yMap);
}
delay(10);
}
And the Slave code:
//Slave code
void loop(){
while(Serial.available())
{
c = Serial.read();
delay(5);
val = Serial.read();
if(c == 'X'){
digitalWrite(13, LOW);
myServo.write(val);
}else if(c == 'Y'){
digitalWrite(13, HIGH);
}
}
}
Edit: I was thinking that maybe I should do something like a start and stop bit for X and Y?
Edit2: I wrote an if statement in Arduino B because it was reading the value -1, I don't know why, but after the if statement the servo motor hasn't been going crazy and the program works now. I still would like to know if I can do this in a better way. Thanks in advance!
