Hello
Can someone guide me through making a code to control what angle the servo will go when i push a push button. I want it to go to a position and stay there when i push it once, then when i push it again, it will go back to original angle. Im using the push button on a joystick for this. I try it out but i cant seem to get it to work. I've got the push button pin going into the digital 2 pin. What am i doing wrong Thanks a million !!
#include <Servo.h> // include a library that does all the hard work controlling the servo
Servo servo1; // declare servo #1
int gripbtn = 2;
int gripPOS = 0; // intitialize gripper position variable
int buttonState = 0; // Variable for reading the pushbutton status
int directionState = 0;
void setup() {
Serial.begin(9600);
pinMode(gripbtn, INPUT); // let Arduino know that this is an input
servo1.attach(11); // pin #11 for servo 1
}
void loop() {
gripPOS = digitalRead(gripbtn); // digital input for push button
if (buttonState == HIGH) {
directionState = 1;// The direction for the servo is clockwise
// goes from 0 degrees to 180 degrees in steps of 1 degree
for(gripPOS = 0; gripPOS < 180; gripPOS=gripPOS+1)
{
servo1.write(gripPOS); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
else if (directionState == 1) {
// The button is pushed
if (buttonState == HIGH) {
directionState = 0; // The direction for the servo is anti-clockwise
// goes from 180 degrees to 0 degrees in steps of 1 degree
for(gripPOS = 180; gripPOS>=1; gripPOS=gripPOS-1)
{
servo1.write(gripPOS); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
}
}
#include <Servo.h> // include a library that does all the hard work controlling the servo
Servo servo1; // declare servo #1
int gripbtn = 2;
int gripPOS = 0; // intitialize gripper position variable
int buttonState = 0; // Variable for reading the pushbutton status
int directionState = 0;
void setup() {
Serial.begin(9600);
pinMode(gripbtn, INPUT); // let Arduino know that this is an input
servo1.attach(11); // pin #11 for servo 1
}
void loop() {
buttonState = digitalRead(gripbtn); // digital input for push button
if (buttonState == HIGH && directionState == 0) {
directionState = 1;// The direction for the servo is clockwise
// goes from 0 degrees to 180 degrees in steps of 1 degree
for(gripPOS = 0; gripPOS < 180; gripPOS=gripPOS+1)
{
servo1.write(gripPOS); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
buttonState = LOW;
}
if (buttonState == HIGH && directionState == 1) {
// The button is pushed
if (buttonState == HIGH) {
directionState = 0; // The direction for the servo is anti-clockwise
// goes from 180 degrees to 0 degrees in steps of 1 degree
for(gripPOS = 180; gripPOS>=1; gripPOS=gripPOS-1)
{
servo1.write(gripPOS); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
}
}
Made a couple of small changes.
What I did to find the problems was to put Serial.println(gripPOS) after servo1.write(gripPOS) in both for-loops. I only had the button connected. This way I could open serial monitor and watch what was actually going on with the position. Just a tip for next time
Why did you have Serial.begin(9600) by the way? Wouldn't need that.