servo with push button

hello i've been toying around with stepper motors and found they would be too hard for me so i have decided to try with a servo.

im trying to get a servo to drive from 0 - 180 degrees with the press of a button, first button driving - direction till 0 degrees and second button driving + direction too 180 degrees

i can get the servo to drive from 0 - 180 degrees but i want it to do this 1 step at a time rather then telling it to go from 0 - 180 degrees. this way i would be able to stop as soon as i let go of the button.

i am making an arm that can spin with 2 buttons forward and backwards so i need to be able to stop when i let go of the button and not go to predefined positions any help is greatly appreciated.

here is the code i have scrapped up so far.

#include <Servo.h> 

const int forwardbuttonPin = 2;     // the number of the pushbutton pin.
const int backwardbuttonPin = 3;
const int ledPin =  13;      // the number of the LED pin.

// variables will change:
int forwardState = 0;         // variable for reading the pushbutton status.
int backwardState = 0;
int pos = 0;    // variable to store the servo position 

Servo aaxisservo;  // create servo object to control a servo 
                 // a maximum of eight servo objects can be created 
  


void setup() {
  
   aaxisservo.attach(11);  // attaches the servo on pin 9 to the servo object 
   
   pinMode(ledPin, OUTPUT);      // initialize the LED pin as an output.
   pinMode(forwardbuttonPin, INPUT);     // initialize the pushbutton pin as an input.
   pinMode(backwardbuttonPin, INPUT); 
   aaxisservo.write(pos=90);
}

void loop(){
   
   forwardState = digitalRead(forwardbuttonPin);    // read the state of the pushbutton value.
   backwardState = digitalRead(backwardbuttonPin);
   
   
   if (forwardState == HIGH) {     // check if the pushbutton is pressed.
         
     digitalWrite(ledPin, HIGH);  // turn LED on.
     aaxisservo.write(pos=pos+1);              // tell servo to go to position  
    
   } 
   else {
     // turn LED off:
     digitalWrite(ledPin, LOW); 
   }
   
   if (backwardState == HIGH) {
     
     digitalWrite(ledPin, HIGH);
     aaxisservo.write(pos=pos-1);              // tell servo to go to position  
   }
   
   else {
     digitalWrite(ledPin, LOW);
   }
}

cheers ash.

You want to separate the code into two parts.

One part looks at the buttons' state and sets a target_position variable (and
changes the LED?)

The other controls the servo and is something like:

  if (millis () - last_servo_time >= SERVO_DELAY) // delay between servo steps
  {
    last_servo_time += SERVO_DELAY ;   // setup for next time
    if (current_position > target_position)  // move current position one step closer
      current_position -- ;
    else if (current_position < target_position)
      current_position ++ ;
    servo.write (current_position) ;
  }

So whenever target_position is updated this code will start moving the servo
as approriate at a constant rate.

Two button servo test code.

//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //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;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}