25 light flashing sequence


Looking to have 25 LED flash on/off one at a time on after one another starting with a push button

They then need to stop after a short delay (after the button is realised ) and one light is randomly selected

to sat lit until the button is pressed again

I have got the button working and light flashing in sequence

really struggling to get the off delay and the random light to selection

My current code attached (thanks to Mitesh Upda for assistance so far)

not use if this is best approach but started with an array for the 25 pins thought this would save coding later

can anyone help with this next stage or tell me if I have the wrong approach

Code here:

//LED Pin Variables

int ledPins = {22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46}; //An array to hold the pin each LED is connected to
//i.e. LED #0 is connected to pin 2, LED #1, 3 and so on
//to address an array use ledPins[0] this would equal 2
//and ledPins[7] would equal 9

int buttonPin = 11; // button pin variable, we will be using pin 11

int val = 0; // variable to read button pin value

void setup()
//Set each pin connected to an LED to output mode (pulling high (on) or low (off)

for(int i = 0; i < 25; i++){ //this is a loop and will repeat eight times
pinMode(ledPins*,OUTPUT); //we use this to set each LED pin to output*

  • } //the code this replaces is below*

  • pinMode(buttonPin, INPUT_PULLUP); // set button pin to be an input*
    void loop( ) // run over and over again
    *{ *
    val = digitalRead(buttonPin);
    *if(val == LOW) *
    *oneOnAtATime(); *

//oneOnAtATime() - Will light one LED then the next turning off all the others
void oneOnAtATime(){

  • int delayTime = 300; //the time (in milliseconds) to pause between LEDs*

  • //make smaller for quicker switching and larger for slower*

  • for(int i = 0; i <= 25; i++){*

  • int offLED = i - 1; *

  • if(i == 0) { *

  • offLED = 25 ; *

  • } *

_ digitalWrite(ledPins*, HIGH); //turn on LED #i*_
* digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time*
* delay(delayTime);*
* }*
sketch_flashing_lights.ino (1.66 KB)


Always remember to use code tags when posting code.

(sp. "advice")

Their is a lot wrong with that code, you don’t seem to have the syntax correct in most of it. For example

pinMode(ledPins,OUTPUT); //we use this to set each LED pin to output
  }                                   //the code this replaces is below

Is wrong, it should be ledPins[i] to address the LEDs.

Also the comments on your array declaration are rubbish, so if you think that is how it works you are wrong.

More advice after you post your code correctly.

Hi, Please do not go and update your previous posts. If you change/edit your code please place it in a new posting. Other people may use this thread to help them with their problem, editing previous posts as you correct your code causes inconsistencies in the flow of solutions to your problem.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :)

Tom, understood

Mike, as I said i am new and just learning and looking for advise. This is my first post

Least I got the lights flashing on and off when I press the button so I made a start. Just learnt I need to debounce the button. I Will keep trying to improve but thanks for the advise anyway

So you don’t want any more advice then? Or do you just not know how to post code so the forum doesn’t mangle it? No one can correct mangled code, what appears in this thread as your code is not what you posted.

Hi Mike

I would love to get some advise. As I state if I have the wrong approach I would like to Know and Least I can learn from it

I would love to get some advise.

Then please post your code correctly using code tabs.
Copy your code in the IDE.
Press the </> icon at the right of the reply box, and paste your code between the code tabs that come up.

That is what Tom in reply #3 is telling you.

If you can’t manage that then it is doubtful if you can benefit from any advice we can give you. Show you care by helping us to help you. Look at your code you posted in the original post. Is that what you posted? Does your code change into italics part way down?

reply to the thread put in your code tags and you read about in the link tom posted.

post your code.

stop asking for advise, when you are want advice.

the simple things do matter.

as you make corrections in your code, post it in a new post. that way others can see the progress.

lastly, go to your first post at the bottom right of the post is a place you can use to edit your post.

put your code in code tags as the link Tom posted shows you how to do.

hi Guys

Been working on this and I got rid off the array I assume i did not need it to get the sequence to work.

I have been trying to work on method to use a button push using ‘millis’ command, but i am getting a little confused and again not sure if I am going along the correct route

I am sure how to generate the delay off timer and a random winning lamp at the end of the and then re-start the program again.

anyone with more experience who can help would be much appreciated

current code

const int buttonPin = 11; 
int speed  = 100;
boolean running = true;
long currentMillis =0;
long previousMillis =0;

void setup() {

pinMode (buttonPin, INPUT_PULLUP); // input for button 

// set output for the 25 pins 

for(int pin = 22; pin <= 46; pin++){

void loop() {

//  set start state 

for(int pin = 22; pin <= 46; pin++){
 digitalWrite (pin, LOW);

// digital write high & read low for outputs without delays allows for program to works without delays 

 for(int highPin = 22; highPin <= 46; highPin++)   
      digitalWrite(highPin, HIGH);
      readButton (speed);
      digitalWrite(highPin, LOW);
      readButton (speed);

//  method for button to, not sure on this it does seam to stop and start the sequence running 

void readButton(int delay) { 

  boolean buttonDown = false;
  boolean buttonBackUp = false;

  boolean input;

  currentMillis = millis();
  previousMillis = currentMillis;

  while(currentMillis - previousMillis < delay) {

    input = digitalRead(buttonPin);
    if (input == LOW) {
      buttonDown = true;
    else if (buttonDown == true & input == HIGH) {
      buttonBackUp = true;
    currentMillis = millis();

  if(buttonDown == true) {
    running = false;

  while(running == false & buttonBackUp == false){
    input = digitalRead(buttonPin);
    if(input == HIGH)
      buttonBackUp = true;
  int lastInput = HIGH;
  while(running == false){

    input = digitalRead(buttonPin);
    if(input == LOW){
      lastInput = LOW;
    else if(input == HIGH & lastInput == HIGH) {
      running = true;

When you post code you should say what it does and what you want it to do.

I got rid off the array I assume i did not need it to get the sequence to work.

It is almost universal that getting rid of an array is not what you want to do. Why did you put an array in there in the first place?

I am sure how to generate the delay off timer and a random winning lamp at the end of the and then re-start the program again.

OK so what are you not sure about.

Your comment in the code:-

method for button to, not sure on this it does seam to stop and start the sequence running

Yes it will stop and start the sequence because that is what you have written.

It would be good if you gave a clear explanation of what you are trying to make, then how you think you should go about programming it. We have only had the latter so far spread over a few posts and not very clear.