Hello everyone, I'm hoping for some feedback on my code that doesnt seem to want to work for me, though that may be down to my circuitry work as well. My goal is to have a pushbutton function with multiple different outcomes depending on how many times the button has been pressed and how long it has been since the button has been pushed. The first push turns three servo motors, begins a time counter, and begins a pushbutton counter. That sets the pushbutton counter to "1." Pushing the button again has two different conditions;
Condition 1: less than 2 minutes has elapsed since the previous push, thus causing two more servos to turn and the pushbutton counter to go up by two counts, thus now at "3;"
Condition 2: over 2 minutes has elapsed since the previous push and the pushbutton counter increases by one without the button being pushed, setting it now at "2," and the next push (however long after the timer passes the 2 minute mark) causes the two aforementioned servos to turn, the pushbutton counter to increase by one, and have it set to "3."
Then, a final button push will increase the pushbutton counter by one to "4," all five servos move back into original position in sequence from last to first, and the code resets the pushbutton counter, timer, and loop back to the beginning.
Here is my code as follows:
#include <Servo.h>
//These constants won't change:
const int buttonPin = 2; // the number of the push button pin
const int eyeplatePin = 3;
const int crplatePin = 5;
const int clplatePin = 6;
const int jrplatePin = 9;
const int jlplatePin = 10;
//Variables will change:
Servo eyeplate; //initialize the various individuals servos and set beginning state to zero:
int eyepos = 0;
Servo cheekright;
int crpos = 0;
Servo cheekleft;
int clpos = 0;
Servo jawright;
int jrpos = 0;
Servo jawleft;
int jlpos = 0;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
long lastPressedTime = 0;
long sweepDelay = 120000;
void setup() {
//initialize button pin as input:
pinMode(buttonPin, INPUT);
//initialize servo pins as outputs:
pinMode(eyeplatePin, OUTPUT);
pinMode(crplatePin, OUTPUT);
pinMode(clplatePin, OUTPUT);
pinMode(jrplatePin, OUTPUT);
pinMode(jlplatePin, OUTPUT);
eyeplate.attach(3); //attach the servos to their pins
cheekright.attach(5);
cheekleft.attach(6);
jawright.attach(9);
jawleft.attach(10);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if state has changed, increment the counter
if (buttonState == HIGH) {
// if current state is HIGH then the button went from off to on:
buttonPushCounter++;
lastPressedTime = millis();
// also sweep the eyeplate open to a set angle position:
eyeplate.write(45);
delay(300);
cheekright.write(45);
cheekleft.write(-45);
}
lastButtonState = buttonState;
delay(50);
}
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
if ((millis() - lastPressedTime) < sweepDelay) {
buttonPushCounter;
buttonPushCounter;
jawright.write(45);
jawleft.write(45);
}
else ((millis() - lastPressedTime) > sweepDelay); {
buttonPushCounter++;
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
jawright.write(45);
jawleft.write(45);
}
lastButtonState = buttonState;
delay(50);
}
}
}
}
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
jawright.write(0);
jawleft.write(0);
delay(300);
cheekright.write(0);
cheekleft.write(0);
delay(300);
eyeplate.write(0);
}
}
}
I hope to hear back from somebody soon, thank you!
P.S. I am a super beginner so actual lines of code to swap into mine rather than verbal instructions is much appreciated :).