Push Button to turn off led, while still running random lighting of led

Hi all arduino masters.

I am trying to do an electronic whack a mole with six leds and six push buttons. The led will light up randomly, and the push button will switch off the led on pressing. I have been able to switch it off with the code below, but then the whole sketch just stop, other led does not light up again. Please advise how to fix this problem.

All help and advice rendered very much appreciated.

//Input and Output Initialized
int LED1 = 8; // initialise LED
int LED2 = 9;
int LED3 = 10;
int LED4 = 11;
int LED5 = 12;
int LED6 = 13;
int BTN1 = A0; // initialize push button
int BTN2 = A1;
int BTN3 = A2;
int BTN4 = A3;
int BTN5 = A4;
int BTN6 = A5;

// a variable to hold a random number
long randomNumber;

void setup() {

// start serial communication
Serial.begin(9600);
delay(150);

// send at start messages to the serial monitor
Serial.println("Start a new sequence of random number");

// set LED pins as output
pinMode (LED1, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode (LED3, OUTPUT);
pinMode (LED4, OUTPUT);
pinMode (LED5, OUTPUT);
pinMode (LED6, OUTPUT);

// set BTN pins as input

pinMode (BTN1, INPUT);
pinMode (BTN2, INPUT);
pinMode (BTN3, INPUT);
pinMode (BTN4, INPUT);
pinMode (BTN5, INPUT);
pinMode (BTN6, INPUT);

}//close setup

void loop() {

//assign a random number to our variable
randomNumber = random(8,14);

//print the output to the serial monitor window
Serial.print("The Random Number is = ");
Serial.println(randomNumber);

//turn on a "Random LED"
digitalWrite(randomNumber, HIGH);

//delay to see the LED light up
delay(1000);

//turn LED off
digitalWrite(randomNumber, LOW);
}

"the push button will switch off the led on pressing."

Where do you read an input?

.

larryd:
"the push button will switch off the led on pressing."

Where do you read an input?

.

Hi Larry,

Sorry did not complete what I wanted.
BTN1 when pressed to switch off LED1 (if it is HIGH), then continue with the random lighting of led
if LED 6 is HIGH, BTN6 when pressed switch off LED6 and loop again into random. this carry on so forth.

Thanks

The question is still the same. In the code you posted, where do you read the input so the program can be controlled by the state that is read?

Grumpy_Mike:
The question is still the same. In the code you posted, where do you read the input so the program can be controlled by the state that is read?

Hi Mike,

This is where I am stuck, I tried to put it after

//turn on a "Random LED"
digitalWrite(randomNumber, HIGH);

//delay to see the LED light up
delay(1000);

but it just turn off the led and remain off, and does not do the random again..

I am very new to coding, maybe you could guide me along.

Thanks

The code you have posted does not do ANYTHING with any buttons. So we can't tell what you did wrong when/if you tried to put some code in to check the buttons.

What results do you see from the serial prints ?

Steve

but it just turn off the led and remain off, and does not do the random again..

Just the line of code does nothing to aid understanding because we don't know where this line is. That is vital to understanding what is happening. Those two lines will not stop anything from happening. Without seeing the code that generates the variable randomNumber we can't see if it is getting too big.