So I am trying to build a circuit where when I press a button, it activates a servo for a specific amount of time. Right now I have it wired so that when I have the button pressed, it activates but I'd like it so that I can press it just once, and I activates for defined amount of time. Here's the code I have now, I have it set up for two buttons, one for each direction,
#include<Servo.h>
int pos = 0;
Servo servo;
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
servo.attach(9);
}
void loop() {
while (digitalRead(2) == HIGH && pos < 180) {
pos++;
servo.write(pos);
delay(5);
}
while (digitalRead(3) == HIGH && pos > 0) {
pos--;
servo.write(pos);
delay(5);
}
}
Hi,
Welcome to the forum.
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Thanks.. Tom.
You can also get it to "auto increment" when it detects a HIGH signal from the button presses.
Do you want it to auto increment all the way up to 180 after a button press, or up to a certain value.
How much time are we talking?
At the moment - it has been programmed to go from 0 to 180 in less than second.
Thank you for the suggestions everyone. Also I would just like it to go from 0 to 180
Typically servos are instructed to move to a certain position, not for a certain amount of time. If you wanted it to move x degrees in y milliseconds you could add a delay of (y/x milliseconds) after every iteration of your for loop.
ryanf123:
Thank you for the suggestions everyone. Also I would just like it to go from 0 to 180
Then you could do something like this:
#include<Servo.h>
int pos = 0;
Servo servo;
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
servo.attach(9);
}
void loop() {
if(digitalRead(2) == HIGH ) {
servo.write(180);
}
if(digitalRead(3) == HIGH) {
servo.write(0);
}
}