Controlling two+ servos over a serial connection, problem with arrays?

Hi,
I have got 2 arduinos, a Nano and a Pro Mini, and I am trying to control 2 servos on one arduino from the other. I have one pot. and one basic switch, which I have set to generate a random int between 0 and 1023 when on. These two values are both stored in an array as Tx[0] and Tx[1], I map the values between 0 and 180, then send them over serial with: Serial.write(Tx, 2);
The other arduino then attaches it's servos and reads the serial, and I am trying to get it to store the value in an array called Rx. It then writes to 2 values to the servos.

I have successfully done this with 1 servo and just one integer, but I need to control at least 5 for my project. The problem I have is that I cannot compile my Rx code, I get this error:

error: incompatible types in assignment of 'int' to 'byte [2]'

I think this is a problem with my array and I have little/no experience with C/C++, So i have no idea on how to solve it.

My Tx code is as following:

// MultiServoWiredSerialTx.ino

int Tx[2];
char buf[255];

void setup() {

	pinMode(7, OUTPUT);
	digitalWrite(7, HIGH);

	Serial.begin(38400);

}

void loop() {

	Tx[0] = analogRead(0);
	if(digitalRead(7) == HIGH){
		Tx[1] = random(10, 1023);
	} else {
		Tx[1] = 0;
	}
	Tx[0] = map(Tx[0], 0, 1023, 0, 179);
	Tx[1] = map(Tx[1], 0, 1023, 0, 179);

	Serial.write(Tx, 2);

}

Rx Code:

// MultiServoWiredSerialRx.ino

#include "Servo.h"

byte Rx[2];

Servo servo1;
Servo servo2;

void setup() {

	Serial.begin(38400);

	servo1.attach(9);
	servo2.attach(10);
}

void loop() {

	if (Serial.available() > 0) {
		Rx = (Serial.read());
	}
	servo1.write(Rx[0]);
	servo2.write(Rx[1]);

}

Thanks.

Look into sprintf and either strtok or parsing data.

I map the values between 0 and 180

Then, you send 2 of the 4 bytes in the array.

On the receiver, you read one byte, and try to store that one byte in an array, without saying where in the array. That code won't even compile. After that, you expect that somehow there will be useful data in both elements of the array.

Try again, on both ends. The sender needs to declare Tx to be a byte array.

Thanks for the replies.

PaulS, I changed the Tx array to a byte and then did this to my Rx code, hopefully so that if there was a 2 byte packet available, it would add it the each arrays's address:

// MultiServoWiredSerialRx.ino

#include "Servo.h"

byte Rx[2];

Servo servo1;
Servo servo2;

void setup() {

	Serial.begin(38400);

	pinMode(13, OUTPUT);
	digitalWrite(13, HIGH); //testing LED
	delay(1500);
	digitalWrite(13, LOW);

	servo1.attach(9);
	servo2.attach(10);

	servo1.write(90);
	servo2.write(90);

	delay(3000); //wait for servos to stabilise and comms to connect
}

void loop() {
	while (Serial.available()>1) {
		digitalWrite(13, HIGH); //too see if serial is available
		delay(1000);
		digitalWrite(13, LOW);
		for(int n=0; n==2; n++) {
			Rx[n] = Serial.read();
			digitalWrite(13, HIGH); //too see if serial is being interpreted and used
			delay(500);
			digitalWrite(13, LOW);
		}
	}

	//if (Serial.available() > 0) {
	//	Rx = (Serial.read());
	//}
	//servo1.write(Rx[0]);
	//servo2.write(Rx[1]);
}

When, however, I added the LED, pin 13, to test, the LED just stays on constantly, and servo1 buzzes. This happens only after the Tx --> Rx line is connected, if it is disconnected, however, the LED stays on, so I suspect my program is locking up.

		for(int n=0; n==2; n++) {

Back to the books!

The middle clause of a for loop is the while condition. The loop executes as long as the middle condition is true. Since you initialized n to 0, n is not 2, so the loop never iterates.

I may just be being a bit stupid!, but I have changed my code:

for(int n=0; n<2; n++) {

and the exact same thing is happening as before.

Thanks.

and servo1 buzzes.

Since the receiver code never actually moves the servo after setup() ends, the servo buzzing happens regardless of serial data arriving, or not, and regardless of what the serial data is.

So, how are the servos powered? What kind of servos are they?

Why are you using the hardware serial pins for communication between the Arduinos? You should be using other pins, and SoftwareSerial, so the hardware serial pins can be used for debugging.

The servos are standard 4.8-6v Micro Plane servos, 9g. I have just realised, however, that usb, which is what I am powering them from, as well as the arduinos, may have insufficient current to power 2 servos, with one it worked perfectly. The servos still move at the start of setup() though, and upon removing the signal cable to either/both of the servos the buzzing stops. I am also concerned about the pin13 LED lighting and staying lit even though it is never told to do so.
I don't really know how to use the hardware serial pins for debugging, but if it would help I am sure I could, and I could very easily incorparate software serial.

Thanks.

The cheap 9g servos will strip their gears if the servo horn is twisted or the servo is driven against its hard stop, so be careful. Not sure why you are focused on arrays for your servo control. Not sure just what you are trying to do. Are you using a pot on one arduino to generate a servo control position, and then sending that control position to another arduino to move servos connected to that arduino? If so, then there is probably easier code to do the job. But first you need to properly power the servos.

This is my end goal: An rc plane using xbees, with one arduino in the plane and one arduino in a portable transmitter, I need 5 channels for ESC, servos. I'm starting small, however, without the xbees, just using a wired serial connection and have so far managed to control on servo precisely, on the Rx arduino, from a pot on the Tx arduino.
I read somewhere a while ago of someone using arrays for his setup, which is kind of the same as mine, but a bit more confusing. I tried to understand his code, but I couldn't as many of the things were very complicated.
What code, if you know, do you think would be easier to do? I am fairly new to arduino and this is my first project with it so I am fairly inexperienced with the language.
My servos will be powered by a 2A BEC in my 30A ESC which will have a 2 cell LiPo battery connected, which has a nominal 7.4voltage. The arduino will be connected directly to the LiPo. It is just much harder to do when at a computer and testing the code, then slightly modifying it, then reuploading it, as my workbench is at the other end of the house.

Thanks.

Below is some servo test code that is setup for several servos. The servo command position is followed by the servo designator (a, b, c, and d) for the desired servo, followed by a comma( ,) delimiter indicating the end of the command data packet. You could try this code with the rx arduino using the serial monitor. If this works, then making the code for the tx arduino should be fairly straight forward.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}