Go Down

Topic: Controlling two servos with push button (Read 922 times) previous topic - next topic

TattooMike

Hello,

I am very new to Arduino and trying to get a small project done. I am attempting to control two servos with a single push button. On every press of the button I want the servos to rotate 90 degrees in opposite directions. It seems to be working ok except that the left servo makes a vibrating noise at one of the positions (180). Any help would be greatly appreciated.

Here is the code I am using.

Code: [Select]
#include <Servo.h>

Servo leftservo;  // create left servo object
Servo rightservo;  // create right servo object
// this constant won't change:
const int  buttonPin = 2;    // pushbutton attached to pin 2
// Variables will change:
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int buttonPushCounter = 0;   // counter for the number of button presses
int centerPos = 90;
boolean currentButton = LOW;

void setup() {
 
   leftservo.attach(9); // left servo attached to pin 9
   rightservo.attach(10); //right ervo attached to pin 10
  // tell both servos to go to center position
  leftservo.write(centerPos);
  rightservo.write(centerPos);
 
  delay(15);
 
  pinMode(buttonPin, INPUT); // initialize the button pin as a input:
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(buttonPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(buttonPin);
  }
  return current;
}


void loop() {
 
  buttonState = debounce(buttonPin); // read the pushbutton input pin:
 
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;
    }
    else {
    }

    // save the current state as the last state,
    lastButtonState = buttonState;
  }
 
  /* moves the servo every other button push by
  checking the modulo of the button push counter.
   the modulo function gives you the remainder of
   the division of two numbers: */
  if (buttonPushCounter % 2 == 0) {
  //move to position 90 
   rightservo.write(centerPos);
   leftservo.write(centerPos);
   delay(15);
  } else {
  //OR: move back to position 0 
   leftservo.write(0); //move left servo 90 degrees ccw from center
   rightservo.write(180); // move right servo 90 cw from center
   delay(15);
  }
}

Drew Davis

And what is wrong with it making a vibrating noise? If that is your only problem you don't really have any problems!

zoomkat

Quote
It seems to be working ok except that the left servo makes a vibrating noise at one of the positions (180).


The servo might be against the internal hard stop, which is probably not good for the servo. Change the 180 to a lower value and see if the vibrating stops.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Scodasso


Hello,

I am very new to Arduino and trying to get a small project done. I am attempting to control two servos with a single push button. On every press of the button I want the servos to rotate 90 degrees in opposite directions. It seems to be working ok except that the left servo makes a vibrating noise at one of the positions (180). Any help would be greatly appreciated.

Here is the code I am using.

Code: [Select]
#include <Servo.h>

Servo leftservo;  // create left servo object
Servo rightservo;  // create right servo object
// this constant won't change:
const int  buttonPin = 2;    // pushbutton attached to pin 2
// Variables will change:
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int buttonPushCounter = 0;   // counter for the number of button presses
int centerPos = 90;
boolean currentButton = LOW;

void setup() {
 
   leftservo.attach(9); // left servo attached to pin 9
   rightservo.attach(10); //right ervo attached to pin 10
  // tell both servos to go to center position
  leftservo.write(centerPos);
  rightservo.write(centerPos);
 
  delay(15);
 
  pinMode(buttonPin, INPUT); // initialize the button pin as a input:
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(buttonPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(buttonPin);
  }
  return current;
}


void loop() {
 
  buttonState = debounce(buttonPin); // read the pushbutton input pin:
 
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;
    }
    else {
    }

    // save the current state as the last state,
    lastButtonState = buttonState;
  }
 
  /* moves the servo every other button push by
  checking the modulo of the button push counter.
   the modulo function gives you the remainder of
   the division of two numbers: */
  if (buttonPushCounter % 2 == 0) {
  //move to position 90 
   rightservo.write(centerPos);
   leftservo.write(centerPos);
   delay(15);
  } else {
  //OR: move back to position 0 
   leftservo.write(0); //move left servo 90 degrees ccw from center
   rightservo.write(180); // move right servo 90 cw from center
   delay(15);
  }
}



Thank you for your code tatoo mike with the nice suggestion of zoomkat, it works perfectly!
I'm using it for a project, i also add one more servo and it works.
But i'm trying to add a new pushbutton to use it for move the servos to different standard position, do you have any suggestion on how to do it?

zoomkat

Servo toggle test code.

Code: [Select]

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
Servo servo1;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  servo1.attach(8); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      servo1.write(20);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      servo1.write(160);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

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

Sand_HK

I had a very similar issue.
http://forum.arduino.cc//index.php?topic=174901.msg1298277#msg1298277

I found that the servos I had; all cheap Tower Pro 9g's, had different ranges (not all had 180 degrees) and if I tried to push them too far they started stressing out, knocking/vibrating.

Using Servo.writeMicroseconds() instead of Servo.Write(degrees) allowed me better control. I just had to work out what degree was achieved by the different microsecond values.







Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy