I need my led's to stop with a push button. Help please.

Hi guys,

This is my first time working with Aurduinos. I have an Arduino Uno and I have 10 led's turning on and off in a sequence from 1-10 and then repeat. I would like for the loop to stop when a push button is pressed and then continue when the push button is pressed again. i tried a few things I saw online but did not manage to make it stop. Any kind of help will be great full.

/*
  Blinking LEDs - test program to run 3 LEDs in a pattern of blinks
*/

int buttonPin = 2;
int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int led5 = 7;   // the number of the LED pin
int led6 = 8;   // the number of the LED pin
int led7 = 9;   // the number of the LED pin
int led8 = 10;   // the number of the LED pin
int led9 = 11;   // the number of the LED pin
int led10 = 12;   // the number of the LED pin

// the setup routine runs once when you press reset:
void setup() { 
              
  // initialize the digital pin as an output.
  pinMode(led1, OUTPUT);   
  pinMode(led2, OUTPUT);  
  pinMode(led3, OUTPUT);    
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);   
  pinMode(led7, OUTPUT);  
  pinMode(led8, OUTPUT);    
  pinMode(led9, OUTPUT);
  pinMode(led10, OUTPUT);
  pinMode(buttonPin, INPUT);
  
}

// the loop routine runs over and over again forever:
void loop() {
//check button pressed, if so enter program condition (inside if statement)
  if(digitalRead(buttonPin) == LOW) //functions based off of button pulling input pin LOW
  {
  
  digitalWrite(led1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(800);                      // wait for 1/2 a second
  digitalWrite(led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(led2, HIGH);    // turn the LED on (HIGH is the voltage level)
  delay(800);                      // wait for 1/2 a second
  digitalWrite(led2, LOW);     // turn the LED off by making the voltage LOW
  digitalWrite(led3, HIGH);     // turn the LED on (HIGH is the voltage level)
  delay(800);                      // wait for 1/2 a second
  digitalWrite(led3, LOW);      // turn the LED off by making the voltage LOW
  digitalWrite(led4, HIGH);     // turn the LED on (HIGH is the voltage level)
  delay(800);                      // wait for 1/2 a second
  digitalWrite(led4, LOW);      // turn the LED off by making the voltage LOW
  digitalWrite(led5, HIGH);     // turn the LED on (HIGH is the voltage level)
  delay(800);    
  digitalWrite(led5, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(led6, HIGH);    // turn the LED on (HIGH is the voltage level)
  delay(800);                      // wait for 1/2 a second
  digitalWrite(led6, LOW);     // turn the LED off by making the voltage LOW
  digitalWrite(led7, HIGH);     // turn the LED on (HIGH is the voltage level)
  delay(800);                      // wait for 1/2 a second
  digitalWrite(led7, LOW);      // turn the LED off by making the voltage LOW
  digitalWrite(led8, HIGH);     // turn the LED on (HIGH is the voltage level)
  delay(800);                      // wait for 1/2 a second
  digitalWrite(led8, LOW);      // turn the LED off by making the voltage LOW
  digitalWrite(led9, HIGH);     // turn the LED on (HIGH is the voltage level)
  delay(800);          
  digitalWrite(led9, LOW);  
  digitalWrite(led10, HIGH);
  delay(800);
  digitalWrite(led10, LOW);
  delay(500);                     // wait for a second
  }
  


  }

sketch_jan14d.ino (3.06 KB)

How did you wire the button ?

This is basically my sketch but with 10 led's

Depends on a button press causing the pin to go high-to-low.

Compiles, not tested.

/*
  Blinking LEDs - test program to run 3 LEDs in a pattern of blinks
*/

int buttonPin = 2;
int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int led5 = 7;   // the number of the LED pin
int led6 = 8;   // the number of the LED pin
int led7 = 9;   // the number of the LED pin
int led8 = 10;   // the number of the LED pin
int led9 = 11;   // the number of the LED pin
int led10 = 12;   // the number of the LED pin

unsigned long
    timeNow,
    timeSequence,
    timeDelay;
 
bool
    bSequenceState;
int
    ledIdx,
    buttonNow,
    buttonLast;

// the setup routine runs once when you press reset:
void setup() 
{
    int
        i;
                
    // initialize the digital pin as an output.
    for( i=0; i<10; i++ )
        pinMode(led1+i, OUTPUT);   

    pinMode(buttonPin, INPUT_PULLUP);
    
    buttonNow = digitalRead(buttonPin);
    buttonLast = buttonNow;
    bSequenceState = true;
    ledIdx = 0;
    timeNow = millis();
    timeSequence = timeNow;
    timeDelay = 0;
    
}//setup

bool ReadValidateButton( void )
{
    buttonNow = digitalRead(buttonPin); 
    if( buttonNow == LOW )
    {
        if( buttonLast == HIGH )
        {
            buttonLast = buttonNow;
            return( true );
            
        }//if
        else
        {
            //button was low and still is...return invalid
            buttonLast = buttonNow;
            return( false );
            
        }//else
            
    }//if
    else
    {
        buttonLast = buttonNow;
        return( false );
    
    }//else
    
}//ReadValidateButton

void loop() 
{
    if( ReadValidateButton() == true )
        bSequenceState ^= 1; 

    if( !bSequenceState )
        return;         

    timeNow = millis();
    if( (timeNow - timeSequence) > timeDelay )
    {
        timeSequence = timeNow;

        switch( ledIdx )
        {
            case    0:
                digitalWrite(led1, HIGH);   // turn the LED on (HIGH is the voltage level)
                timeDelay = 800;
                ledIdx++;
            break;
            case    1:
                digitalWrite(led1, LOW);    // turn the LED off by making the voltage LOW
                digitalWrite(led2, HIGH);    // turn the LED on (HIGH is the voltage level)
                timeDelay = 800;
                ledIdx++;
            break;
            case    2:
                digitalWrite(led2, LOW);    // turn the LED off by making the voltage LOW
                digitalWrite(led3, HIGH);    // turn the LED on (HIGH is the voltage level)
                timeDelay = 800;
                ledIdx++;
            break;
            case    3:
                digitalWrite(led3, LOW);    // turn the LED off by making the voltage LOW
                digitalWrite(led4, HIGH);    // turn the LED on (HIGH is the voltage level)
                timeDelay = 800;
                ledIdx++;
            break;
            case    4:
                digitalWrite(led4, LOW);    // turn the LED off by making the voltage LOW
                digitalWrite(led5, HIGH);    // turn the LED on (HIGH is the voltage level)
                timeDelay = 800;
                ledIdx++;
            break;
            case    5:
                digitalWrite(led5, LOW);    // turn the LED off by making the voltage LOW
                digitalWrite(led6, HIGH);    // turn the LED on (HIGH is the voltage level)
                timeDelay = 800;
                ledIdx++;
            break;
            case    6:
                digitalWrite(led6, LOW);    // turn the LED off by making the voltage LOW
                digitalWrite(led7, HIGH);    // turn the LED on (HIGH is the voltage level)
                timeDelay = 800;
                ledIdx++;
            break;
            case    7:
                digitalWrite(led7, LOW);    // turn the LED off by making the voltage LOW
                digitalWrite(led8, HIGH);    // turn the LED on (HIGH is the voltage level)
                timeDelay = 800;
                ledIdx++;
            break;
            case    8:
                digitalWrite(led8, LOW);    // turn the LED off by making the voltage LOW
                digitalWrite(led9, HIGH);    // turn the LED on (HIGH is the voltage level)
                timeDelay = 800;
                ledIdx++;
            break;
            case    9:
                digitalWrite(led9, LOW);    // turn the LED off by making the voltage LOW
                digitalWrite(led10, HIGH);    // turn the LED on (HIGH is the voltage level)
                timeDelay = 800;
                ledIdx++;
            break;
            case    10:
                digitalWrite(led10, LOW);    // turn the LED off by making the voltage LOW
                timeDelay = 500;
                ledIdx = 0;
            break;
            
        }//switch
        
    }//if

}//loop