I have arduino code for operating 2 servos, but we are using 4 servos and am having trouble getting the other 2 to talk.
The program so far as I can make out is that the angles for the servos that are calculated by the processing side are being sent out one after the other (shoulder, elbow, wrist, wrist2) then repeated. The arduino program gets this data and stores in into an array and then is written to the pin of the appropriate array segment. So 0 is shoulder, 1 is elbow, 2 is wrist and 3 is wirst2.
I can easily get 2 servos to run with no problem. But when I try and add 1 or 2 more we get no response. Can anyone please help me to get the other 2 servos to work? My knowledge on this code is rather limited, so any help is appreciated.
processing Data being sent to the arduino:
byte out[] = new byte[4];
out[0] = byte(shoulderAngle);
out[1] = byte(elbowAngle);
out[2] = byte(wristAngle);
out[3] = byte(wrist2Angle);
port.write(out);
Arduino Code:
#include <Servo.h>
//Declares the servos.
Servo shoulder;
Servo elbow;
Servo wrist;
Servo wrist2;
//Setup servo positions.
int nextServo = 0;
int servoAngles[] = {0, 0};
//Define pins for each servo.
void setup()
{
shoulder.attach(50);
elbow.attach(51);
wrist.attach(52);
wrist2.attach(53);
Serial.begin(9600);
}
void loop()
{
if(Serial.available())
{
int servoAngle = Serial.read();
servoAngles[nextServo] = servoAngle;
nextServo++;
if(nextServo > 3)
{
nextServo = 0;
}
shoulder.write(servoAngles[0]);
elbow.write(servoAngles[1]);
wrist.write(servoAngles[2]);
wrist2.write(servoAngles[3]);
}
}
Sorry for the lengthy post but have been stuck for a while.
int servoAngles[] = {0, 0};
toint servoAngles[4];
or toint servoAngles[] = {0,0,0,0};
Also, run a test with the original code but 51 changed to 52 or 53 to check hardware $\endgroup$