const int numLights = 9; // number of lights in the circle
const int lightPins[numLights] = {13, 12, 11, 10, 9, 8, 7, 6, 5}; // pins for each light
const int buttonPin = 4; // pin for button input
int currentLight = 0; // current position of the lit light
bool buttonReleased = true; // flag to track button release
void setup() {
// set each light pin as an output
for (int i = 0; i < numLights; i++) {
pinMode(lightPins[i], OUTPUT);
}
// set button pin as an input
pinMode(buttonPin, INPUT);
Serial.begin(9600);
Serial.println("Starting game");
}
void loop() {
// turn off all lights
for (int i = 0; i < numLights; i++) {
digitalWrite(lightPins[i], LOW);
}
// turn on current light
digitalWrite(lightPins[currentLight], HIGH);
// check if button is pressed
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && buttonReleased) {
// button was pressed, set flag to false
buttonReleased = false;
if (currentLight == 5) {
// button was pressed at the right time, player wins
Serial.println("You win!");
}
}
else if (buttonState == LOW) {
// button was released, set flag to true
buttonReleased = true;
}
// update current light position
currentLight = (currentLight + 1) % numLights;
// delay for a certain time to control the speed of the lights
delay(300);
}