const int unlock_button = 2;
const int lock_button = 3;
const int red_led = 5;
const int yellow_led = 6;
const int green_led = 7;
//secondary approach was to convert int/str into list to ez convert
const int a_seconds = 3;
const int b_hertz = 6;
const int c_seconds = 5;
const int d_seconds = 3;
const int e_hertz = 5;
const int f_seconds = 8;
const int g_hertz = 8;
const int h_seconds = 6; //temp for testing SHOULD be 0
unsigned long state_start = 0;
unsigned long last_toggle = 0;
int flash_count = 0;
int flash_target = 0;
bool led_state = false;
unsigned long last_report = 0;
const unsigned long report_interval = 1000;
//report states
String lock_state = "None";
String last_button_pressed = "None";
String status_light = "None";
enum States {S1, S2, S3, S4, S5, S6, S7};
States current_state = S1;
void interrupt_check() {
if (current_state != S1 && current_state != S6 && current_state != S7) {
if (digitalRead(unlock_button) == HIGH) {
last_button_pressed = "Unlock";
current_state = S1;
lock_state = "Unlocked";
status_light = "Green";
reset();
state_start = millis();
}
}
}
void report(int state, String lock_state, String last_button_pressed, String current_status_light) {
Serial.print("State: ");
Serial.print(state);
Serial.print(" | System: ");
Serial.print(lock_state);
Serial.print(" | Last Button: ");
Serial.print(last_button_pressed);
Serial.print(" | Status Light: ");
Serial.println(current_status_light);
}
void reset() {
digitalWrite(red_led, LOW);
digitalWrite(yellow_led, LOW);
digitalWrite(green_led, LOW);
}
void state_1() {
reset();
digitalWrite(green_led, HIGH);
status_light = "Green";
lock_state = "Unlocked";
if (digitalRead(unlock_button) == HIGH) {
last_button_pressed = "Unlock";
}
}
//initializes next state before moving on
void state_2() {
if (millis() - state_start >= a_seconds * 1000) { //1000 to convert s to ms
current_state = S3;
state_start = millis();
flash_count = 0;
flash_target = c_seconds * b_hertz * 2;
last_toggle = millis();
led_state = false;
}
}
void state_3() {
unsigned long interval = 500 / b_hertz; //500 numerator since half period to flash
if (flash_count < flash_target) {
if (millis() - last_toggle >= interval) {
last_toggle = millis();
led_state = !led_state;
digitalWrite(green_led, led_state ? HIGH : LOW);
flash_count++;
}
} else {
current_state = S4;
state_start = millis();
}
}
void state_4() {
reset();
digitalWrite(yellow_led, HIGH);
status_light = "Yellow";
if (millis() - state_start >= d_seconds * 1000) {
current_state = S5;
state_start = millis();
flash_count = 0;
flash_target = f_seconds * e_hertz * 2;
last_toggle = millis();
led_state = false;
}
}
void state_5() {
unsigned long interval = 500 / e_hertz;
if (flash_count < flash_target) {
if (millis() - last_toggle >= interval) {
last_toggle = millis();
led_state = !led_state;
digitalWrite(yellow_led, led_state ? HIGH : LOW);
flash_count++;
}
} else {
current_state = S6;
}
}
void state_6() {
reset();
digitalWrite(red_led, HIGH);
status_light = "Red";
lock_state = "Locked";
if (digitalRead(lock_button) == HIGH) {
last_button_pressed = "Lock";
}
}
void state_7() {
if (digitalRead(lock_button) == HIGH) {
last_button_pressed = "Lock";
}
unsigned long interval = 500 / g_hertz;
if (flash_count < flash_target) {
if (millis() - last_toggle >= interval) {
last_toggle = millis();
led_state = !led_state;
digitalWrite(red_led, led_state ? HIGH : LOW);
flash_count++;
}
} else {
current_state = S1;
}
}
void setup() {
Serial.begin(9600);
pinMode(unlock_button, INPUT);
pinMode(lock_button, INPUT);
pinMode(red_led, OUTPUT);
pinMode(yellow_led, OUTPUT);
pinMode(green_led, OUTPUT);
state_start = millis();
}
void loop() {
interrupt_check();
switch (current_state) {
case S1:
state_1();
if (digitalRead(lock_button) == HIGH) {
last_button_pressed = "Lock";
current_state = S2;
state_start = millis();
}
break;
case S2:
state_2();
break;
case S3:
state_3();
break;
case S4:
state_4();
break;
case S5:
state_5();
break;
case S6:
state_6();
if (digitalRead(unlock_button) == HIGH) {
last_button_pressed = "Unlock";
current_state = S7;
state_start = millis();
flash_count = 0;
flash_target = h_seconds * g_hertz * 2;
last_toggle = millis();
led_state = false;
}
break;
case S7:
state_7();
break;
default:
Serial.println("reached default state. Reverting to S1");
current_state = S1;
}
if (millis() - last_report >= report_interval) {
last_report = millis();
report((current_state + 1), lock_state, last_button_pressed, status_light); //need to + 1 current state cause starts at 0
}
}