Why when the button pressed the arduino restart?

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
  }
}

My guess would be that when you press the button you are short circuiting 5V and GND

You don't need 3 connections to the button. Use INPUT_PULLUP in pinMode() to turn on the built in pullup resistor, change the wiring to take the pin to GND when the button is pressed and detect LOW on the pin when the button is pressed

1 Like

I think you are missing a pull down resistor connected between the button and the ground. Anyway, I don't know why are you having a restart, but try that out

Use 10KOhms

thanks!!!

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

a button press can be recognized by detecting a change in state and becoming LOW

but is also looks like there are no current limiting resistors in series with the LEDs. the LEDs can be damaged without the resistor

The UNO should run very hot per your fuzzy picture. the 3V3 is shorted on the proto board where you plug both +- into the same bus.

Or you can damage the Arduino or cause it to glitch/reset, etc.!!!

Here is an LED resistor calculator, or you can just use 220 Ohms (or something close to that). The resistor can go on either side of the LED as long as it's in series.

...LEDs are "current operated" and like all diodes they are non-linear. As the voltage goes-up, resistance goes down, and once you are above the normal forward operating it drops drastically and you'll get excess current if there's nothing to limit it. (Resistance is "the resistance to current flow".) Ohm's Law

...Some people skip the resistors and they sometimes "get away with it". So, sometimes you'll see that on the Internet. But, it's poor-amateur engineering!

High power LEDs (1W and above) normally use a switchmode constant-current driver because power is wasted in the resistor.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.