Long, long time lurker and I have learned so much from everyone but I'm a total and abject noob working on his first servo project. I'm trying to get a servo to move to X position when a button is pressed...hold position for 5 seconds and move back to start position. Would someone (or several) like to provide some help in debugging my code and/or pointing me to other threads? Not looking for a handout here.
#include <Servo.h>
const int buttonPin = 8;
const int servoPin = 9;
int buttonState = 0;
int directionState = 0;
Servo servoOne;
int pos = 0;
I think you code can be simplified like below by using this library.
#include <ezButton.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
ezButton button(7); // create Button object that attach to pin 7;
int pos_start = 0;
int pos_end = 180;
void setup() {
button.setDebounceTime(50); // set debounce time to 50 milliseconds
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
button.loop(); // MUST call the loop() function first
if(button.isPressed()){
myservo.write(pos_end);
delay(5000); // hold position for 5 seconds
myservo.write(pos_start);
}
}