motor runs continuously in void setup

Hello all,

I am running a for loop within void setup. This for loop is used for ramping the servo speed up from stationary to full speed in one direction. According to the for loop the motor should stop as soon as it reaches the top speed, as this is the last iteration of the for loop. Instead the speed ramps up, reaches the top speed, and then maintains the top speed indefinitely…

Please could somone inform me as to why the for loop is not stopping at the last iteration…

Thanks

Here is my code:

#include <Servo.h> //servo library

Servo myservo1; //projector platform servo

unsigned long Timer1; //define timer variable for state 1 if statement

void setup()
{ 
  myservo1.attach(9); // attatch servo 1 to pin 9
  for(int speedv1 = 0; speedv1 <= 100; speedv1 += 2) // loop to ramp up speed of servos
  {
  myservo1.writeMicroseconds(1474 + speedv1); // speed increase by 2 each iteration (servo 1) until servo reaches fullspeed (ACW)
  delay(30); // delay between loop iterations
  }
}

void loop(){}
 for(int speedv1 = 0; speedv1 <= 100; speedv1 += 2)
  speedv1 <= 100

According to the for loop the motor should stop as soon as it reaches the top speed

Where is the command to STOP ?

  #include <Servo.h> //servo library

Servo myservo1; //projector platform servo

unsigned long Timer1; //define timer variable for state 1 if statement

void setup()
{ 
       myservo1.attach(9); // attatch servo 1 to pin 9
       for(int speedv1 = 0; speedv1 <= 100; speedv1 += 2) // loop to ramp up speed of servos
          {
            myservo1.writeMicroseconds(1474 + speedv1); // speed increase by 2 each iteration (servo 1) until servo reaches fullspeed   
            (ACW)
             delay(30); // delay between loop iterations
           }
               //[MOTOR SPEED = ZER0 COMMAND SHOULD BE HERE !
                // If you wanted to ramp it down, that FOR loop would be here. Otherwise , insert command to STOP MOTOR HERE.
               // loop to ramp DOWN speed of servos would go here
               //  for(int speedv1 >= 100; speedv1 = 0; speedv1 -= 2) // loop to ramp up speed of servos
                 // {
              // command to decrease motor speed by 2 would go here if you wanted to ramp it down.
              //  delay(30); // delay between loop iterations
             //     }
   }

Okay, but why does the for loop get to the last iteration and just keep writing this value to the motor. After the last iteration is complete, nothing should happen as the loop is finished...

Once you have written the command and sent it to the motor controller, the command is written to ram in the controller and executed until another command replaces it.

Do you have the datasheet for the motor controller ? Did you read it ?

Ah okay. I didn’t realize this occurred. Thanks for the help.

Do you have the datasheet for the motor controller ? Did you read it ?

I presume you are using a continuous rotation servo.

If you are using the Arduino servo library there is no separate motor controller.

When you use the Arduino servo library such as with myServo.writeMicroseconds(1700); the Arduino will continue to send 1700 microsecond pulses to the servo for ever - unless it gets another command.

If you want to stop the motor you need another command that sends the appropriate number of microseconds that causes the motor to stop. That will be somewhere around 1500 usecs, but you will need to experiment with your particular motor.

...R

the Arduino will continue to send 1700 microsecond pulses to the servo for ever - unless it gets another command.

I guess I was right but maybe not for the right reasons.
Are we talking about a little 5V RC servo running totally off the arduino ?

@Robin2

Yes thats exactly my case. Now that I know it keeps writing the last writeMicroseconds() value to the motor ive been able to manipulate my code correctly :slight_smile:

YIPPEE !