Go Down

Topic: How to can I make multiple LEDs circuit stop with a button? (noob here) (Read 328 times) previous topic - next topic

alexholstv

I have this multiple LED circuit, in which the program lights up each LED in sequence and then loops (simple). Now i'd like to add the button to interrupt the circuit, but get the LED (which was lit up when pressing the button) to stay on. Sort of like the arcade game "cyclone".

Here is some code if you are a bit confused on how I did this:
Code: [Select]
void setup() {
  for (int Pin = 3; Pin < 8; Pin++) {
    pinMode(Pin, OUTPUT);
  }
}

void loop() {
  
  for (int Pin = 3; Pin < 8; Pin++) {
    digitalWrite(Pin, HIGH);
    delay(100);
    digitalWrite(Pin, LOW);
  }

  for (int Pin = 7; Pin >= 3; Pin--) {
    digitalWrite(Pin, HIGH);
    delay(100);
    digitalWrite(Pin, LOW);
  }

}


I also attached a picture of how my wiring is...
Thanks in advance!

Grumpy_Mike

Simply read the button in that loop and if it is pushed then hold until it is released. Use a while loop to keep reading the button and only finish when it is released.
Code: [Select]
while( digitalRead (inputPin) == LOW) ;

Note wire up the push button between input and ground and enable the internal pull up resistors.

alexholstv


Grumpy_Mike

sorry, should the button be in the circuit as the LED is?
Well yes if you want it to work with the sketch.

dougp

The switch arrangement suggested by @Grumpy_Mike would be wired as S3 in the pictured example.  Note, the switches can be connected to any digital input (avoid using D0 & D1).

Everything we call real is made of things that cannot be regarded as real.  If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet. - Niels Bohr

No private consultations undertaken!

Go Up