Go Down

Topic: controlling a micro servo with two buttons (Read 1 time) previous topic - next topic

katys866

I am relatively new to ardunio and I am looking to control a micro servo using two buttons. I would like one button to move the servo arm up 10 degrees at a time and the other button to move it down 10 degrees at a time. (Total turn of only 90 degrees)
I can wire up my arduino ok, but I am struggling with the code to make it all work. Any help would be brilliant!

1pacman

have you tried the servo library?
http://arduino.cc/playground/ComponentLib/Servo

or the single servo example?
http://arduino.cc/playground/Learning/SingleServoExample

both should give you a rough idea.

katys866

thanks for your response, I have had a wee look at both, but like I said Im quite new to all this and very unsure how to edit/add code to suit this project, but cant seem to find a tutorial or code that uses the servo and two buttons and was hoping someone may have one already.

1pacman

it depends on how you connect it but it would look like this,

Code: [Select]

#include <Servo.h>
Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position
// constants won't change. They're used here to
// set pin numbers:
const int buttonPinup = 2;     // the number of the pushbutton pin
const int buttonPindown = 3;     // the number of the pushbutton pin
// variables will change:
int buttonStateup = 0;         // variable for reading the pushbutton status up
int buttonStatedown = 0;         // variable for reading the pushbutton status down

void setup()
{
// initialize the pushbutton pins as an inputs:
  pinMode(buttonPinup, INPUT);
  pinMode(buttonPindown, INPUT);     
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}


void loop()
{
// read the state of the pushbutton values:
  buttonStateup = digitalRead(buttonPinup);
  buttonStatedown = digitalRead(buttonPindown);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonStateup == HIGH) pos += 10;  // move 10 deg +   
  if (buttonStatedown == HIGH) pos -= 10;  // move 10 deg -
   
   // tell servo to go to position in variable 'pos' ;
    myservo.write(pos);           
    delay(15);                       // waits 15ms for the servo to reach the position
}


buttons on pins 2 and 3 like this http://arduino.cc/en/Tutorial/Button
servo on 9 like this http://arduino.cc/en/Tutorial/Sweep

I haven't tested as I have no servo but it compiles an its a start?

katys866

Yeah that is a fantastic help, I'll try it. Thanks so much for getting back to me!

1pacman

oh and don't forget in ya main loop above the servo move;

if (pos<90) pos=90; // so only moves 90 deg or what you wanted
if (pos>0) pos=0; // so cant go under 0 deg

ebun91

I need to do this too. I used the code and wired it and everything, but it still won't work. What is the problem?

zoomkat

Some test code from a while back for two buttons and a servo.

Code: [Select]

//zoomkat servo button test 12-29-2011

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

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

ebun91

Oh never mind sorry. It was the servo that was faulty. Replaced it and it works.

Go Up