Move 4 servos, all of them on one shield, controlled by knobs in another?

So I’m doing this project for high school and we need to control 4 continous rotation servos on one Uno shield with 4 knobs in other Uno shield (one knob for each servo). Both shields communicate via two xbees, because we need to control this from long distance. This is the code I’ve got so far:

Shield with the knobs:

int joystickIU = 0; //joystick izquierdo arriba-abajo //potentiometer value
int joystickIR = 0; //joystick izquierdo derecha-izquierda
int joystickDU = 0; //joystick derecho arriba-abajo
int joystickDR = 0; //joystick derecho derecha-izquierda
const int joystickIUpin = A0; //pins for the potentiometers
const int joystickIRpin = A1;
const int joystickDUpin = A2;
const int joystickDRpin = A3;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  joystickIU = analogRead(joystickIUpin); //read the potentiometer's value
  joystickIR = analogRead(joystickIRpin);
  joystickDU = analogRead(joystickDUpin);
  joystickDR = analogRead(joystickDRpin);
  Serial.write(byte (joystickIU)); //send potentiometer's value to the other shield
  Serial.write(byte (joystickIR));
  Serial.write(byte (joystickDU));
  Serial.write(byte (joystickDR));
}

Shield with the servos:

#include <Servo.h>

Servo servoA, servoB, servoC, servoD;// servoA es el servo derecho adelante, B servo derecho atrás, C izquierda delante y D izquierda atrás
byte joystickIU = 0; //joystick izquierdo arriba-abajo // value sent by the other shield
byte joystickIR = 0; //joystick izquierdo derecha-izquierda
byte joystickDU = 0; //joystick derecho arriba-abajo
byte joystickDR = 0; //joystick derecho derecha-izquierda
int valA; // to convert value to a number that the servo can read
int valB;
int valC;
int valD;

void setup()
{
  Serial.begin(115200);
  servoA.attach(6); //pins for the servos
  servoB.attach(7);
  servoC.attach(8);
  servoD.attach(9);
}

void loop()
{
  if (Serial.available()>0)
  {
    joystickIU=Serial.read(); //takes the value sent from the other shield
    joystickIR=Serial.read();
    joystickDU=Serial.read();
    joystickDR=Serial.read();
    valA = analogRead(joystickIU); //make it an integer
    valB = analogRead(joystickIR);
    valC = analogRead(joystickDU);
    valD = analogRead(joystickDR);
    valA = map(valA, 0, 1023, 0, 179); //make it readable for the servo
    valB = map(valB, 0, 1023, 0, 179);
    valC = map(valC, 0, 1023, 0, 179);
    valD = map(valD, 0, 1023, 0, 179);
    servoA.write(valA); //move accordingly
    servoB.write(valB);
    servoC.write(valC);
    servoD.write(valD);
    delay(15);
  }
}

So basically I’d like to know if this is correct so I can go tomorrow to class and directly test it or if i need to work it now. I suppose you should also tell me if my //coments are correct.

Thank you!

PD: I’m sorry if this is posted in a wrong forum or if there is anything incorrect about my English.

And here is the modified receive loop:

byte count;
void loop()
{
  if (Serial.available()>0)
  {
    byte value=Serial.read();
    if (value==200) // 'sync' char received
    {
      count=0; // 'sync' char received, reset counter
    }
    else  // normal servo value received
    {
      count++;
      if (count==1) servoA.write(value); //value for servoA, move accordingly
      else if (count==2) servoB.write(value);
      else if (count==3) servoC.write(value);
      else if (count==4) servoD.write(value);
    }
  }
}

This code should receive in each cycle - one 'sync' char - four servo values

I think I just messed it up, by "editing" my original posting instead of adding the second code as a new reply. :drooling_face:

Code above is a suggestion for a new loop code for the receiver.

So next try to write some words about the 'sender':

Serial.write(byte (joystickIU)); //send potentiometer's value to the other shield
 Serial.write(byte (joystickIR));
 Serial.write(byte (joystickDU));
 Serial.write(byte (joystickDR));

The first problem is, that potentiometer readings using analogRead() are of the data type "int", which is 16 bits. But you cast that to "byte" (8 bits) when sending, so that only the lower byte of the int is actually sent.

The second problem is, that you are constantly sending values, with no separating char and no non-transmission period. So when the receiver starts reading values, he will never know which value belongs to which potentiometer. Nothing is synchronized with sending/receiving. The chance for the receiver of being in sync with the sender is 1:4.

So first thing I would change is the sending protocol. As you just want to send servo positions in the range of 0...180 degrees, I would suggest that: - calculate and send servo positions as 'byte' - send a 'synchronizing' char before sending the first value (the 'synchronizing' char must have a value not in the range of valid servo positions)

Let's say you send servo positions 0..180 and use 200 for the 'sync' char.

So sending 'loop()' logic could be like that:

void loop()
{
  joystickIU = analogRead(joystickIUpin); //read the potentiometer's value
  joystickIR = analogRead(joystickIRpin);
  joystickDU = analogRead(joystickDUpin);
  joystickDR = analogRead(joystickDRpin);
  byte valA = map(joystickIU, 0, 1023, 0, 179); //make it readable for the servo
  byte valB = map(joystickIR, 0, 1023, 0, 179);
  byte valC = map(joystickDU, 0, 1023, 0, 179);
  byte valD = map(joystickDR, 0, 1023, 0, 179);
  Serial.write(200); // send 'sync' char, the value must be out of range for normal values
  Serial.write(valA); //send potentiometer's value to the other shield
  Serial.write(valB);
  Serial.write(valC);
  Serial.write(valD);
}