Go Down

Topic: Control servos with push buttons (Read 995 times) previous topic - next topic

Newbie here but making slow progress.  I've got an Uno and a couple of continuous revolution servos that I'm trying to control.

What I'd like to accomplish is when I press a button, the servo turns clockwise, and when I release it, it stops.  When I press the other button, it would turn counter-clockwise and then stop on button release. 

To simplify it, think taking a window shade and the servo would raise or lower it depending on the button I press.

I've been playing with code that I've found online and I managed to find code that works perfectly for me for 1 button, but I can't figure out how to add a second button.  I tried calling out button 2 and then duplicating some code even to see if I could get both buttons to turn it one way, but I've had no luck.

Here's what I snagged from online right now...

Code: [Select]
#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 2
int button2 = 3;  // Another button will be on Pin 3

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
pinMode(pos, OUTPUT);
pinMode(button, INPUT);
pinMode(button2, INPUT);
digitalWrite (button, LOW);
}

void loop()
{
 
    if (digitalRead(button) == HIGH)

  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) == LOW)
 
  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
  }


}

AWOL

Code: [Select]
// waits 1s for the servo to reach the position
It does nothing of the sort.

Quote
I tried calling out button 2

What does that mean?
"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.

Like I said, it's code that I got from a Google search that did about half of what I wanted it to do.

What I tried to do was add:

Code: [Select]
int button2 = 3;  // Another button will be on Pin 3

Using that I tried to copy/paste a chunk of the code that I thought controlled the servo and then change it so button2 (on pin 3) did the controlling instead of button (on pin 2) but I couldn't make headway of it.

AWOL

It might help us if you posted the code that uses button2
"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.

After further digging and playing with code here's what I came up with which SORTA works like I want, but it seems to be interrupting itself...

Code: [Select]
#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 2
int button2 = 3;  // Another button will be on Pin 3

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
pinMode(pos, OUTPUT);
pinMode(button, INPUT);
pinMode(button2, INPUT);
digitalWrite (button, LOW);
}

void loop()
{
 
    if (digitalRead(button) == HIGH)   // button 1 press
    myservo.write(0);                  // tell servo to turn Clockwise
  if (digitalRead(button) == LOW)      // button 1 release
    myservo.write(90);                 // stop servo
    if (digitalRead(button2) == HIGH)  // button 2 press
    myservo.write(180);                // tell servo to turn counter-clockwise
  if (digitalRead(button2) == LOW)     // button 2 release
    myservo.write(90);                 // stop servo
}


If I press the clockwise button, the servo turns clockwise, but pauses about briefly about every half-second.

If I press the counter-clockwise button, the servo turns counter-clockwise, but it's very jerky.

I'm guessing I don't have a smooth workflow and something is interrupting the PWM signal to the servo.

wildbill

Could be power, could be floating inputs. How do you have this wired up?


Could be power, could be floating inputs. How do you have this wired up?


Right now I have it wired so the Uno is spinning the servo since there's no load on it, but I just tried it with a separate power source (common grounded to the breadboard) and it does the same thing

I've attached a horrible cell phone picture for you to see how it's wired currently.  Sorry for the mess.

zoomkat

You might want to try the below test code that works just by connecting the button pins to ground (I don't have any buttons). Might fix floating pin issues.

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);
  }
 
  /*else {
    servo1.write(90);
  }*/
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

I think I was able to figure it out using an if/else statement.  So far it's working as intended but I will play with it more tomorrow when I have all of my parts together.

Here's the code I came up with...

Code: [Select]
//  CW&CCW using 2 buttons

#include <Servo.h>

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

int button1 = 2; // CW Button
int button2 = 3; // CCW Button


void setup()
{
  myservo.attach(9);                       // attaches the servo on pin 9 to the servo object
  pinMode (button1, INPUT);
  pinMode (button2, INPUT);
}

void loop()
{
  if (digitalRead(button1) == HIGH)        // Watches to see if CW button is pressed
  myservo.write(0);                        // If it is, turn servo CW
  else if (digitalRead(button2) == HIGH)   // If CW button isn't pressed, watch for CCW button
  myservo.write(180);                      // ^^ If CCW button is pressed, turn servo CCW
  else
  myservo.write(90);                       // If no buttons pressed, keep servo quiet
}

Here's my latest code that accomplishes what I was trying to do.  Time to hunt for a project box I guess...

Code: [Select]
//  2 servos CW&CCW using 4 inputs

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2; // create servo object to control a second servo

int button1 = 2; // Upper CW Button (UCW)
int button2 = 3; // Upper CCW Button (UCCW)
int button3 = 4; // Lower CW Button (LCW)
int button4 = 5; // Lower CCW Button (LCCW)

void setup()
{
  myservo.attach(9);                       // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);                     // attaches the servo on pin 10 to the servo object
  pinMode (button1, INPUT);
  pinMode (button2, INPUT);
  pinMode (button3, INPUT);
  pinMode (button4, INPUT);
}

void loop()
{
  if (digitalRead(button1) == HIGH)        // Watches to see if UCW button is pressed
  myservo.write(0);                        // If UCW button is pressed, turn servo CW
  else if (digitalRead(button2) == HIGH)   // Watches to see if UCCW button is pressed
  myservo.write(180);                      // If UCCW button is pressed, turn servo CCW
  else
  myservo.write(90);                       // If no buttons pressed, keep servo quiet
  if (digitalRead(button3) == HIGH)        // Watches to see if LCW button is pressed
  myservo2.write(0);                       // If LCW button is pressed, turn servo2 CW
  else if (digitalRead(button4) == HIGH)   // Watches to see if LCCW button is pressed
  myservo2.write(180);                     // If LCCW button is pressed, turn servo2 CCW
  else
  myservo2.write(90);                      // If no buttons pressed, keep servo2 quiet
}

Go Up