Delay Motor Problem & Controlling 2 Motors are different speed.

Hello,

I have a problem with my servo motor. I am currently using a arduino uno, and a TowerPro MG995 Servo motor. This motor was from my starter kit. I am using the sweep example code from the Arduino and for some reason when I changed the delay from 15ms to 1000ms, the servo no longer runs from 0 to 180 and reverse. It just goes to a certain angle and gets stuck at that point. I only changed the delay in the code and nothing else.

Here is the code:

// Sweep
// by BARRAGAN http://barraganstudio.com
// This example code is in the public domain.

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

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

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

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

In that code I changed delay(15) to delay(1000).

Also, I was wondering how can I change my code to make my 2 motors run at different speeds. This is currently my code but after awhile they start to run together at the same speed. Which I do not want. I want myservo1 to run 20 degrees ahead of myservo2 for every position (degree). Here is the code:

#include <Servo.h>

Servo myservo0, myservo1;

int pos = 0;

void setup()
{
myservo0.attach(9);
myservo1.attach( 8 );
}//setup

void loop()
{
for(pos = 0; pos < 180; pos += 1)
{
myservo0.write(pos);
myservo1.write(pos + 20);
delay(10);
}//for

for(pos = 180; pos >= 1; pos -= 1)
{
myservo0.write(pos);
myservo1.write(pos + 20);
delay(10);
}//for
}//loop

Take both delays OUT of the FOR loops and put them right AFTER the FOR loops.

but wouldn't that ruin the whole point? The would just make me go from 1 to 180 without ever giving it a delay.

Sorry ,I didn't realize you were just trying to slow down the sweep. Have you tried 999 ?

that does not work either.

Also, I was wondering how can I change my code to make my 2 motors run at different speeds.

If you want the motors to run at different speeds you can’t use delay() for the timing as it affects the entire program. You need need to use the timing technique in the BlinkWithoutDelay example sketch. I wrote a more extensive example in the first post in this Thread.

…R

I don't know why that delay doesn't work inside the FOR loop. That's a question for more advanced programmers. Good luck with that. It seems a mystery but there is probably some simple explanation .