moving a servo left & right with 2 buttons

ok here is what im trying to do. i have 2 buttons an arduino and a servo. when i hold down button number 1 i want the servo to turn left. when i let go of the button i want it to stop. when i press button number 2 i want it to turn right and when i let go i want it to stop. I also want it to turn slow (adjustable by changing the numbers in the code. well i have it built but i em having a real hard time with the code. could some one give me a hand? Thank you.

Here is one way to do what you want:

In loop, test to see if one of the buttons is pressed:
if Button 1 is pressed then decrease a variable that holds the servo angle, checking to make sure the angle does not go below 0.
If Button 2 is pressed then increase the variable holding the servo angle, checking to make sure the angle does not go above 180.
Write the servo angle to the servo.
Delay 20 milliseconds (this is important to debounce the switch)

You can have another variable in your sketch that holds the amount used to increase or decrease the angle on each pass through the loop. The bigger this value the faster the servo will move whan a button is pressed.

Perhaps mem did a good enough explanation... but if you want, here is the code and a video to show how it works...

#include <MegaServo.h>

byte pin = 8;
byte pin2 = 7;
int pos = 90;
MegaServo servo;
void setup(){
 
 pinMode(pin, INPUT);  // initialize pins
  pinMode(pin2, INPUT);
  
  digitalWrite(pin, HIGH); // set internal pull up resistors
  digitalWrite(pin2, HIGH);

servo.attach(4);
}

void loop(){
 
 int val = digitalRead(pin); // get the button values... low is pressed... high is not pressed
int val2 = digitalRead(pin2);

if (val == 0){
  if (pos < 180 && pos > 0){
  
  pos+=2;
} if (pos == 180){
 pos = 178; 
}

if (pos == 0){
 pos = 2; 
}
} 

if (val2 == 0){
 if (pos < 180 && pos > 0){
  
  pos-=2;
} 
  if (pos == 180){
 pos = 178; 
}

if (pos == 0){
 pos = 2; 
}
  
}

servo.write(pos);
delay(15);
  
}

and the vid... :

http://bxsciborgs.com/rus.tech.studio/mypics/DSCN1158.MOV

it takes a while to load... sry.

here is an easier way to do it

#include <MegaServo.h>

const int  incSwitch = 8;
const int  decSwitch = 7;
const int  servoPin = 4;

int angle = 90;
int change = 2; // this value determines how much the angle changes each time through the loop

MegaServo servo;

void setup(){

  pinMode(incSwitch, INPUT);  // initialize pins
  pinMode(decSwitch, INPUT);

  digitalWrite(incSwitch, HIGH); // set internal pull up resistors
  digitalWrite(decSwitch, HIGH);

  servo.attach(servoPin);
}

void loop(){

  if( digitalRead(incSwitch) == LOW) { 
    // here if increment switch pressed
    angle = angle + change;
  }
  if( digitalRead(decSwitch) == LOW) {
    // here if decrement switch pressed
    angle = angle - change;
  }   
  angle = constrain(angle, 0, 180); // limit value of angle 
  servo.write(angle);
  delay(20); 
}

Thanks for bringing this up! I can think of quite a few uses!

I just have one question,
I've always hooked up my buttons using a pull-down resistor, how do you go about hooking up a button with the internal pull-ups on?

I've always hooked up my buttons using a pull-down resistor, how do you go about hooking up a button with the internal pull-ups on?

Connect one side of the button to the Arduino pin and the other to GND. digitalRead will return LOW when the button is pushed.

Awesome, that makes alot of sense!

I have the Radioshack Electronics Learning Lab and it has 4 buttons along with all kinds of other goodies with only 2 pins, so that will make wiring like 42% easier, give or take! :smiley:

Muchos gracias!


thats a pic of my project. im not really using a button im using this as a button.

http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItem&item=250453024870

im using the code posted my mem because i could not get the first one to work. and theres a problem ,,,,,,,,,,, whats new.

i just figed out that when i press the botton on the remote it gos high and stays high till i press it again. so thats not making this ez dont really know what to do now

weird... working for me...

i just figed out that when i press the botton on the remote it gos high and stays high till i press it again.

I have something similar - those decoder chips usually come in two different types, momentary and latching – it looks like you have the latching type and you want the momentary version.

This is the momentary contact version of the chip used in the receiver: http://cgi.ebay.com.my/PT2272-M4-RF-decode-IC-compectable-with-SC2272-M4_W0QQitemZ290334056957QQcmdZViewItemQQptZLH_DefaultDomain_0?hash=item43994221fd&_trksid=p4634.c0.m14.l1262

here is a pic of the board, are you sure that chip is the right one? if you say yes im going to buy it. is there anything i need to know so i dont mess it up ? how do i know which way to install the new one? and thanks for the info.

and i think the first code did not work for me because of this.
but thank you for the code.

If you are not experienced with desoldering ICs then you would have some difficulty getting the old chip out without damageing the board.

I do know the part I linked is the momentary version of the SC2272-L4 but I have not found an English datasheet for the part with the –T4 suffix so don't know for sure if its compatible with the chip you have.

It works!!! take a look

this was kind of an after thought you all mite like

I only messed that servo because it was getting stuck when to turned it all the way to the right so i did not really care if it got messed up. now im trying to come up with a project to use it in ::slight_smile:

Hey thank you for all your help