Controlling servo with pushbuttons

I’m trying to control a servo using two pushbuttons. One to make it rotate clockwise while the button is pushed and the other counterclockwise. My code looks like

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
const int button1 = 2;
const int button2 = 3;
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
 
void loop() 
{ 
  int state1,state2;
  state1 = digitalRead(button1);
  state2 = digitalRead(button2);
  if ((state1 == LOW) && (state2 !=LOW)) 
  {   
    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 
    }}
  else if ((state2 ==LOW) && (state1 != LOW))
 { 
    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 
    } 
} }

and is modified from the code “Sweep” in the example library. Once the button is pushed,however, the servo rotates the full 180 degrees. How do I alter it to only move while the button is pressed?

Zoomkat will be along shortly with some example code.

clawedpurse:

....

if ((state1 == LOW) && (state2 !=LOW))
 {
   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
   }}
 else if ((state2 ==LOW) && (state1 != LOW))
{
   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
   }
} }

Hi and welcome.
You are checking for a button press and then controlling the servo until it has reached the end.
You want to check a button press and then control the servo for one step unless it has reached the end.
The difference is that you are not checking that button between the steps, you are doing the full sweep once the button has been pressed.

Remember that the second { } makes the code between them is executed as long as pos < 180 (or pos >= 1, depending on where you are in your sketch).
You will not exit this before the movement of the servo is completed, and so the buttonstate isn’t checked in between…

for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees

No, it doesn’t.

Oops, missed that.

I don’t have a servo to test with, but the below code should move a servo in the desired direction when the appropriate button is pressed. I only tested the code by touching a wire between ground and pins 5 and 6.

//zoomkat servo button sweep test 12-23-2013
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 6; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;
int pos = 90; // variable to store and set the servo position 

void setup()
{
  Serial.begin(9600);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  servo1.write(pos); //starting position
  digitalWrite(6, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
  Serial.println("servo button sweep test 12-23-2013");
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    pos=(pos+1);
    if(pos>180) pos=180; //limit upper value
    Serial.println(pos); //for serial monitor debug
    servo1.write(pos); // tell servo to go to position in variable 'pos' 
    delay(150); // waits 150ms to slow servo movement 
  }    

  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    pos=(pos-1);
    if(pos<0) pos=0; //limit lower value
    Serial.println(pos); //for serial monitor debug
    servo1.write(pos); // tell servo to go to position in variable 'pos' 
    delay(150); // waits 150ms to slow servo movement
  }
}