6 LEDs have to blink, after a button is pushed, but not held

Hi there!

Im working on a script, that activates 6 leds, after pushing, but not holding the button. When i push the button again, it has to stop. This is the code i got:

int ledPin[] = {4,5,6,7,8,9};
int ButtonPin = 13;
int state = 0;

void setup() {
  pinMode(ledPin[0],OUTPUT);
  pinMode(ledPin[1],OUTPUT);
  pinMode(ledPin[2],OUTPUT);
  pinMode(ledPin[3],OUTPUT);
  pinMode(ledPin[4],OUTPUT);
  pinMode(ledPin[5],OUTPUT);
  pinMode(ButtonPin,INPUT);  
  }

void loop(){
  state = digitalRead(ButtonPin);
    if (state==LOW){
      digitalWrite(ledPin[0],HIGH);
      delay(250);
      digitalWrite(ledPin[0],LOW);
      digitalWrite(ledPin[1],HIGH);
      delay(250);
      digitalWrite(ledPin[1],LOW);
      digitalWrite(ledPin[2],HIGH);
      delay(250);
      digitalWrite(ledPin[2],LOW);
      digitalWrite(ledPin[3],HIGH);
      delay(250);
      digitalWrite(ledPin[3],LOW);
      digitalWrite(ledPin[4],HIGH);
      delay(250);
      digitalWrite(ledPin[4],LOW);
      digitalWrite(ledPin[5],HIGH);
      delay(250);
      digitalWrite(ledPin[5],LOW);
    }
  }

  /*else
  {
  digitalWrite(ledPin[0],LOW);
  digitalWrite(ledPin[1],LOW);
  digitalWrite(ledPin[2],LOW);
  digitalWrite(ledPin[3],LOW);
  digitalWrite(ledPin[4],LOW);
  digitalWrite(ledPin[5],LOW);
  }

Thanks for your help!
1 Like

The IDE has a section under "File" called "Examples".
Look for the state change detection example, and work from there.

Please follow the advice given in the link below when posting code . Use code tags when posting code here to make it easier to read and copy for examination

Use code tags when posting code.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.