Servo operated by push button

Hello everyone! So, I'm working on a little project and I cant figure out how to set up the programing on the Arduino, so I was hoping that someone would be able to give me a little help.

I have a servo, that I want to be at position = 0 while the push button is in the LOW position. When I hit the push button in the HIGH position I need the servo to sweep to position = 180 and then back to position = 0, ending the cycle, until the push button is hit again.

I have the servo working with no problem and I set up a push button with no problem but the specific task I need it to do when the button is hit is not working. I'm sure it has to do with programing the push button a little different than how I have it already.

If anyone can help me with the code on this that would be GREAT! I'm fairly new to the Arduino sketches so please bare with me, I have about 40 hours invested into figuring out a solution to this, so I feel like I gave it a good run, before asking for help. I was looking forward to doing this one on my own. But I would like to see my project in motion rather than being stuck on something that I'm sure is fairly simple, but I'm not experienced enough with code to figure it out.

Thank you for any help you guys are willing to give!

-Pete

Post the code you have.

As soon as I get home I will, I took the sketch for the push button hold and the sketch for servos and tried to combine them.... It's all types of messed up, lol! As I said I have very little experience with code. I appricate your willingness to help.

Okay, well here it is... I didn't know how to combine the two of them properly... I'm still working on it, any guidance you can give would be very helpful. Also, if it's not to much to ask, I'm really not looking for someone to just do it for me, but if you can explain it so I can understand it a little more that would be awesome! Again, thank you for any help you can give!

#include <Servo.h>

Servo myservo;  
 
 int pos = 0; 
 int pushButton = 2;
 int LED = 13; 
 
 int state = HIGH;
 int readibg;
 int previous = LOW;
 
 long time = 0;
 long debounce =200;

 
 void setup() 
{ 
  {
    pinMode(pushButton, INPUT);
    pinMode(LED, OUTPUT);
  }
  
  {
  myservo.attach(9);   
  }{
  Serial.begin(9600);
  pinMode(pushButton,INPUT);
  }
} 
  
 void loop() 
{ 
  
{
  reading = digitalRead(pushButton);

  
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;
}
  {
    int buttonState = digitalRead (pushButton);
    Serial.println(buttonState);                            
  }
  
  
  for(pos = 0; pos < 180; pos +=1)   
  {                                   
    myservo.write(pos);               
                        
  } 
  for(pos = 180; pos >= 1; pos -=1)      
  {                                
     myservo.write(pos);                                   } 

}
}

Four things:

  1. Use code tags to post your code - the # button above lets you do this
  2. You have a lot of superfluous curly braces - the code will be a lot clearer without them
  3. Put a delay in each of your for loops - the arduino is fast. Your servo is being told to go to 180 degrees and back before it has a chance to move at all.
  4. The commands to move the servo are not dependent on the state of the button.

Fix number 3 first - then you should at least be able to confirm that your servo wiring is correct as it should then move.

Well, let me ask you a question in regards to the delays, I took them out for a reason. If you can tell me if this is correct. When you put a delay in a sketch the entire sketch takes a delay, so if the Arduino is doing a read operation at the dsame time a delay from a say a write operation, it will miss the command to read because of the delay.

I was trying to get 2 LEDs to blink but alternating them and I had to take the delay out for that same reason, that's why I tried not to use the delay.

So, if this is true, how would I go about pausing the operation without using the delay function?

How about this one? I'm getting an error in the script but I think I'm on the right track.... I need to get that delay function out of it, because in time I'm going to have more functions. Can you give me an idea as to what I would need to change to make it work and even make it work without the delay?

#include <Servo.h>

Servo myservo; /* create servo object to control a servo*/

int button = 0;
int val;

void setup() 
{
  myservo.attach(9); /* attaches servo to pin 9*/
}

void loop()
{
  val = analogRead(button);
  val = map(val, 0, 1023, 0);
myservo.write(val);
delay(15);
}

(code tags added by moderator)

With some hard work last night and a nice size knot on my head from banging it into the desk. I came out victorious from the first part of my battle with this project! I now have the servo being operated by the push button. Now there may be a different way to do it, but this is what I have done and it's working!

(Add code tags next post please!)

