Servo On/Off using 2 push buttons.

Help guys. I'm newbie and working on arduino uno code where if button 1 is clicked once(not long pressed) servo rotates forward and backward continuously and when button 2 is clicked servo stops rotating. I've been looking for solutions for quite a while but can't find any thread or alternatives to modify it. Any suggestion is appreciated.

#include <Servo.h> 
const int buttonPin1 = 2;    
const int buttonPin2 = 3;
int buttonState1 = 0;        
int buttonState2 = 0;
Servo myservo;  // create servo object to control a servo 
                     
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
   myservo.attach(4);  // attaches the servo on pin 4 to the servo object 
   pinMode(buttonPin1, INPUT);
   pinMode(buttonPin2, INPUT); 
  
} 
 
void loop() 
{ 
 buttonState1 = digitalRead(buttonPin1);
 buttonState2 = digitalRead(buttonPin2);
 
  if (buttonState1 == HIGH)
{
     
  for(pos = 0; pos <= 180 ; pos += 1) // ges 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 
  }    
  for(pos = 180; pos>=0; 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 
  } }
 if (buttonState2 == HIGH){  
    myservo.write(pos);
}
}

Hi I havent tested the code below but it should do what you need.

The button you press only stays high when you press it so you can hold it in and your servos should move forward and back or add a variable, I just added a variable that changes when the button is pressed so the if statement is true each time the loop runs through and when button 2 is pressed the variable will change back to 0.

#include <Servo.h>
const int buttonPin1 = 2;   
const int buttonPin2 = 3;
int buttonState1 = 0;       
int buttonState2 = 0;
Servo myservo;  // create servo object to control a servo

int ServoMove = 0;
                     
 
int pos = 0;    // variable to store the servo position
 
void setup()
{
   myservo.attach(4);  // attaches the servo on pin 4 to the servo object
   pinMode(buttonPin1, INPUT);
   pinMode(buttonPin2, INPUT);
 
}
 
void loop()
{
 buttonState1 = digitalRead(buttonPin1);
 buttonState2 = digitalRead(buttonPin2);
 
 if (buttonState1 == HIGH){
  ServoMove = 1; 
 }
  
  if (ServoMove == 1)
{
     
  for(pos = 0; pos <= 180 ; pos += 1) // ges 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
  }   
  for(pos = 180; pos>=0; 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
  } }
 if (buttonState2 == HIGH){
    ServoMove = 0; 
    myservo.write(pos);
}
}

M.......

Many thanks Undermentioned. Sorry for not replying immediately i'm having internet problem. The code works great for button 1 except button 2 does not give function. I'm looking on it now hopefully i will post results after i figure it out. Thanks again!

Hi,
How have you got your buttons connected?
If you are pulling the arduino input HIGH with the button, you will need a 10K resistor from the arduino input to arduino gnd, this will make sure with the button open that the input is LOW.

Tom.... :slight_smile:

Thank you Tom. I used 5010 servo and followed a simple 2 button layout from google which I used 10k or 1k resistor to connect button to ground and arduino input. I found out that when button 2 is pressed while servo is moving it doesn't give an output but when the servo has finished a revolution and return to zero then I click the button, it will stop. Also long pressing button 2 will not stop the servo but when it finishes an entire revolution while long pressing servo will stop when it reach pos 0 . Is there a way that servo will automatically return zero when button 2 is clicked? I can't find my error.

The initialization happens first and exactly once. Each time through the loop, the condition is tested; if it's true, the statement block, and the increment is executed, then the condition is tested again. When the condition becomes false, the loop ends.

That means that your button does not get tested to see if its pressed while you are in the for loop, you can always read the button in your for loop and if the button is high use a break; statement to exit the for loop.

M.....

Edit: the quote above is reference to a for loop.

Why not do it properly ?
When the button becomes pressed set a flag variable to indicate that servo movement is required and save the start time from millis(). Then, each time through loop() if the flag variable is true test whether the current millis() value minus the start time is greater than the time required between each servo step. If so then update the servo position variable by adding the step size to it, move the servo to the new position and save the time of the move. When/if the servo reaches its end position change the sign of the step size variable. In loop() test the button state each time through. When the button becomes pressed again set the movement required flag to false to stop the servo. Move the servo to its home position if required.

Thank you very much for all your concern guys. I'm only good at basic programming. Seems I have to stick with 1 button(on/off) to make it easier because later I'll need to add dc motors,led,buzzer,ldr,etc. 2 button looks complicated to me for servo and specially in combining codes. Hoping for your assistance later on. More powers!