Go Down

Topic: Stopping a Servo Sweep. (Read 612 times) previous topic - next topic


Hello people.

I'm working in a small project. I basically need two servos to sweep mirrored one to the other and stop via a push button. My problem is that I already got it working, and it starts the sweep when I press the button. The problem comes to stopping it, as it only does when I press the button at the moment the servos finish their sweeping cycle. If I press the button at any other moment, they wont stop.

My code is as follows, but basically is just a slightly modified version of the Sweep example Arduino comes with.
Code: [Select]
#include <Servo.h>

Servo myservo; 
Servo myservo2;

int pos = 0;

int switchpin = 2;
int stoppin = 3;
boolean lastbutton = LOW;
boolean currentbutton = HIGH;
boolean Servosmove = false;

void setup()
  pinMode(switchpin, INPUT);

boolean debounce(boolean last)
  boolean current = digitalRead(switchpin);
  if (last != current)
    current = digitalRead(switchpin);
  return current;

void loop()
  currentbutton = debounce(lastbutton);
  if (lastbutton == LOW && currentbutton == HIGH)
    Servosmove = !Servosmove;
  lastbutton = currentbutton;
  if (Servosmove == true) {
     for(pos = 90; pos>=1; pos-=1)
     for(pos = 0; pos < 90; pos += 1)
} else if (Servosmove == false) {

Basically, I just need to know


I am sure that you realise that the problem is caused by the use of delay() within the for loops which means that for nearly 4 seconds the program is tied up and cannot read the input.  The solution is not to use delay() for the timing but to use the principle shown in the BlinkWithoutDelay example.  This involves starting an action, such as moving the servo then checking frequently (each time through loop()) whether it is time to do something else, such as moving the servo again.  If not then do something else such as checking an input and acting on it,
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


The problem is your for loops. There is nothing in there that interrupts them, and they block the execution of the loop function. Your loops will keep running until they reach their end.

You need to completely restructure your code in order to simultaneously monitor the button and move the servos. I'd recommend a simple state machine (http://www.gammon.com.au/forum/?id=12316) with two states: SWEEP and STOP. Use a couple of global variables to keep track of the servo position, and use BlinkWithoutDelay style coding to control the servos without blocking. Use the button press to transition between the two states.

It's stupid that so many of the Arduino examples use delay when it's a horrible way to program. It works only for the trivial examples they give, but as soon as you need to start doing something more complicated, you need to ditch the delays and work with a different paradigm.


Actually, you only delay 20 ms at a time. You just need to check the switch within the for loops and if pressed, use break to exit the for.

That being said, UKHeliBob is right... a "Blink without delay" technique is much better.
There are 10 kinds of people in the world,
those who understand binary, and those who don't.


Two or three hours spent thinking and reading documentation solves most programming problems.


Thank you all.  :)

Following Robin's example I managed to make it switch states at any moment. It's still a little bit faulty but at least the biggest problem is gone.


It's still a little bit faulty but at least the biggest problem is gone.

If you want more advice please post your latest code and describe the problems.

Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131