Pages: [1]   Go Down
Author Topic: Arduino + Servo  (Read 335 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...

Code:
#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,
Code:
#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 [glow]int pos [4] = 0;[/glow] line

Can someone tell me how can i fix this?

Thanks AWOL for all your help smiley
and thanks everybody
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49272
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int pos[4] = {0, 0, 0, 0};
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34128
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Guzz, please can you remove your duplicate posts.
« Last Edit: November 11, 2009, 02:22:00 pm by Grumpy_Mike » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26278
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oops!
Code:
int pos[4] = {0};

Two things here:
The initialisation isn't necessary because:
a) it's done at the start of the for loops anyway.
b) the values are reset to zero when the program is started, but only because of their global scope.

Quote
how could i make them move at the same time?

Turn the example inside out:
Code:
 
for(int pos = 0; pos < 180; pos += 1) {
    for (int i = 0; i < 4; ++i) {
         myservo[i].write(pos);
         delay(5);
    }
}
  
« Last Edit: November 11, 2009, 03:33:02 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: