Servo Interrupt.

I want to sweep a servo back and forth slowly. I also have a button, where when pushed, the servo will stop sweeping and go to center position. I’m doing the slow sweep with a for loop and delay. The problem is the servo sweep must complete its movement then go to center position. Is there a way to interrupt the slow servo sweep?

#include <Servo.h>
long randNumber;

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 100; // variable to store the servo position
const int led = 8; //name pin 8 as led
const int button = 7; //name pin 7 as button
int leaf = 200;
void setup() {
pinMode(led,OUTPUT); //set pin 8 as OUTPUT
pinMode(button,INPUT) ; //set pin 7 as INPUT
myservo.attach(9);

}

void loop() {
delay(10);
int reads = digitalRead(button); //read the digital value on pin 7
digitalWrite(led,reads); //set the digital output
leaf = 200;
for (pos = 100; pos <= 140; pos += 1) {

if (reads==1)
leaf = 1;
myservo.write(pos);

delay(leaf);
}
for (pos = 140; pos >= 100; pos -= 1) {
myservo.write(pos);
delay(20);
myservo.write(pos);
}

}

get rid of the 2 for loops and just architect your code to use the main loop to do the sweeping.

declare 2 global variables

int incrementWith = 1; // will be -1 when we go the other direction
unsigned long previousMillis = 0;

also initialize your servo at the right position in the setup()

    myservo.write(pos);

then modify the loop

void loop() {
  int reads = digitalRead(button);   //read the digital value on pin 7
  digitalWrite(led, reads);     //set the digital output
  if (reads == HIGH) { // I'm assuming pressing == HIGH as you did not declare INPUT_PULLUP
    // button is pressed
    // we don't do anything with the servo, the button is pushed
    // but you can do something else here if you have to

    // some code can come here


  } else {
    // button is low, we keep the sweep moving when it's due
    // we check if it is time to move the servo
    if ( millis() - previousMillis > 20) {
      // the delay to allow the servo to move has gone, move to next step
      pos += incrementWith;
      myservo.write(pos);
      // we check for bounds
      if ((incrementWith  == 1) && (pos == 140)) {
        // we reached the top most, reverse direction
        incrementWith = -1;
      } else if ((incrementWith == -1) && (pos == 100)) {
        // we reached the bottom most, reverse direction
        incrementWith = 1;
      }
      previousMillis = millis();
    } else {
      // the servo might be still moving from previous command
      // so we don't move it at this point
      // that's an opportunity to do something else

      // some code can come here

    }
  }
}

I've not tested it, so try to read and understand the idea - fix the errors if any.

The idea is that there is no delay, the loop loops as fast as it can but will check if 20 ms have passed before asking the servo to move again. 20ms is an eternity for your arduino, so you don't want to block it in a delay() call while you could do other things like checking if the button is being pressed.

Have a look at the demo Several Things at a Time. It contains servo code and a responsive button.

...R

@robin2:

I'm guessing after the 10th time you post the same thing, you must be thinking " There must be a better way ".

@ J-M-L :

Ill collect all this snippet for future reference.

Ashraf_Zolkopli: @robin2:

I'm guessing after the 10th time you post the same thing, you must be thinking " There must be a better way ".

I wrote the tutorial so that I could easily convey a lot of information with a short post - I am too lazy to write it all out every time :)

...R

@robin2:

Yeah I know, I book mark and have an offline copy of both your Several thing at a time and also your Serial Input basic. I refer them almost all the time.

Ashraf_Zolkopli: Yeah I know, I book mark and have an offline copy of both your Several thing at a time and also your Serial Input basic. I refer them almost all the time.

Thank you for your kind words. I had misunderstood the intent of Reply #3

...R

J-L-M your program worked. Thanks. I spent many hours trying to get the delay operated servo to work right. While in delay the chip is useless.