Sweep Function with 5 Servos

First off, I am relatively new to programming using an Arduino, and I am starting with this project for a science fair project.

As for the coding, I have included my the code both in this message and in the attachment link below. I used the 'Servo Sweep' example sketch to base this code off of. Again, I am new to programming, both in general and with an Arduino, so don't be surprised if there are simple mistakes (there will probably be a lot of them haha)

Anyways, thank you for your help! code is below.

#include <Servo.h>

Servo myservoa, myservob, myservoc, myservod, myservoe

int pos = 0; // variable to store the servo position, error message "expected intializer before 'int' "

void setup() {
myservoa.attach(5);
myservob.attach(6);
myservoc.attach(7);
myservod.attach(8);
myservoe.attach(9);
}

void loop()
{
{
for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservoa.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
myservoa.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservob.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
myservob.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservoc.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
myservoc.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservod.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
myservod.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservoe.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
myservoe.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
}

_5_Servo_Sweep.ino (3.02 KB)

Was there a problem or question with this?

Please use code tags when posting code. I'm sure there are no smiley faces with sunglasses in your code.

Read this: How To Use This Forum and pay particular attention to point 7.

gaellington:
First off, I am relatively new to programming using an Arduino, and I am starting with this project for a science fair project.

As for the coding, I have included my the code both in this message and in the attachment link below. I used the 'Servo Sweep' example sketch to base this code off of. Again, I am new to programming, both in general and with an Arduino, so don't be surprised if there are simple mistakes (there will probably be a lot of them haha)

Anyways, thank you for your help! code is below.

#include <Servo.h>

Servo myservoa, myservob, myservoc, myservod, myservoe

int pos = 0; // variable to store the servo position, error message "expected intializer before 'int' "

void setup() {
myservoa.attach(5);
myservob.attach(6);
myservoc.attach(7);
myservod.attach(8);
myservoe.attach(9);
}

void loop()
{
{
for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservoa.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
myservoa.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservob.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
myservob.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservoc.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
myservoc.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservod.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
myservod.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservoe.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
{
for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
myservoe.write(pos); // tell servo to go to position in variable 'pos'
delay(500); // waits 500ms for the servo to reach the position
}
}

RE: Error message "expected initializer before 'int' "
Are you missing the ";" on the previous line ?

Use the "</>" button to format your code.

mrsummitville:
Use the "</>" button to format your code.

Actually, it should be formatted before being placed between code tags. >Tools >"Auto Format" makes this easy.

So it looks like this:

#include <Servo.h>

Servo myservoa, myservob, myservoc, myservod, myservoe;  // << this line has syntax error, missing ; 

int pos = 0;    // variable to store the servo position, error message "expected intializer before 'int' "

void setup() {
  myservoa.attach(5);  
  myservob.attach(6);
  myservoc.attach(7);
  myservod.attach(8);
  myservoe.attach(9);
}

void loop() 
{
  {
    for (pos = 0; pos <= 180; pos += 1)  // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      myservoa.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position
  }
  {
    for (pos = 180; pos >= 0; pos -= 1)  // goes from 180 degrees to 0 degrees
      myservoa.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position
  }
  {
    for (pos = 0; pos <= 180; pos += 1)  // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      myservob.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position
  }
  {
    for (pos = 180; pos >= 0; pos -= 1)  // goes from 180 degrees to 0 degrees
      myservob.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position
  }
  {
    for (pos = 0; pos <= 180; pos += 1)  // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      myservoc.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position
  }
  {
    for (pos = 180; pos >= 0; pos -= 1)  // goes from 180 degrees to 0 degrees
      myservoc.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position
  }
  {
    for (pos = 0; pos <= 180; pos += 1)  // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      myservod.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position
  }
  {
    for (pos = 180; pos >= 0; pos -= 1)  // goes from 180 degrees to 0 degrees
      myservod.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position
  }
  {
    for (pos = 0; pos <= 180; pos += 1)  // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      myservoe.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position
  }
  {
    for (pos = 180; pos >= 0; pos -= 1)  // goes from 180 degrees to 0 degrees
      myservoe.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position
  }
}

The Ctrl-T (Tools:autoformat) makes the code a lot easier to read.

After removing the superfluous brackets, (and no doubt with them as well), the code compiles fine.
It now looks like this:-

#include <Servo.h>

Servo myservoa, myservob, myservoc, myservod, myservoe;  // << this line has syntax error, missing ;

int pos = 0;    // variable to store the servo position, error message "expected intializer before 'int' "

void setup()
{
    myservoa.attach(5);
    myservob.attach(6);
    myservoc.attach(7);
    myservod.attach(8);
    myservoe.attach(9);
}

void loop()
{
    for (pos = 0; pos <= 180; pos += 1)  // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
        myservoa.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position

    for (pos = 180; pos >= 0; pos -= 1)  // goes from 180 degrees to 0 degrees
        myservoa.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position

    for (pos = 0; pos <= 180; pos += 1)  // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
        myservob.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position

    for (pos = 180; pos >= 0; pos -= 1)  // goes from 180 degrees to 0 degrees
        myservob.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position

    for (pos = 0; pos <= 180; pos += 1)  // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
        myservoc.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position

    for (pos = 180; pos >= 0; pos -= 1)  // goes from 180 degrees to 0 degrees
        myservoc.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position

    for (pos = 0; pos <= 180; pos += 1)  // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
        myservod.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position

    for (pos = 180; pos >= 0; pos -= 1)  // goes from 180 degrees to 0 degrees
        myservod.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position

    for (pos = 0; pos <= 180; pos += 1)  // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
        myservoe.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position

    for (pos = 180; pos >= 0; pos -= 1)  // goes from 180 degrees to 0 degrees
        myservoe.write(pos);              // tell servo to go to position in variable 'pos'
    delay(500);                       // waits 500ms for the servo to reach the position
}

@gaellington, was there an actual question? I don't see one in the original post.

This series of article shows exactly this.

Multi-tasking the Arduino

Hutkikz:
This series of article shows exactly this.

Multi-tasking the Arduino

That type of method is far better than all of those horrible delays. 8)

Edit: This is well worth reading too:-
Demonstration code for several things at the same time

Anyways, thank you for your help! code is below.

You probably could modify each of your servos positioning code sections to use the millis format from the "blink without delay" example bottom. Use the conditional "if" below part of the code below to enter the servo movement code instead of using delays after the movement.

   if (currentMillis - previousMillis >= interval) {
     // save the last time you moved servo
     previousMillis = currentMillis;
//move servo
/* Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital
  pin, without using the delay() function.  This means that other code
  can run at the same time without being interrupted by the LED code.

  The circuit:
  * LED attached from pin 13 to ground.
  * Note: on most Arduinos, there is already an LED on the board
  that's attached to pin 13, so no hardware is needed for this example.

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald


  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
  */

// constants won't change. Used here to set a pin number :
 const int ledPin =  13;      // the number of the LED pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
 const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
   // set the digital pin as output:
   pinMode(ledPin, OUTPUT);
}

void loop() {
   // here is where you'd put code that needs to be running all the time.

   // check to see if it's time to blink the LED; that is, if the
   // difference between the current time and last time you blinked
   // the LED is bigger than the interval at which you want to
   // blink the LED.
   unsigned long currentMillis = millis();

   if (currentMillis - previousMillis >= interval) {
     // save the last time you blinked the LED
     previousMillis = currentMillis;

     // if the LED is off turn it on and vice-versa:
     if (ledState == LOW) {
       ledState = HIGH;
     } else {
       ledState = LOW;
     }

     // set the LED with the ledState of the variable:
     digitalWrite(ledPin, ledState);
   }
}

Has anyone noticed that the OP is still absent this discussion and still hasn't said what he wants his program to do?

Delta_G:
Has anyone noticed that the OP is still absent this discussion and still hasn't said what he wants his program to do?

Probably got scared off by some of the post. I think the OP wants to have five servos to move in a sweeping motion.