Hello friends!
I am working on a "useless box" project. The silly little toy that when switched on, switches itself off. I am trying to use a switch case to cycle through the switch counts to change the behavior of the arm that flips the off switch. The issue I am having is that, despite running case 4: "simpleClose();" (the 4th time the switch is powered on), it runs "delayClose();" again. I have the serial outputting the variable of the current switch count, so I can track it, and it shows that selectedMove is equal to 4.
case 5 and default both trigger properly.
I feel like it's something simple that I am missing. Any guidance would be appreciated!
Thanks!
#include <JC_Button.h>
#include <Servo.h>
const byte
BUTTON_PIN(8);
Button myBtn(BUTTON_PIN);
int selectedMove = 0;
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
// variable to store the servo position
long timeDelay;
void setup()
{
Serial.begin(57600);
myservo.attach(2); // attaches the servo on pin 2 to the servo object
myservo.write(180);
myBtn.begin();
}
void loop()
{
myBtn.read();
static uint8_t selectedMove = 0;
if (selectedMove > 9) {
selectedMove = 1;
}
switch (selectedMove)
{
case 1:
simpleClose();
Serial.print("Move number: ");
Serial.println(selectedMove);
break;
case 2:
simpleClose();
Serial.print("Move number: ");
Serial.println(selectedMove);
break;
case 3:
delayClose();
Serial.print("Move number: ");
Serial.println(selectedMove);
break;
case 4:
simpleClose();
Serial.print("Move number: ");
Serial.println(selectedMove);
break;
case 5:
pikaBoo();
Serial.print("Move number: ");
Serial.println(selectedMove);
break;
default:
selectedMove = 0;
Serial.print("Move number: ");
Serial.println(selectedMove);
myservo.write(180);
break;
}
if (myBtn.wasPressed()) {
selectedMove += 1;
}
}
// MOVES
void simpleClose()
{
if (myBtn.isPressed()) {
myservo.write(10);
delay(15);
} else {
if (myBtn.wasReleased()) {
timeDelay = random(1, 4);
myservo.write(180);
delay(15);
}
}
}
void delayClose()
{
if (myBtn.isPressed()) {
myservo.write(90);
delay(4500);
myservo.write(10);
delay(200);
} else {
if (myBtn.wasReleased()) {
timeDelay = random(1, 4);
myservo.write(180);
delay(15);
}
}
}
void pikaBoo()
{
if (myBtn.isPressed()) {
myservo.write(90);
delay(4500);
myservo.write(180);
delay(2000);
myservo.write(10);
delay(300);
} else {
if (myBtn.wasReleased()) {
timeDelay = random(1, 4);
myservo.write(180);
delay(15);
}
}
}