Help with a timed random light sequence.

So I'm trying to build a prop that has 5 lights on it that when you press a button it will randomly blink through the lights for 40 seconds before settling on a single light. This is my first real project and I simply can't get the code to well, run. I push my button, nothing happens. Obviously I'm doing something wrong but I can't figure out what. If anyone can help me figure this out i would greatly appreciate it. Here is the code i have so far. Note: It is incomplete so not all variables are currently being used, i will add them in once i fix the issue with the randomizing lights.

//button
const int buttonPin = 2;
boolean lastButton = LOW;
boolean currentButton = LOW;
//light sequence
boolean sequenceOn = false;
//potentiometer
int potValue = 0;
const int potPin = A1;
//Pin 1 = power on light
const int LEDPin1 = 5;
//Pin 2-6 = status lights
const int LEDPin2 = 6;
const int LEDPin3 = 7;
const int LEDPin4 = 8;
const int LEDPin5 = 9;
const int LEDPin6 = 10;
//random number generation
int randNumber1;
int randNumber2;
//button debouncing
boolean debounce(boolean last){
  boolean current = digitalRead(buttonPin);
  if (last != current){
    delay(30);
    current = digitalRead(buttonPin);
  }
  return current;
}

void setup() {
randomSeed(analogRead(5));
pinMode(buttonPin, INPUT);
pinMode(LEDPin1, OUTPUT);
pinMode(LEDPin2, OUTPUT);
pinMode(LEDPin3, OUTPUT);
pinMode(LEDPin4, OUTPUT);
pinMode(LEDPin5, OUTPUT);
pinMode(LEDPin6, OUTPUT);
digitalWrite(LEDPin1, HIGH);
}

void loop() {
//Turn on the light sequence
  currentButton = debounce(lastButton);

if (lastButton == LOW && currentButton == HIGH)
  {sequenceOn = !sequenceOn;
   }
   lastButton = currentButton;
   
   //have random numbers generate during the sequence on time
   //lights change every quarter of a seond
   while (sequenceOn == true){
  randNumber1 = (1,6);
  delay(250);
}
   //these random numbers will be used to control a sound element after i fix the lights
   while (sequenceOn == true){
     randNumber2 = (1,101);
     delay(50);
   }
   //turn on one light randomly
   if (randNumber1 == 1) {
    digitalWrite(LEDPin2, HIGH);}
    else {
      digitalWrite(LEDPin2, LOW);
   }
   if (randNumber1 == 2) {
    digitalWrite(LEDPin3, HIGH);
   }
    else {
      digitalWrite(LEDPin3, LOW);
   }
   if (randNumber1 == 3) {
    digitalWrite(LEDPin4, HIGH);
   }
    else{
     digitalWrite(LEDPin4, LOW);
   }
   if (randNumber1 == 4) {
     digitalWrite(LEDPin5, HIGH);
   }
    else{
      digitalWrite(LEDPin5, LOW);
   }
   if (randNumber1 == 5) {
      digitalWrite(LEDPin6, HIGH);
   }
    else{
      digitalWrite(LEDPin6,LOW);
   }
   delay(40000);
   sequenceOn = !sequenceOn; //turn off the light sequence
}

What do these statements actually do?

randNumber1 = (1,6);
randNumber2 = (1,101);

What have you debugged?

Step 1) Does the debounce(); function work? Step 2) Does sequenceOn ever get set to TRUE? Step 3) What is the value of randNumber1 ? Step 4) What is the value of randNumber2 ?

It's best to use code tags when posting code or errors, rather than quote tags. It makes it easier to read, doesn't convert it to italics and also allows for easy selection and copying/pasting for test in the IDE. (You could edit, and put "code" in the opening and closing tags instead of "quote".)

This stops all execution and loops forever:-

 while (sequenceOn == true)
    {
        randNumber1 = (1, 6);
        delay(250);
    }

(Nothing within the 'while' loop ever changes the 'sequenceOn' value.)

This would do the same, if execution could get that far:-

 while (sequenceOn == true)
    {
        randNumber2 = (1, 101);
        delay(50);
    }

Also, it would be more readable if you used 'switch case' rather than a series of 'if' statements.

And even if you do prefer the 'if' statements, there's no need for brackets when only a single statement follows the 'if' and 'else':-

 //turn on one light randomly
    if (randNumber1 == 1)
        digitalWrite(LEDPin2, HIGH);
    else
        digitalWrite(LEDPin2, LOW);
    if (randNumber1 == 2)
        digitalWrite(LEDPin3, HIGH);
    else
        digitalWrite(LEDPin3, LOW);
    if (randNumber1 == 3)
        digitalWrite(LEDPin4, HIGH);
    else
        digitalWrite(LEDPin4, LOW);
    if (randNumber1 == 4)
        digitalWrite(LEDPin5, HIGH);
    else
        digitalWrite(LEDPin5, LOW);
    if (randNumber1 == 5)
        digitalWrite(LEDPin6, HIGH);
    else
        digitalWrite(LEDPin6, LOW);

And what mrsummitville said. This might work better:-

randNumber2 = random(1, 101);

Well by pointing out those two statements i noticed i forgot a section of code. It's supposed to be randNumber1 = random(1,6); and randNumber2 = random(1,101); These are supposed to generate random numbers between those values.

Debounce works, i tested it with a simple LED earlier in the project sequenceOn should be set to true once the button is pressed

also after changing those random functions it's still not working, but at least that's one thing figured out.

The code ends after the first 'While' sequence? I was under the impression it would run alongside the rest of the code. I essentially need 2 random number generators running at the same time but at different rates. What should i use if not the 'While' statement? Also i'm not sure what a 'switch case' is.

KWeiss: Well by pointing out those two statements i noticed i forgot a section of code. It's supposed to be randNumber1 = random(1,6); and randNumber2 = random(1,101); These are supposed to generate random numbers between those values.

Sorry, I was editing my last reply at the same time and didn't realise you'd picked yourself up on this point. :)

I fixed my main post, sorry for the inconvenience. I think the main thing i need to figure out right now is how to run 2 seperate random number generators at different rates for a certain amount of time without having the delays affect the rest of the code.

Here's the reference on switch case:- switch case

You don't "run" a random number generator as such. It's just a mathematical function that occurs almost instantaneously. Just call 'random()' immediately before you need to use the random number.

To get this to work, you need to re-think the logic, and also base your timing on millis() to have things happen the way you want.

i'm flat-out on my program right now, but this might help:- Several things at the same time

Thanks for the advice, I’ll do a bit of reading and see if i can it working.

As "YoungSteve" says, while{} loops effectively stops other code execution when the while loop is looping. I do not believe you intend this.

Did you know your can press "CTRL T" in the IDE to format the sketch? Put { and } on separate lines to make things more readable. Are you aware of using BWD techniques to stop using the evil delay() function? If you put some Serial.println("I GOT HERE"); statements at strategic locations you can see if you are executing code. You can also Serial.println(myVariable); to check variables out too.

KWeiss: Thanks for the advice, I'll do a bit of reading and see if i can it working.

No worries. After you do that reading, we'll help all we can if you strike problems.

Meantime, I've got a bug of my own to track down. (You're not the only one. :) )

Just wanted to say I managed to fix the errors and even complete my project.
My code is probably sloppy and more than likely could have been done better but it works!
Now to never touch it again in fear of ruining everything.

KWeiss:
Just wanted to say I managed to fix the errors and even complete my project.
My code is probably sloppy and more than likely could have been done better but it works!

Good one. And no doubt you learned a few things along the way. That’s what it’s really all about. :slight_smile:

Now to never touch it again in fear of ruining everything.

There’s nothing wrong with trying to improve it. There are always things that can be improved in our code on a second visit.
Just make sure you make a backup of the working version first. Then you can’t f up.