#include <Servo.h>

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
 int pos = 0;    // variable to store the servo position 
 int button = 2;  // The button will be on Pin 7 
 
 
 void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 pinMode(pos, OUTPUT);
 pinMode(button, INPUT); 
 digitalWrite (button, LOW);
} 
 
 void loop() 
{ 
  
    if (digitalRead(button) == LOW)

  for(pos = 0; pos < 90; pos += 90)  // goes from 0 degrees to 90 degrees 
  {                                  // in steps of  degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                          // waits 1s for the servo to reach the position 
  } 
  if (digitalRead(button) == HIGH) 
  
  for(pos = 90; pos>=90; pos-=90)     // goes from 90 degrees to 0 degrees 
  {                                
     myservo.write(pos);              // tell servo to go to position in variable 'pos' 
  delay(50);                             // waits 50ms for the servo to reach the position

I have it all working now, it wasn't as hard as I was making it out to be... It took me long enough to figure it out however, I have it working now and I have learned from my mistakes!

Great code. Exactly what i needed for my trigger on my skeet thrower. Great job. Forgot a couple things on the end of the code though : )....

#include <Servo.h>

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
 int pos = 0;    // variable to store the servo position 
 int button = 2;  // The button will be on Pin 7 
 
 
 void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 pinMode(pos, OUTPUT);
 pinMode(button, INPUT); 
 digitalWrite (button, LOW);
} 
 
 void loop() 
{ 
  
    if (digitalRead(button) == LOW)

  for(pos = 0; pos < 90; pos += 90)  // goes from 0 degrees to 90 degrees 
  {                                  // in steps of  degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                          // waits 1s for the servo to reach the position 
  } 
  if (digitalRead(button) == HIGH) 
  
  for(pos = 90; pos>=90; pos-=90)     // goes from 90 degrees to 0 degrees 
  {                                
     myservo.write(pos);              // tell servo to go to position in variable 'pos' 
  delay(50);                             // waits 50ms for the servo to reach the position 
  }
  
}

can anyone post a picture of the connection of the button to the servo please?? thanks a lot.. i cant seem to get the button to work with the servo..

I'm fairly new to arduino and I was wondering why do you put the "for" in there? Could you just make 2 pos's and flip between them when needed.

Thanks,

Sorta new to Arduino and tried this but I can't seem to get it wired correctly. If anybody can, please post a pic or an animation so I can learn how to wire it!!!

Hello everyone :slight_smile: so with a bit of research i figured out how to wire-up and operate my servo with a push button. The problem that I face now is cycling from one servo to the next using a third push button. currently i have button A controlling increase and button B controlling decrease, i would like to know how to make button C switch from servo 1 to servo 2. i tried incorporating the switch case function but now my code is too cluttered to make heads or tails of where its failing. if anyone could please point me in the right direction it would be greatly appreciated.

Hi, I need to rotate a servo through 270deg on four buttons, 0 - 90 -180 - 270 deg

zero position at start:

Press button 2 servo goes to position 90deg
Press button 3 servo goes to position 180deg
Press button 4 servo goes to position 270deg
Press button 1 servo goes back to position 0deg

Pressing buttons 1 - 2 - 3 - 4 in any order will take up assigned positions:
THE SERVO WILL BE MODDED TO GET 270DEG

Can any one help with code please
Mike

You can. The Button example in the IDE will show you how to read a button.

To move a servo to a specific position you just need to use servo.write(value). If you look at the Sweep or Knob examples they will show you the basics to set up to get started with servos.

You'll have work out what values you need to write() because although you use 0 to 180 if you've modified the servo to travel 270 degrees instead of the normal 180 then the values will all vary e.g. write(180) will actually go to 270.

Steve

@MikeMCA:
Please post details of your MOD.

You might try something like below.

//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.

#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);
  }
}

Hi all, thank you for getting back to me, as I an very new at this coding thing its all a bit AGHHHH, hardware now thats my thing so if anyone has code to do this, it would get me out of a rather large hole I seem have dug myself...
The project must have four buttons to control the servo

I have found that I can buy a servo that will go from 0 to 270 deg,

servo starts at 0 deg from start

Press button 2 servo goes to position 90 deg
Press button 3 servo goes to position 180 deg
Press button 3 servo goes to position 270 deg
Press button 1 servo goes to position 0 deg

pressing button 1 will reset the position to 0 deg
or pressing button 1 - 2 - 3 - 4 will take up the assigned deg

It must not go over centre, say from 270 deg straight to 0 deg but travel the same track, the reason for this is the servos are rotating a small platform, the platform has wires attached to it, so, if it did the wires would get twisted up around the centre.

Mike

As I said I am very new at code, can someone look at this a tell me if it will work.

de: [Select]
//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually DOES NOT WORK.

#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 = 90;

int button3 = 6; //button pin, connect to ground to move servo
int press3 = 180;

int button4 = 7; //button pin, connect to ground to move servo
int press4 = 270;

Servo servo1;

void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);

servo1.attach(7);

digitalWrite(1, HIGH); //enable pullups to make pin high
digitalWrite(2, HIGH); //enable pullups to make pin high
digitalWrite(3, HIGH); //enable pullups to make pin high
digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(0);
}

press2 = digitalRead(button2);
if (press2 == LOW)
{
servo1.write(90);
}

press1 = digitalRead(button1);
if (press3 == LOW)
{
servo1.write(180);
}

press1 = digitalRead(button1);
if (press4 == LOW)
{
servo1.write(270);
}

}