Servo operated by push button

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

}

It's not going to work if you keep reading button1 into press1 then checking press3 or 4.

And connecting both button4 and the servo to the same pin is a bit silly.

Also doing those digitalWrites to pins that the buttons aren't connected to won't help much either.

Then there's the fact that servo.write() only takes values from 0 to 180 and these are not real angles. 180 just tells the servo to go as far as it can. So if your servo's max angle is 270 then write(180) will send it to 270 and you'll need to work out the intermediate values from there.

Steve

I am sorry I do not understand code as a compleat beginner to this, so thought i will have a go. I do need a lot of help with this code. if any one can sort it out for me it would so good, then i can get a project finished.

Only time i have written code is on my very old VIC20 C64 about 35 years ago.
software to me is a black art. i am a hardware engineer, cant stone me for trying lol

But please can someone help me.
Mike

"But please can someone help me."

So what have you actually tried yourself? You only need two wires to touch together to simulate a switch. You don't even need a servo, just print the servo control numbers to the serial monitor. You might try using the forum search in the upper right of this page and searched for "servo button"?

MikeMCA:
I am sorry I do not understand code as a compleat beginner to this, so thought i will have a go. I do need a lot of help with this code. if any one can sort it out for me it would so good, then i can get a project finished.

The code you posted was nearly there. So which part of the detailed help I tried to give you are you having trouble with?

Ask questions and you will get answers but it's not very likely that anyone is going to want to just write all your code for you.

Steve

//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 = 100;

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

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

press3 = digitalRead(button3);
if (press3 == LOW)
{
servo1.write(100);
}

press4 = digitalRead(button4);
if (press4 == LOW)
{
servo1.write(180);
}

}