im making a timing game with 7 leds, a pushbutton, and a buzzer. when i press the button, the arduino restarts. can you help me?
// Define the pin numbers
int ledPins[] = {3, 4, 5, 6, 7, 8, 9};
int buttonPin = 10;
int buzzerPin = 11;
int numLeds = 7;
bool buttonPressed = false;
bool gameWon = false;
void setup() {
// Set all LED pins as outputs
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Set the buzzer pin and button pin
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
// Run the LED chaser effect continuously
runChaserEffect();
// Check if the button is pressed
if (digitalRead(buttonPin) == HIGH && !buttonPressed) {
buttonPressed = true;
handleButtonPress();
}
// Check if the game is won
if (gameWon) {
flashBuzzer(); // Flash the buzzer
delay(1000); // Delay for 1 second
gameWon = false; // Reset the game won flag
}
}
void runChaserEffect()
{
// Turn on each LED in sequence
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], HIGH);
delay(100); // Adjust the delay to control the speed of the chaser effect
digitalWrite(ledPins[i], LOW);
}
// Turn off each LED in reverse sequence
for (int i = numLeds - 1; i >= 0; i--) {
digitalWrite(ledPins[i], HIGH);
delay(100); // Adjust the delay to control the speed of the chaser effect
digitalWrite(ledPins[i], LOW);
}
}
void handleButtonPress() {
// Handle the button press
// Add your button press logic here to determine if the game is won or lost
// For example, you can use a random number generator to determine win/lose conditions
// For the sake of example, let's assume winning condition is met
gameWon = true; // Set the game won flag
buttonPressed = false; // Reset the buttonPressed flag
}
void flashBuzzer() {
for (int i = 0; i < 3; i++) {
digitalWrite(buzzerPin, HIGH);
delay(500); // Buzzer on for 0.5 seconds
digitalWrite(buzzerPin, LOW);
delay(500); // Buzzer off for 0.5 seconds
}
}