Sweep + buttonstate question

Hey there,

I'm rather new to working with an Arduino but have to start useing a Servo for a school project now.
What i'm trying to achieve is that when a push button is pressed the servo will follow the Sweep library script, and when the button is pressed again is stops, until it is being pressed again (and follows the script again).
what it does with my current program ; script uploaded - nothing happens to the servo - button pressed - servo sweeps 2x and stops, i can press the button again and the servo will do another set of 2 sweeps.
Not bad, but not exactly what im looking for pref. i'd like an on // off kinda thing going on :slight_smile:

#include <Servo.h>

Servo myservo;

const int servoPin   = 9;      // the pin that the servo is attached to
const int buttonPin  = 11;     // the pin that the pushbutton is attached to

int servoState       = 0;     // current state of the servo
int buttonState      = 0;     // current state of the button
int lastButtonState  = 0;     // previous state of the button

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

void setup() {
  pinMode(buttonPin, INPUT);  // initialize the button pin as an input
  pinMode(servoPin, OUTPUT);  // initialize the servo pin as an output
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  // read the pushbutton input pin
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
   
    // change the state of the servo when someone pressed the button
    if (buttonState == 1) {
      if(servoState==1) servoState=0;
      else            servoState=1;        
    }
    
    { 
  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(10);                       // 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(10);                       // waits 15ms for the servo to reach the position 
  }

   
    lastButtonState = buttonState;
  }
 
  // turns servo on if the servoState=1 or off if the servoState=0
  digitalWrite(servoPin, servoState);
 
  //delay(20);
}
}

any pointers would be greatly appreciated!
-R

The behavior you are seeing is exactly what you coded the program to do. That is, when the button is pressed, the two for loops run to completion.

The servo library manages the digital pin that the servo is attached to, so the servoState stuff you are doing isn't accomplishing anything.

To get the servo to move only when the button is held down, you'll need to get rid of the for loops, and read the button state a lot more often. The previous state of the button is irrelevant, too.

int servoVal = 0;
int servoInc = +1;

void loop()
{
   if(digitalRead(buttonPin) == HIGH)
   {
      servoVal += servoInc;
      myservo.write(servoVal);
      delay(10);

      if(servoVal >= 180)
      {
         servoInc = -1;
      }
      if(servoVal <= 0)
      {
         servoInc = +1;
      }
   }
}

This code will read the button state. If it's pressed, it will calculate a new value to move the servo to, move the servo, pause to let the servo get there, and determine what the next step value should be (+1 or -1).

Then, loop ends, and is called again.