#define BTN_RED 19
#define BTN_GREEN 18
#define BTN_BLUE 5
#define BTN_YELLOW 17
#define LED_RED 25
#define LED_GREEN 26
#define RED 1
#define GREEN 2
#define BLUE 3
#define YELLOW 4
#define PRESSED true
bool btn_red_state = !PRESSED;
bool btn_green_state = !PRESSED;
bool btn_blue_state = !PRESSED;
bool btn_yellow_state = !PRESSED;
const int combination[4] = { RED, GREEN, BLUE, YELLOW };
int input[4] = { 0, 0, 0, 0 };
int input_idx = 0;
void setup_gpios()
{
pinMode(BTN_RED, INPUT_PULLUP);
pinMode(BTN_GREEN, INPUT_PULLUP);
pinMode(BTN_BLUE, INPUT_PULLUP);
pinMode(BTN_YELLOW, INPUT_PULLUP);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
}
void check_btn(int btn, int color, bool *btn_state)
{
if (LOW == digitalRead(btn))
{
*btn_state = PRESSED;
delay(50);
}
else
{
if(PRESSED == *btn_state)
{
input[input_idx] = color;
input_idx= (input_idx+1) % 4;
delay(50);
*btn_state = !PRESSED;
}
}
}
bool is_valid_input()
{
for(int i=0; i < 4; i++)
{
if (input[i] != combination[i])
{
return false;
}
}
return true;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
setup_gpios();
}
void loop() {
// put your main code here, to run repeatedly:
//check_btn(BTN_RED, RED, &btn_red_state);
if (LOW == digitalRead(BTN_RED))
{
btn_red_state = PRESSED;
delay(50);
}
else
{
if(PRESSED == btn_red_state)
{
input[input_idx] = RED;
input_idx= (input_idx+1) % 4;
delay(50);
btn_red_state = !PRESSED;
}
}
check_btn(BTN_GREEN, GREEN, &btn_green_state);
check_btn(BTN_BLUE, BLUE, &btn_blue_state);
check_btn(BTN_YELLOW, YELLOW, &btn_yellow_state);
Serial.print("USER_INPUT:");
for (int i = 0; i < 4; i++)
{
Serial.print(input[i]);
Serial.print(", ");
}
Serial.println("");
if ( input_idx % 4 == 0 )
{
if (is_valid_input() )
{
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_RED, LOW);
}
else
{
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, HIGH);
}
}
else
{
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, LOW);
}
delay(10); // this speeds up the simulation
}