Hi, my name is Guzz and i'm new on the arduino world. I'm having some trouble to write a code to control 4 servos with a arduino, and i dont know what can be wrong with my code, i'm not a programmer (yet). I wrote this code based on de sweep code from the arduino examples. ''The code compiles fine, but when i try to upload to the board it crashes. Im using the arduino duemilanove with atmega 328." - solved - The code is like this...
#include <Servo.h>
Servo myservo0; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos0 = 0; // variable to store the servo position
Servo myservo1; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos1 = 0; // variable to store the servo position
Servo myservo2; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos2 = 0; // variable to store the servo position
Servo myservo3; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos3 = 0; // variable to store the servo position
void setup()
{
myservo0.attach(9); // attaches the servo on pin 9 to the servo object
myservo1.attach(8); // attaches the servo on pin 8 to the servo object
myservo2.attach(7); // attaches the servo on pin 7 to the servo object
myservo3.attach(5); // attaches the servo on pin 5 to the servo object
}
void loop()
{
for(pos0 = 0; pos0 < 180; pos0 += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo0.write(pos0); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos0 = 180; pos0>=1; pos0-=1) // goes from 180 degrees to 0 degrees
{
myservo0.write(pos0); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos1 = 0; pos1 < 180; pos1 += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo1.write(pos1); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos1 = 180; pos1>=1; pos1-=1) // goes from 180 degrees to 0 degrees
{
myservo1.write(pos1); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos2 = 0; pos2 < 180; pos2 += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos2 = 180; pos2>=1; pos2-=1) // goes from 180 degrees to 0 degrees
{
myservo2.write(pos0); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos3 = 0; pos3 < 180; pos3 += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo3.write(pos3); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos3 = 180; pos3>=1; pos3-=1) // goes from 180 degrees to 0 degrees
{
myservo3.write(pos3); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
}
This code is working fine, but is not doing what i wanted. Each one of the servos moves one at a time, how could i make them move at the same time?
AWOL gave me some help and rewrote this sketch for me to simplify it,
#include <Servo.h>
Servo myservo [4]; // create servo object to control four servos
int pos [4] = 0; // variable to store the servo position
void setup()
{
myservo[0].attach(9); // attaches the servo on pin 9 to a servo object
myservo[1].attach(8); // attaches the servo on pin 8 to a servo object
myservo[2].attach(7); // attaches the servo on pin 7 to a servo object
myservo[3].attach(5); // attaches the servo on pin 5 to a servo object
}
void loop()
{
for (int i = 0; i < 4; ++i) {
for(pos[i] = 0; pos[i] < 180; pos[i] += 1)
{
myservo[i].write(pos[i]);
delay(5); // waits 5ms for the servo to reach the position
}
for(pos[i] = 180; pos[i]>=1; pos[i]-=1) // goes from 180 degrees to 0 degrees
{
myservo[i].write(pos[i]); // tell servo to go to position in variable 'pos'
delay(5); // waits 5ms for the servo to reach the position
}
}
}
but when i try to compile it i get the message
error: array must be initialized with a brace-enclosed initializer
in the int pos [4] = 0; line
Can someone tell me how can i fix this?
Thanks AWOL for all your help
and thanks everybody