Created
August 20, 2021 01:06
-
-
Save dpliakos/22aa55311dc569b6a6cec9522fffae25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define DEBUG false | |
# define DEBUG_BTN false | |
# define DEBUG_STATE false | |
# define DEBUG_BTN_STATUS false | |
# define BTN_LEFT 0 | |
# define BTN_RIGHT 1 | |
# define LED_LEFT 2 | |
# define LED_RIGHT 3k | |
# define BTN_PRESSED HIGH | |
# define BTN_IDLE LOW | |
# define LED_SPEED 200 | |
# define DEBOUNCE_DELAY 5 | |
int btnLeft = BTN_IDLE; | |
int btnRight = BTN_IDLE; | |
int ledStatusLeft = LOW; | |
int ledStatusRight = LOW; | |
int currentState = 0; | |
int previousState = -1; | |
int ledState = 0; | |
int ledStatePrev = -1; | |
unsigned int t0_led; | |
unsigned int t1_led; | |
unsigned int t0_left; | |
unsigned int t0_right; | |
unsigned int t1_left; | |
unsigned int t1_right; | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(BTN_LEFT, INPUT); | |
pinMode(BTN_RIGHT, INPUT); | |
pinMode(LED_LEFT, OUTPUT); | |
pinMode(LED_RIGHT, OUTPUT); | |
# if DEBUG == true | |
Serial.begin(9600); | |
#endif | |
} | |
void flash(int direction) { | |
switch(direction) { | |
case -1: | |
ledStatusLeft = HIGH; | |
ledStatusRight = HIGH; | |
break; | |
case 0: | |
ledStatusLeft = LOW; | |
ledStatusRight = LOW; | |
break; | |
case 1: | |
ledStatusLeft = HIGH; | |
ledStatusRight = LOW; | |
break; | |
case 2: | |
ledStatusLeft = LOW; | |
ledStatusRight = HIGH; | |
break; | |
default: | |
ledStatusLeft = LOW; | |
ledStatusRight = LOW; | |
} | |
} | |
void handleButtonState() { | |
# if DEBUG && DEBUG_STATE | |
if (previousState != currentState) { | |
Serial.print("Transitioning from "); | |
Serial.print(previousState); | |
Serial.print(" to "); | |
Serial.println(currentState); | |
} | |
# endif | |
# if DEBUG_STATE | |
previousState = currentState; | |
# endif | |
switch(currentState) { | |
case 0: // reset | |
currentState = 1; | |
break; | |
case 1: // wait | |
btnLeft = digitalRead(BTN_LEFT); | |
btnRight = digitalRead(BTN_RIGHT); | |
if (btnLeft == BTN_PRESSED) { | |
# if DEBUG && DEBUG_BTN_STATUS | |
Serial.println("Button presse left"); | |
# endif | |
currentState = 2; | |
} else if (btnRight == BTN_PRESSED) { | |
# if DEBUG && DEBUG_BTN_STATUS | |
Serial.println("Button press right"); | |
# endif | |
currentState = 3; | |
} | |
break; | |
case 2: // arming left | |
t0_left = millis(); | |
currentState = 4; | |
break; | |
case 3: //arming right | |
t0_right = millis(); | |
currentState = 5; | |
break; | |
case 4: // armed left | |
btnLeft = digitalRead(BTN_LEFT); | |
t1_left = millis(); | |
if (t1_left - t0_left > DEBOUNCE_DELAY) { | |
# if DEBUG && DEBUG_BTN_STATUS | |
Serial.println("Button press left confirmed"); | |
# endif | |
currentState = 6; | |
} | |
if (btnLeft == BTN_IDLE ) { | |
# if DEBUG && DEBUG_BTN_STATUS | |
Serial.println("Button press left abort"); | |
# endif | |
currentState = 0; | |
} | |
break; | |
case 5: // armed right | |
btnRight = digitalRead(BTN_RIGHT); | |
t1_right = millis(); | |
if (t1_right - t0_right > DEBOUNCE_DELAY) { | |
# if DEBUG && DEBUG_BTN_STATUS | |
Serial.println("Button press right confirmed"); | |
# endif | |
currentState = 7; | |
} | |
if (btnRight == BTN_IDLE) { | |
# if DEBUG && DEBUG_BTN_STATUS | |
Serial.println("Button press right abort"); | |
# endif | |
currentState = 0; | |
} | |
break; | |
case 6: | |
if (ledStatusLeft == HIGH) { | |
flash(0); | |
} else { | |
flash(1); | |
} | |
currentState = 8; | |
break; | |
case 7: | |
if (ledStatusRight == HIGH) { | |
flash(0); | |
} else { | |
flash(2); | |
} | |
currentState = 9; | |
break; | |
case 8: // hold left | |
btnLeft = digitalRead(BTN_LEFT); | |
if (btnLeft == BTN_IDLE) { | |
currentState = 0; | |
} | |
break; | |
case 9: // hold right | |
btnRight = digitalRead(BTN_RIGHT); | |
if (btnRight == BTN_IDLE) { | |
currentState = 0; | |
} | |
break; | |
default: | |
# if DEBUG | |
Serial.println("/!\\ Unknown state /!\\"); | |
# endif | |
break; | |
} | |
} | |
void handleLedState() { | |
ledStatePrev = ledState; | |
switch(ledState) { | |
case 0: // idle | |
t0_led = millis(); | |
digitalWrite(LED_LEFT, LOW); | |
digitalWrite(LED_RIGHT, LOW); | |
ledState = 1; | |
break; | |
case 1: // wait idle | |
t1_led = millis(); | |
if (t1_led - t0_led > LED_SPEED) { | |
ledState = 2; | |
} | |
break; | |
case 2: // open | |
t0_led = millis(); | |
digitalWrite(LED_LEFT, ledStatusLeft); | |
digitalWrite(LED_RIGHT, ledStatusRight); | |
ledState = 3; | |
break; | |
case 3: // wait open | |
t1_led = millis(); | |
if (t1_led - t0_led > LED_SPEED) { | |
ledState = 0; | |
} | |
break; | |
} | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
#if DEBUG && DEBUG_BTN | |
Serial.print("Button LEFT: "); | |
Serial.print(digitalRead(BTN_LEFT)); | |
Serial.print("Button RIGHT: "); | |
Serial.println(digitalRead(BTN_RIGHT)); | |
# endif | |
handleButtonState(); | |
handleLedState(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment