Moving a servo motor 90 degress with push of button

Hello! I am very new to Arduino and am hitting lots of road blocks.

I want to be able to push a button and have the servo move 90. I then want to push it a second time, and have it move another 90 degrees...and so on and so fourth, continuously.

I've been reading a lot of these forum and all I can seem to get is having it go 180 degrees and then back to zero, or it does one 90 turn and then stops.

If anyone has any insight, I would be very appreciative! Thanks!

Make your code easy to read then post it.
Also:

please use code blocks!

You need a continuous rotation servo.
Or modify yours to be one.

...and so on and so fourth, continuously.

Normal servos don't rotate 360 degrees. Do you have a special one that does? If you have a continuous rotation servo, check the specs to see how to control it.

Are you able to control it under software control (without the pushbutton)?

I don't know much about continuous rotation servos... With a normal servo, you can't send a "90 degrees more" message. If you want to move from 90 to 180 degrees, you send it a 180 degree command. If you want to "go back 20 degrees" from there, send a 160 degree message.

So, I did some more research and found that servos only turn 180degrees...like I said, I am VERY new to Arduino.

Therefore, I now just want it to go in 5 30 degree increments until it reaches 150 degrees.

I've been playing around with this code, and I can now get it to turn 30 degrees once when pushing the button. But after that, it stops. I've labeled the positions at the top. Again, I would love your wonderful help.

// test sketch a.2
#include <Servo.h>

  Servo servo1;          // define servo aliases
  #define leftPin 2        // define buttons attached to pins
  int pos1 = 30;          // set angle when inialitized
  int pos2 = 60;
  int pos3 = 90;
  int pos4 = 120;
  int pos5 = 150;
  
  int delayPeriod = 80;   // increasing this slows down the servo slew movement
                          // 1000 too much 300 jumpy 100 smooth slew 80 fast 50 
                          // too fast 70 testing
                          // Project addition idea:
                          // add variable to control slew rate via potentiometer 
                          // connected to analog input pin
                          
void setup()
{
  //test
  servo1.write(pos1);  // Put servo1 at home position of 0 degrees
    
  servo1.attach(9);    // attaches the servo on pin 9 to the servo object

  pinMode(leftPin, INPUT);    
 
//  digitalWrite(leftPin, HIGH);    // turn on pullup resistor
 //test 
 
  if(digitalRead(leftPin) == LOW);    // turn on pulldown resistor

}
void loop()
{
  if(digitalRead(leftPin) == HIGH)        // left button instructions  
  {
   // in steps of 1 degree
   if( pos1 > 0)
	--pos1;
    servo1.write(pos1);		  // tell servo to go to position in variable 'pos1'
    delay(delayPeriod);
    
    if( pos2 > 0)
	--pos2;
    servo1.write(pos1);		  // tell servo to go to position in variable 'pos2'
    delay(delayPeriod);	
    			    
  }

 }

Thanks!!

You need a variable to keep track of the button presses.

  • Assume the variable starts at 0
  • Each time the button is pressed add 30 to the variable
  • Check that the value does not exceed the permitted maximum.
  • Move the servo to the current value of the variable.

If you want position control for more than 180 degrees you could use a sail winch servo which can turn through about 3 revolutions.

...R