In this project I am attempting to do a few things. When the upper switch is on HIGH the upper button triggers both servos open upon press and then closed upon another press. When the upper switch is on LOW each button should trigger only one servo (upper and lower buttons are tied to servos respectively) and a similar press to open-press to close process should occur. For the most part this is successful, however I seem to be having issues with the button states bouncing. I have tried to include a debounce delay however there seems to be no delay value in between bouncing, and taking way too long to trigger. Additionally, there seems to be cases where the button input is just entirely missed?
Thanks in advance!
#include <Servo.h>
Servo servo1;
Servo servo2;
int buttonState1 = 0; // the current state of the button
int lastbuttonState1 = 0; // the previous state of the button
int buttonState2 = 0;
int lastbuttonState2 =0;
int switch_state;
const int switch_pin = 4; // switch outlet
const int buttonPin = 2; // input number connected to sensor №1
const int buttonPin2 = 3; // input number connected to sensor №2
const int servo1_pin = 8; // the output number for the servo drive №1
const int servo2_pin = 9; // the output number for the servo drive №2
const int servo_angle = 94; // the angle at which the servo will turn
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(switch_pin, INPUT);
Serial.begin(115200);
servo1.attach(servo1_pin);
servo2.attach(servo2_pin);
}
void loop()
{
switch_state = digitalRead(switch_pin);
if (switch_state == HIGH)
{
Serial.print("BOTH EYES MODE ");
// read the state of the button
buttonState1 = digitalRead(buttonPin);
// check if the button was just pressed
if (buttonState1 != lastbuttonState1) {
// debounce delay
delay(50);
if (lastbuttonState1 == LOW && buttonState1 == HIGH) {
Serial.print("Closing");
servo1.write(-servo_angle);
servo2.write(-servo_angle);
delay(140);
}
else {
Serial.print("Opening");
servo1.write(servo_angle);
servo2.write(servo_angle);
delay(140);
}
// save the current button state for comparison in the next loop
lastbuttonState1 = buttonState1;
}
}
else {
Serial.print("SINGLE EYE MODE");
buttonState1 = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
// check if the button was just pressed
if (buttonState1 != lastbuttonState1) {
// debounce delay
delay(50);
if (lastbuttonState1 == LOW && buttonState1 == HIGH) {
Serial.print("Closing 1");
servo1.write(-servo_angle);
delay(140);
}
else {
Serial.print("Opening 1");
servo1.write(servo_angle);
delay(140);
}
// save the current button state for comparison in the next loop
lastbuttonState1 = buttonState1;
delay(20);
}
// check if the button was just pressed
if (buttonState2 != lastbuttonState2) {
// debounce delay
delay(50);
if (lastbuttonState2 == LOW && buttonState2 == HIGH) {
Serial.print("Closing 2");
servo2.write(-servo_angle);
delay(140);
}
else {
Serial.print("Opening 2");
servo2.write(servo_angle);
delay(140);
}
// save the current button state for comparison in the next loop
lastbuttonState2 = buttonState2;
delay(20);
}
}
}
