so i was hoping that someone could help me with a bit of code. the project i am working on requires me to have 2 servos, that when a button is pushed, go in opposite directions. this is my code so far. however i cant seem to get another servo to work with it, any help would be greatly appreciated
/*
State change detection (edge detection)
Often, you don't need to know the state of a digital input all the time,
but you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON. This is called
state change detection, or edge detection.
This example shows how to use button state change to perform an action.
The circuit:
* Momentary pushbutton attached to digital pin 2 from +5V
* 10K resistor attached to digital pin 2 from ground
* Servo signal wire attached to digital pin 9
* Servo power wire connected to 5v
* Servo ground wire connected to ground
Based off the public domain "State Change Detection" example provided with the
Arduino software. Code edited 8-14-2011 by Will Lyon (SXRguyinMA) to remove
the LED and add in a servo.
For the purpose of
"Integrating Electronics Into Your Next Proect" series of articles on TBCS
www.thebestcasescenario.com
*/
// Include the servo library
#include <VarSpeedServo.h>
// Create a servo object to control a servo
VarSpeedServo myservo;
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// attach the Servo to pin 9
myservo.attach(9);
// make sure the servo starts off with the door closed
myservo.slowmove(180, 0);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the pushbutton input pin:
int buttonState = digitalRead(buttonPin);
// 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
// wend from off to on:
buttonPushCounter++;
}
else {
}
// save the current state as the last state,
//for next time through the loop
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) {
myservo.slowmove(0, 180);
} else {
myservo.slowmove(180, 180);
}
}
Moving two servos in opposite directions using the Servo library is not a problem. Moving two servos in opposite directions using the VarSpeedServo library is.
You can emulate what VarSpeedServo is doing by using small intervals between small steps.
I fail to see how the else block is accomplishing anything. Generally, when you struggle to figure out what to put in the else block, it's a sure sign that no else block is needed.
i agree with you that the var speed servo probably wasn't the best idea. but it'd be a shame to waste the code if i could easily fix it. i tried adding a second servo to the code last night, and it didnt work.
this was my Finished code when i was done
// Include the servo library
#include <VarSpeedServo.h>
// Create a servo object to control a servo
VarSpeedServo myservo;
VarSpeedServo myservo2;
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
myservo.attach(9);
myservo2.attach(11);
myservo.slowmove(180, 0);
myservo2.slowmove(0, 180);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the pushbutton input pin:
int buttonState = digitalRead(buttonPin);
// 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
// wend from off to on:
buttonPushCounter++;
}
else {
}
// save the current state as the last state,
//for next time through the loop
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) {
myservo.slowmove(0, 180);
myservo2.slowmove(180, 0);
} else {
myservo.slowmove(180, 180);
myservo2.slowmove(180, 180);
}
}
if it doesnt look like this will work for me, i have no problem scrapping it, i just appreciate the help
The code does something. You expect it to do something. "It didn't work" says that the two somethings are not the same thing, but doesn't say what either one is.
VarSpeedServo myservo;
VarSpeedServo myservo2;
How can you have #2 without having #1? A pet peeve of mine...
No, but I like names that mean something. If adding a 2 to the end of a name means something, and adding a 3 for the next one, then adding a 1 for the first one makes sense, too.
Of course, names like leftServo and rightServo might make sense, too, and make it more difficult to modify the wrong instance.
ahh i gotcha, well other then that, any ideas why the second one wont work, too me the code seems like it should run the second servo, but i get nothing from it when i push the button
alright, i unplugged the servo 1 from pin 9 and plugged it into pin 11 and i got nothing. i also went into the code and commented out anything that had to do with servo 1. and i still got no response from servo 2 when the button was pressed
openroad12:
your code works great thanks!
i tried adding a second servo to it,
//zoomkat servo button toggle test 4-28-2012
#include <Servo.h>
int button = 2; //button pin, connect to ground to move servo
int press = 0;
Servo servo1;
Servo servo2;
boolean toggle = true;
void setup()
{
pinMode(button, INPUT); //arduino monitor pin state
servo1.attach(9); //pin for servo control signal
servo2.attach(10); //pin for servo control signal
digitalWrite(2, HIGH); //enable pullups to make pin high
}