Random Number Game Project (Coding Problem)

Hi everyone, I am very new to Arduino and I’m having some coding problems for a project I am doing.

The aim of the code is to have 10 LEDs flashing randomly when the button is NOT pressed.
Then when the button is pressed one of the 10 LEDs will randomly stay lit until the button is let go.

So far when I execute the code the LED on pin 3 will always stay lit no matter what state the button is in. (On my code it is only meant to be lit when the button is pressed) (I don’t know how to randomly turn on one of the 10 LEDs)

Could someone please help me or send me some useful links to look at

Many thanks any help is appreciated!

const int buttonPin = 2;
int buttonState = 0;

int LED_1 = 3;
int LED_2 = 4;
int LED_3 = 5;
int LED_4 = 6;
int LED_5 = 7;
int LED_6 = 8;
int LED_7 = 9;
int LED_8 = 10;
int LED_9 = 11;
int LED_10 = 12;

long randomNumber;

void setup() {
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
pinMode(LED_4, OUTPUT);
pinMode(LED_5, OUTPUT);
pinMode(LED_6, OUTPUT);
pinMode(LED_7, OUTPUT);
pinMode(LED_8, OUTPUT);
pinMode(LED_9, OUTPUT);
pinMode(LED_10, OUTPUT);
randomSeed(analogRead(A0));

pinMode(buttonPin, INPUT);
}

void loop() {

buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {

randomNumber = random(3,13);

digitalWrite(randomNumber, HIGH);
delay(50);
digitalWrite(randomNumber, LOW);
} else {
  digitalWrite(3,HIGH);
   }
}

What is your intended state of button pin when the button is pressed/not pressed? Which one is LOW and which one is HIGH? You are supposed to include such critical information in your question.

"Random flashing" in your code is implemented with the button pin is HIGH. So, I presume HIGH means "not pressed" and LOW means "pressed".

But in that case in your code you yourself explicitly do

digitalWrite(3,HIGH);

when the button is pressed. And you never directly set pin 3 to LOW.

Why does it surprise you that LED on pin 3 stays lit permanently?

At the moment the when the button is pressed it is in the LOW state and only the LED in pin 3 lights up.

Where would I include the line to set LED pin 3 to LOW since I still want it to be available to light up during the "random flashing" part.

Would the line be digitalWrite(3,LOW);
or would I need something more complex?

Would the line be digitalWrite(3,LOW);
or would I need something more complex?

While waiting for an answer, I hope you tried something. Simple, that is.

PaulS:
While waiting for an answer, I hope you tried something. Simple, that is.

I tried putting digitalWrite (3,LOW) underneath digitalWrite (3,HIGH)

else {
  digitalWrite(3,HIGH);
  digitalWrite(3,LOW);
   }

This seems to make it a bit better since the LED in pin 3 is not constantly on anymore; however it is now constantly flashing during the random flashing sequence even when a different LED is on.

I tried putting it in the setup part and before the "else" part although it didn't seem to make a difference.

Confus3dL3D:
At the moment the when the button is pressed it is in the LOW state and only the LED in pin 3 lights up.

Where would I include the line to set LED pin 3 to LOW since I still want it to be available to light up during the "random flashing" part.

Would the line be digitalWrite(3,LOW);
or would I need something more complex?

The way your code is currently written, you can simply add digitalWrite(3,LOW) at the very beginning of if (buttonState == HIGH) { branch and that's it.

However, your code is currently written rather wastefully. At every invocation of loop() it attempts to change the state of pin 3. A better idea would be to detect the changes in button state and accompany them with corresponding changes in state of pin 3.

Also, delay in the first branch will waste time and affect responsiveness of the button.

Confus3dL3D:
I tried putting digitalWrite (3,LOW) underneath digitalWrite (3,HIGH)

else {

digitalWrite(3,HIGH);
 digitalWrite(3,LOW);
  }

That is not a good idea. This will simply make the LED flash extremely rapidly while the button is pressed. This will act as a "manual PWM" implementation, making your LED appear dimmed.

Confus3dL3D:
however it is now constantly flashing during the random flashing sequence even when a different LED is on.

Um... According to what you said so far, that LED is supposed to participate in the random flashing sequence together with all other LEDs. What the problem then?

Montmorency:
Um... According to what you said so far, that LED is supposed to participate in the random flashing sequence together with all other LEDs. What the problem then?

The LED was flashing twice as fast as it should of done. Also it was on when other LEDs were on meaning two were on at any given time (when it should only be one).

Your idea of putting "digitalWrite(3,LOW);" before the button pin command worked great and it only has a slight jitter that is barely visible so it will be fine for now.
Many Thanks!

Do you have any idea or know any sites I could go to about making a random LED turn on when the button is pressed instead of just Pin 3?

Going back to the first lot of code you posted - look at what you are doing with the one (random) led you picked.

change delay(50); to delay(1000);

and you will see the leds turn on. 50ms is to short for the human brain to register.

After that learn NEVER to use delay! Look at blink without delay !

Mark

PS learn to use auto format BEFORE you post again!

PPS look at arrays

Mark

holmes4:
Going back to the first lot of code you posted - look at what you are doing with the one (random) led you picked.

change delay(50); to delay(1000);

and you will see the leds turn on. 50ms is to short for the human brain to register.

After that learn NEVER to use delay! Look at blink without delay !

Mark

The person playing the game should not be able to predict which LED is lit. They will have to guess which one is lit so the delay needed to be fast so they can't see and guess which LED is on.

Confus3dL3D:
The aim of the code is to have 10 LEDs flashing randomly when the button is NOT pressed.
Then when the button is pressed one of the 10 LEDs will randomly stay lit until the button is let go.

If anyone was wondering I got this part of the project working correctly! Thanks to everyone that helped especially “Montmorency” For the digitalWrite(3,LOW) part

const int buttonPin = 2;
int buttonState = 0;

int LED_1 = 3;
int LED_2 = 4;
int LED_3 = 5;
int LED_4 = 6;
int LED_5 = 7;
int LED_6 = 8;
int LED_7 = 9;
int LED_8 = 10;
int LED_9 = 11;
int LED_10 = 12;

long randomNumber;

void setup() {
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT);
  pinMode(LED_5, OUTPUT);
  pinMode(LED_6, OUTPUT);
  pinMode(LED_7, OUTPUT);
  pinMode(LED_8, OUTPUT);
  pinMode(LED_9, OUTPUT);
  pinMode(LED_10, OUTPUT);
  randomSeed(analogRead(A0));

  pinMode(buttonPin, INPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  digitalWrite(randomNumber, LOW);
  if (buttonState == HIGH) {
    randomNumber = random(3, 13);

    digitalWrite(randomNumber, HIGH);
    delay(50);
    digitalWrite(randomNumber, LOW);
  } else {
    digitalWrite(randomNumber, HIGH);
  }
}

You forgot this

So far when I execute the code the LED on pin 3 will always stay lit no matter what state the button is in.

from your first post!

Slow it down so that YOU can see what is going on.

Mark

Confus3dL3D:
For the digitalWrite(3,LOW) part

Actually, my suggestion was to put the digitalWrite(randomNumber, LOW); line inside the "true" branch of the if. You current version still keeps rapidly PWM-ing one LED when the button is held down. There's no reason to do that.

 if (buttonState == HIGH) {
   digitalWrite(randomNumber, LOW);

   randomNumber = random(3, 13);

   digitalWrite(randomNumber, HIGH);
   delay(50);
   digitalWrite(randomNumber, LOW);
 } else {
   digitalWrite(randomNumber, HIGH);
 }