Random Numbers and LED

Good day to everyone,

Can someone help me with my code:

The goal of the program is to create a random number (0-3) and Light it up on an LED on (pin1, pin2, pin3) based on the random number output and must be continuous when I am pushing the button. The problem is ONLY One LED must light up on every cycle...........

AND while I release the pushbutton the last value of the random number must lit Up steady. .

The problem of my current code is when I start to push the button the loop does not stop even I release the button and no clearing of last State of LED is happening (multiple LED lights up).

Thanks for your help. Beginner Here :slight_smile:

LED 1= Pin1
LED 2= Pin2
LED 3= Pin3
Pushbutton= Pin 22
*Arduino Mega

int i;
int powerbutton;
int RandomSeq;
void setup() 
{
 for(i=0;i>=3;i++)
 {
  pinMode(i,OUTPUT);
 }
  pinMode(22,INPUT);
}

void loop() 
{
  powerbutton=digitalRead(22);
   while(powerbutton==1)
  {
   RandomSeq=random(0,3);
  digitalWrite(RandomSeq,1);
  delay(250);
  }
  

}

Normally you try to avoid using pins 0 and 1. Those are required for serial communication with the PC. If you have things plugged into those pins, you may need to unplug them to be able to reprogram the Arduino.

Set up an array of pins, so that you can refer to them as 0, 1, 2 but they might connect to totally different physical pins.

const int MAXLEDS = 3;
int LEDArray[MAXLEDS];
LEDArray[0] = 13; //built-in LED on pin 13
LEDArray[1] = 21; //obviously choose your own pin numbers to suit
LEDArray[2] = 2;
void setup() {
  for(int i=0; i<MAXLEDS; i++) {
    pinMode(LEDArray[i], OUTPUT);
  }
}

Your code writes “1” to the output pins, but you never write a pin back to 0. They will all eventually get turned on and never turned off. If you want only one LED to be lit, then you need to cycle through all the others and turn them off.

Then, because you never take another reading from the pin inside the while() loop, it can never exit that loop.

const int PowerButtonPin = 22;
void loop() {
  while(HIGH == digitalRead(PowerButtonPin)) {
    //button is held down (or held up?)
    RandomSeq = random(0,3);
    for(int i=0; i<MAXLEDS; i++) { 
      digitalWrite(LEDArray[i], i==RandomSeq); //write a boolean value to every LED
    }
    delay(250);
  }
  //What do you want to happen when the button is released? All LEDs go off or it keeps the last one lit?
  //put that code here.
}

The problem is you turn the LED's on but not off. Put before writing the new LED a digitalWrite(foreveryPin, LOW).

Plus your code is saying something different then you do. The code says while you push the button it should create a number and write it. If you want to have a random blinking and an initial stop when pushing the button I would write it with an if loop.

if(powerbutton == 0)
{
RandomSeq=random(0,3);
digitalWrite(RandomSeq,HIGH);
}
else
{
digitalWrite(RandomSeq,HIGH);
}

delay(259);

I'm not quite sure if that wil overwrite your command but you can try it out.

Best regards
MajorProb

Thank you for reviewing my posts.Ill try to review and check my codes after I applied what you have taught me, :slight_smile: