#include "esp_timer.h"
#include "driver/gpio.h"
const uint8_t buttonPins[] = {4, 16, 12, 14};
const uint8_t ledPins[] = {0, 17, 13, 27};
const int POSITIONS_QUANTITY = 4;
int buttonPressed = -1;
int currentPos = 0;
// This is the time player has to punch the chosen position
int waitTime = 2;
bool usePattern = true;
int pattern[] = {0, 1, 2};
int currentPatternPos = 0;
void IRAM_ATTR pushButton0() {
buttonPressed = 0;
}
void IRAM_ATTR pushButton1() {
buttonPressed = 1;
}
void IRAM_ATTR pushButton2() {
buttonPressed = 2;
}
void IRAM_ATTR pushButton3() {
buttonPressed = 3;
}
// Return a random position for the user to push
int randomPosition() {
int buf = 0;
esp_fill_random(&buf, sizeof(buf));
int pos = buf % POSITIONS_QUANTITY;
if (pos < 0) {
pos = pos * -1;
}
return (pos);
}
void blinkRandom() {
currentPos = randomPosition();
digitalWrite(ledPins[currentPos], HIGH);
}
void blink() {
digitalWrite(ledPins[pattern[currentPatternPos]], HIGH);
int patternLength = sizeof(pattern) / sizeof(int);
currentPatternPos++;
if (currentPatternPos >= patternLength) {
currentPatternPos = 0;
}
}
void resetLeds() {
for (int i = 0; i < POSITIONS_QUANTITY; i++) {
digitalWrite(ledPins[i], LOW);
}
}
void setup()
{
Serial.begin(9600);
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
attachInterrupt(buttonPins[0], pushButton0, RISING);
attachInterrupt(buttonPins[1], pushButton1, RISING);
attachInterrupt(buttonPins[2], pushButton2, RISING);
attachInterrupt(buttonPins[3], pushButton3, RISING);
}
void loop() {
if (!usePattern) {
blinkRandom();
delay(waitTime * 1000);
resetLeds();
} else {
blink();
delay(waitTime * 1000);
resetLeds();
}
if (buttonPressed == currentPos) {
Serial.print("Correct");
} else {
Serial.print("Incorrect");
}
}