Problem with a simple program

Hi,

I have a small problem with my program. I wrote this program, trying to have 4 servo connected to Arduino. The problem I have is the servos are not responding to the program. I was wondering if there is a problem with my program. Any help would be appreciated.

#include “Servo.h”

int position=0; //position of piston pin from crank center. We initialize it to 0
int servo1PosChord1=60;
int servo2PosChord1=79;
int servo3PosChord1=180;
int servo4PosChord1=40;
int servo1PosChord2=60;
int servo2PosChord2=79;
int servo3PosChord2=180;
int servo4PosChord2=0;
int servo1PosChord3=60;
int servo2PosChord3=79;
int servo3PosChord3=169;
int servo4PosChord3=0;
int servo1PosChord4=90;
int servo2PosChord4=0;
int servo3PosChord4=85;
int servo4PosChord4=40;
int chord=0; //this is how we keep track which chord we are in
const int servoDelay=1000;
Servo myservo1; //servo 1
Servo myservo2; //servo 2
Servo myservo3; //servo 3
Servo myservo4; //servo 4
void setup(){
myservo1.attach(3); //attach each servo to the appropriate pins
myservo2.attach(6);
myservo3.attach(9);
myservo4.attach(10);
//use this to test the code
Serial.begin(9600);

//Use this to sound the chords once
for (chord=1; chord<=4; chord +=1){
Serial.print("Chord ");
Serial.println(chord);
switch(chord){
case 1:
Serial.print(“Case 1”);
Serial.println();
myservo1.write(servo1PosChord1);
myservo2.write(servo2PosChord1);
myservo3.write(servo3PosChord1);
myservo4.write(servo4PosChord1);
delay(servoDelay);
break;
case 2:
Serial.print(“Case 2”);
Serial.println();
myservo1.write(servo1PosChord2);
myservo2.write(servo2PosChord2);
myservo3.write(servo3PosChord2);
myservo4.write(servo4PosChord2);
delay(servoDelay);
break;
case 3:
Serial.print(“Case 3”);
Serial.println();
myservo1.write(servo1PosChord3);
myservo2.write(servo2PosChord3);
myservo3.write(servo3PosChord3);
myservo4.write(servo4PosChord4);
delay(servoDelay);
break;
case 4:
Serial.print(“Case 4”);
Serial.println();
myservo1.write(servo1PosChord4);
myservo2.write(servo2PosChord4);
myservo3.write(servo3PosChord4);
myservo4.write(servo4PosChord4);
delay(servoDelay);
break;
default:
break;
}
}

}
void loop(){ //use this if you want unlimited loop on the chord
for (chord=1; chord<=4; chord +=1){
Serial.print("Chord ");
Serial.println(chord);
switch(chord){
case 1:
Serial.print(“Case 1”);
Serial.println();
myservo1.write(servo1PosChord1);
myservo2.write(servo2PosChord1);
myservo3.write(servo3PosChord1);
myservo4.write(servo4PosChord1);
delay(servoDelay);
break;
case 2:
Serial.print(“Case 2”);
Serial.println();
myservo1.write(servo1PosChord2);
myservo2.write(servo2PosChord2);
myservo3.write(servo3PosChord2);
myservo4.write(servo4PosChord2);
delay(servoDelay);
break;
case 3:
Serial.print(“Case 3”);
Serial.println();
myservo1.write(servo1PosChord3);
myservo2.write(servo2PosChord3);
myservo3.write(servo3PosChord3);
myservo4.write(servo4PosChord3);
delay(servoDelay);
break;
case 4:
Serial.print(“Case 4”);
Serial.println();
myservo1.write(servo1PosChord4);
myservo2.write(servo2PosChord4);
myservo3.write(servo3PosChord4);
myservo4.write(servo4PosChord4);
delay(servoDelay);
break;
default:
break;
}
}
return;
}

Looks OK to me. Perhaps there is a problem with how the servos are connected. Have you been able to control a single servo with one of the example sketches like Files->Examples->Servo->Sweep? Are you getting your debug output on the Serial Monitor?

It could be written better but should work. How are the servos connected, and what is their power source?


Rob

The power source is just an A/C adapter that I use. I can get one servo to work when I use the same similar program. I just feel they servo is not doing what they are suppose to. How else can i power them?

That's probably OK as long as it's up to the job. I assume you have a common GND between the plug pack, servos and Arduino or the single servo wouldn't work either.

I can get one servo to work when I use the same similar program.

Can you get one to work with EXACTLY the same program? In other words unplug three of the servos.


Rob

Yes one servo worked perfectly fine, I had to power up with A/C though. When i was using the USB the servo was acting strange and I thought it might the power issue, so i connected with A/C adepter. I just cant figure it why four cant work with this program?

So, just to clarify, if you unplug 3 servos the remaining one works. If you replace that with one of the other servos THAT works, if you move any one servo to a different pin it works.

In other words all servos work on all pins but only one at a time.

Moving on do two work, three?

If things only start to get dicky when you get > 1 or 2 servos then it is probably a power supply issue.

using the USB the servo was acting strange

That probably is a PSU problem.


Rob