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