so the idea behind this project is i have a servo motor with one button what im trying to do is the motor starts at 0 deg you hit the button once the servo goes to 180 deg you hit that same button again the motor goes back to 0 deg. The problem that im having is that when i hit the button it goes to 180 and goes right back to 0. I have tried deboucing the button and that did not work i have tried all kind of ways i will post the most recent thing i have tried. anyone feel free to chime in see if we can crack this thing.
#include <Servo.h>
#include <ezButton.h>
// constants won't change
const int BUTTON_PIN = 5; // Arduino pin connected to button's pin
const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin
ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 7;
Servo servo; // create servo object to control a servo
// variables will change:
int angle = 0; // the current angle of servo motor
void setup() {
Serial.begin(9600); // initialize serial
button.setDebounceTime(50); // set debounce time to 50 milliseconds
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(angle);
}
void loop() {
button.loop(); // MUST call the loop() function first
if(button.isPressed()) {
Serial.println("The button is pressed");
// change angle of servo motor
if(angle == 0)
angle = 180;
else
if(angle == 180)
angle = 0;
// control servo motor arccoding to the angle
servo.write(angle);
}
}
@nick_d121 Why do users choose to ignore the advice on posting code ?
The easier you make it to read and copy your code the more likely it is that you will get help
Please follow the advice given in the link below when posting code , use code tags and post the code here
As to your problem, you need to detect when the button becomes pressed rather than when it is pressed. I imagine the the ezButton has such a function or look at the StateChangeDetection example in the IDE to see how to do it without a library
Each time you detect that the button has become pressed move the servo to the opposite angle
thank you for your feed back i will keep that in mind next time, so i have looked through the stateChangeDetection example from that i was able to make a program where it would print lets say up and down for example to see that it was registering that the button was doing different things depending on the state it was pushed in. Still, the servo wants to go back to 0 deg when it moves to 180 and for example it will run through the up state over and over again.
#include <Servo.h>
int buttonState = 0;
int lastButtonState = 0;
int buttonPin = 7;
int buttonPushCounter = 0;
int directionState = 0;
int servoPin = 9;
int angle = 0;
Servo servo;
void setup() {
pinMode(buttonPin, INPUT);
servo.attach(servoPin);
servo.write(angle);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState){
if (buttonState == HIGH){
// if the current state is HIGH then the button went from off to on:
Serial.println("on");
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
buttonPushCounter++;
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
if (directionState == 0 && buttonPushCounter == 1){
Serial.println("going up");
servo.write(180);
directionState = 1;
}
else if (directionState == 1 && buttonPushCounter == 1){
Serial.println("going down");
servo.write(0);
directionState = 0;
}
}
// Delay a little bit to avoid bouncing
delay(200);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
buttonPushCounter = 0;
}
so unfortunately with the servo that didnt work either like i said when doing that i can get it to print up and down as well when servo is not active but when the servo code is active its seems like its not completing the loop
#include <Servo.h>
int buttonState = 0;
int lastButtonState = 0;
int buttonPin = 7;
int buttonPushCounter = 0;
int directionState = 0;
int servoPin = 9;
int angle = 0;
Servo servo;
void setup() {
pinMode(buttonPin, INPUT);
servo.attach(servoPin);
servo.write(angle);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState){
if (buttonState == HIGH){
// if the current state is HIGH then the button went from off to on:
Serial.println("on");
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
buttonPushCounter++;
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
if ( buttonPushCounter % 2){
Serial.println("going up");
servo.write(180);
//directionState = 1;
//delay(3000);
Serial.println(servo.read());
}
else {
Serial.println("going down");
servo.write(0);
//directionState = 0;
//delay(3000);
Serial.println(servo.read());
}
}
// Delay a little bit to avoid bouncing
delay(200);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
//buttonPushCounter = 0;
//Serial.println("end of loop");
}
so its a mini push button switch on one side I have connect to 3.3v and the other post is connected to ground with a 5k ohm resistor and on the other side of the ground I have it going to the input
Here is your code slightly rearranged
Note that the pin numbers have been changed because I tested it on my test setup and that I used INPUT_PULLUP to avoid the need to use external pullup/pulldown resistors
The main difference from your code is that the test for direction and writing to the servo are outside of the state change detection test. this results in the servo being written to continuously but there is no harm in that but it could be changed if it offends you