Please check programming

Hi,

Trying to set up my IDE to sweep 2 servos smoothly.
Got lots of bugs apparently. Servos are herky jerky and not sweeping smoothly at all. Would greatly appreciate a look see.Im sure its something simple Im not seeing. Amount of degrees in movement not my concern. Just wanting the servos to do it smoothly in both directions.
Thanks so much

Sweep 2 servos from 0 to 40 degrees and back to 0
#include <Servo.h>
Servo servo1; // create servo object to control a servo

Servo servo2; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup(){

myservo1.attach(9); // attaches the servo on pin 9 to the servo object

myservo2.attach(10); // attaches the servo on pin 10 to the servo object

}

void loop()

}

for(pos = 60; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees

}

// in steps of 1 degree

myservo1.write(pos); // tell servo to go to position in variable ‘pos’

delay(50); // waits 15ms for the servo to reach the position

}

for(pos = 60; pos>=20; pos-=1) // goes from 180 degrees to 0 degrees

}

myservo1.write(pos); // tell servo to go to position in variable ‘pos’

delay(50); // waits 15ms for the servo to reach the position

}

for(pos = 60; pos>=20; pos-=1) // goes from 180 degrees to 0 degrees

myservo2.write(pos); // tell servo to go to position in variable ‘pos’

delay(50); // waits 15ms for the servo to reach the position

}

for(pos = 20; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees} // in steps of 1 degree

}

myservo1.write(pos); // tell servo to go to position in variable ‘pos’

delay(60); // waits 15ms for the servo to reach the position

}

for(pos = 90; pos>=20; pos-=1) // goes from 180 degrees to 0 degrees

}

myservo2.write(pos); // tell servo to go to position in variable ‘pos’

delay(70);

}

}

Please edit your post to add code tags.

Servos require a separate power supply, such as a 4xAA battery pack for one or two small servos. Do NOT use the Arduino 5V output, and don't forget to connect the grounds.

the code defines "servo1" and "servo2" instead of "myservo1".

in many cases, the code had a closing brace, "}", instead of an opening brace, "{", after the loop() and after several of the for loops

#include <Servo.h>

Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup(){
    myservo1.attach(9); // attaches the servo on pin 9 to the servo object
    myservo2.attach(10); // attaches the servo on pin 10 to the servo object
}

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

    for(pos = 60; pos>=20; pos-=1) // goes from 180 degrees to 0 degrees
    {
        myservo1.write(pos); // tell servo to go to position in variable `pos'
        delay(50); // waits 15ms for the servo to reach the position
    }

    for(pos = 60; pos>=20; pos-=1) // goes from 180 degrees to 0 degrees
        myservo2.write(pos); // tell servo to go to position in variable `pos'
    delay(50); // waits 15ms for the servo to reach the position

    for(pos = 20; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees} // in steps of 1 degree
    {
        myservo1.write(pos); // tell servo to go to position in variable `pos'
        delay(60); // waits 15ms for the servo to reach the position
    }

    for(pos = 90; pos>=20; pos-=1) // goes from 180 degrees to 0 degrees
    {
        myservo2.write(pos); // tell servo to go to position in variable `pos'
        delay(70);
    }
}

Hi,

Yes I did that. Im using a voltage step up module with a 6 volt battery pack. Voltage increased to 8 volts. Thank you for responding.

Great. Thank you very much.

Why? Most servos do not tolerate more than 6V.

Post a link to your servos.

It looks like you intend your sketch to look like this:

#include <Servo.h>
Servo servo1; // create servo object to control a servo

Servo servo2; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup()
{
  servo1.attach(9); // attaches the servo on pin 9 to the servo object
  servo2.attach(10); // attaches the servo on pin 10 to the servo object
}

void loop()
{
  // goes from 60 degrees to 180 degrees
  // in steps of 1 degree
  for (pos = 60; pos <= 180; pos += 1)
  {
    servo1.write(pos); // tell servo to go to position in variable ‘pos’
    delay(50); // waits 50 ms for the servo to reach the position
  }

  // jumps from 180 degrees to 60 degrees and then
  // goes from 60 degrees to 20 degrees
  for (pos = 60; pos >= 20; pos -= 1) // goes from 180 degrees to 0 degrees
  {
    servo1.write(pos); // tell servo to go to position in variable ‘pos’
    delay(50); // waits 50 ms for the servo to reach the position
  }

  // goes from 60 degrees to 20 degrees
  for (pos = 60; pos >= 20; pos -= 1) // goes from 180 degrees to 0 degrees
  {
    servo2.write(pos); // tell servo to go to position in variable ‘pos’
    delay(50); // waits 15ms for the servo to reach the position
  }

 // goes from 20 degrees to 90 degrees
  for (pos = 20; pos <= 90; pos += 1)
  {
    servo1.write(pos); // tell servo to go to position in variable ‘pos’
    delay(60); // waits 15ms for the servo to reach the position
  }

  // goes from 90 degrees to 20 degrees
  for (pos = 90; pos >= 20; pos -= 1)
  {
    servo2.write(pos); // tell servo to go to position in variable ‘pos’
    delay(70);
  }
}

john
isn't the correctly formatted and corrected code already posted?

You had already posted a working sketch based on the original when I posted my response to the original post. Sorry I was late.

Thanks so much guys!. Really appreciate it.

This is the attached schematic that was recommended to me previously but I agree, 5VDC is standard for a servo. Maybe you can help understand the logic here.
Thanks

The schematic shows a 6V battery powering the servos, which is standard.

It is very inefficient to increase the battery voltage to 8V, then have the on-board regulator reduce it back down to 5V for the Arduino power.

Most people would use a buck/boost or low drop out regulator to power the Arduino directly through the 5V pin.

noise on the 6V supply due to the servos is filtered using the regulator on the arduino

Thanks so much. I honestly didnt think batteries could cause any noise as opposed to an insufficiently filtered step down power supply.

Thanks again John.
The two servos are rotating now which is great but still having one chronic problem that hopefully you can check. The two servos rotate slowly in one direction as desired but then after rotating to max direction they snap back to their starting position and start over. Ive had this same problem in the past with other programs using servos so I'm wondering why this keeps occurring.
I am working hard at learning to program and Ill eventually get it but it seems like I'm always under a time crunch.
Best.

loop() is repeated called.
it goes thru the sequences and then immediately repeats

(S1 suddenly jumps from 90 to 60)
S1, 60 to 180
(S1 suddenly jumps from 180 to 60)
S1, 60 to 20
                  (S2 suddenly jumps from 20 to 60)
                  S2, 60 to 20
S1, 20 to 90
                  (S2 suddenly jumps from 20 to 90)
                  S2, 90 to 20
and repeat...

If you don't want jumps, each move has to start at the same place the last move ended!

Ok thank you so much!!

Thanks so much!!