Pages: [1]   Go Down
Author Topic: controlling a micro servo with two buttons  (Read 1199 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
//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);
  }
 
  }

Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25713
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try here
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: