Stopping while loop when pressing multiple buttons

Hi I am making a Led stip matrix (17x8 RGB LEDS) that I control with one Arduino data pin. I have also added 4 buttons to Arduino. I have made simple light sequence into a while Loop that runs when no buttons is pressed. When I press a button_1 few LEDs in the row #1 on the matrix will light up and the main sequence stops . But my problem is after I release the button and the main sequence in the while loop starts again the few LEDs in row 1 still keeps turned on!

I have been trying all kinds of loops but nothing works.

void loop() {

Takki_1_state = digitalRead (Takki_1); // Takki_1 is button_1
Takki_2_state = digitalRead (Takki_2);
Takki_3_state = digitalRead (Takki_3);
Takki_4_state = digitalRead (Takki_4);

buttonState = digitalRead(Takki_1);

while (buttonState == 1) {

toggleState =! toggleState;

leds[28] = (CRGB(121, 5, 76));
FastLED.show();
leds[28] = (CRGB(0, 0, 0));
FastLED.show();
leds[89] = (CRGB(7, 14, 80));
FastLED.show();
leds[89] = (CRGB(0, 0, 0));
FastLED.show();

leds[33] = (CRGB(36, 100,100));
FastLED.show();
leds[33] = (CRGB(0, 0,0));
FastLED.show();
leds[4] = (CRGB(67, 0, 70));
FastLED.show();
leds[4] = (CRGB(0, 0, 0));
FastLED.show();
leds[50] = (CRGB(55, 0, 0));
FastLED.show();
leds[50] = (CRGB(0, 0, 0));
FastLED.show();
leds[70] = (CRGB(0, 76, 60));
FastLED.show();
leds[70] = (CRGB(0, 0, 0));
FastLED.show();
leds[67] = (CRGB(12, 90, 0));
FastLED.show();
leds[67] = (CRGB(0, 0, 0));
FastLED.show();
leds[68] = (CRGB(0, 54, 50));
FastLED.show();
leds[68] = (CRGB(0, 0, 0));
FastLED.show();
leds[88] = (CRGB(46, 48,49));
FastLED.show();
leds[88] = (CRGB(0, 0, 0));
FastLED.show();
leds[134] = (CRGB(80, 0, 50));
FastLED.show();
leds[134] = (CRGB(0, 0, 0));
FastLED.show();
leds[128] = (CRGB(21, 78,230));
FastLED.show();
leds[128] = (CRGB(0, 0, 0));
FastLED.show();
leds[68] = (CRGB(7, 0, 56));
FastLED.show();
leds[68] = (CRGB(0, 0, 0));
FastLED.show();
leds[98] = (CRGB(90, 90, 0));
FastLED.show();
leds[98] = (CRGB(0, 0, 0));
FastLED.show();
leds[14] = (CRGB(0, 90, 50));
FastLED.show();
leds[14] = (CRGB(0, 0, 0));
FastLED.show();
leds[58] = (CRGB(765, 0, 0));
FastLED.show();
leds[58] = (CRGB(0, 0, 0));
FastLED.show();
leds[3] = (CRGB(0, 56, 50));
FastLED.show();
leds[3] = (CRGB(0, 0, 0));
FastLED.show();
leds[8] = (CRGB(455, 0, 0));
FastLED.show();
leds[8] = (CRGB(0, 0, 0));
FastLED.show();
leds[61] = (CRGB(0, 0, 45));
FastLED.show();
leds[61] = (CRGB(0, 0, 0));
FastLED.show();

delay (50);

buttonState = digitalRead(Takki_1);
}

toggleState =! toggleState;

if (Takki_1_state == LOW)
{
leds[0] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[0] = (CRGB(0, 0, 80));
FastLED.show();
leds[33] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[33] = (CRGB(0, 0, 70));
FastLED.show();
leds[34] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[34] = (CRGB(0, 0, 60));
FastLED.show();
leds[67] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[68] = (CRGB(0, 0, 50));
FastLED.show();
leds[67] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[67] = (CRGB(0, 0, 50));
FastLED.show();
leds[68] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[68] = (CRGB(0, 0, 50));
FastLED.show();
leds[101] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[101] = (CRGB(0, 0, 50));
FastLED.show();
leds[102] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[102] = (CRGB(0, 0, 50));
FastLED.show();
leds[135] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[135] = (CRGB(0, 0, 50));
FastLED.show();

}
else {
leds[0] = (CRGB(0, 0, 0));
FastLED.show();
leds[33] = (CRGB(0, 0, 0));
FastLED.show();
leds[34] = (CRGB(0, 0, 0));
FastLED.show();
leds[67] = (CRGB(0, 0, 0));
FastLED.show();
leds[68] = (CRGB(0, 0, 0));
FastLED.show();
leds[101] = (CRGB(0, 0, 0));
FastLED.show();
leds[102] = (CRGB(0, 0, 0));
FastLED.show();
leds[135] = (CRGB(0, 0, 0));
FastLED.show();

}

if (Takki_2_state == LOW)
{
leds[1] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[1] = (CRGB(0, 0, 80));
FastLED.show();
leds[32] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[32] = (CRGB(0, 0, 70));
FastLED.show();
leds[35] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[35] = (CRGB(0, 0, 60));
FastLED.show();
leds[66] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[66] = (CRGB(0, 0, 50));
FastLED.show();
leds[69] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[69] = (CRGB(0, 0, 50));
FastLED.show();
leds[100] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[100] = (CRGB(0, 0, 50));
FastLED.show();
leds[103] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[103] = (CRGB(0, 0, 50));
FastLED.show();
leds[134] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[134] = (CRGB(0, 0, 50));
FastLED.show();

}
else {
leds[1] = (CRGB(0, 0, 0));
FastLED.show();
leds[32] = (CRGB(0, 0, 0));
FastLED.show();
leds[35] = (CRGB(0, 0, 0));
FastLED.show();
leds[66] = (CRGB(0, 0, 0));
FastLED.show();
leds[69] = (CRGB(0, 0, 0));
FastLED.show();
leds[100] = (CRGB(0, 0, 0));
FastLED.show();
leds[103] = (CRGB(0, 0, 0));
FastLED.show();
leds[134] = (CRGB(0, 0, 0));
FastLED.show();

}

if (Takki_3_state == LOW)
{
leds[2] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[2] = (CRGB(0, 0, 80));
FastLED.show();
leds[31] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[31] = (CRGB(0, 0, 70));
FastLED.show();
leds[36] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[36] = (CRGB(0, 0, 60));
FastLED.show();
leds[65] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[65] = (CRGB(0, 0, 50));
FastLED.show();
leds[70] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[70] = (CRGB(0, 0, 50));
FastLED.show();
leds[99] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[99] = (CRGB(0, 0, 50));
FastLED.show();
leds[104] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[104] = (CRGB(0, 0, 50));
FastLED.show();
leds[133] = (CRGB(255, 0, 0));
FastLED.show();
delay (20);
leds[133] = (CRGB(0, 0, 50));
FastLED.show();

}
else {
leds[2] = (CRGB(0, 0, 0));
FastLED.show();
leds[31] = (CRGB(0, 0, 0));
FastLED.show();
leds[36] = (CRGB(0, 0, 0));
FastLED.show();
leds[65] = (CRGB(0, 0, 0));
FastLED.show();
leds[70] = (CRGB(0, 0, 0));
FastLED.show();
leds[99] = (CRGB(0, 0, 0));
FastLED.show();
leds[104] = (CRGB(0, 0, 0));
FastLED.show();
leds[133] = (CRGB(0, 0, 0));
FastLED.show();

}

}

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)


do you really want to refresh ALL the LEDs after every small change?

  leds[28] =  (CRGB(121, 5, 76));
  FastLED.show();
  leds[28] =  (CRGB(0, 0, 0));
  FastLED.show();
  leds[89] =  (CRGB(7, 14, 80));
  FastLED.show();
  leds[89] =  (CRGB(0, 0, 0));
  FastLED.show();

probably looks the same as

  leds[28] =  (CRGB(121, 5, 76));
  leds[28] =  (CRGB(0, 0, 0));
  leds[89] =  (CRGB(7, 14, 80));
  leds[89] =  (CRGB(0, 0, 0));
  FastLED.show();

and is faster to write :slight_smile:

The code does not run unless I add FastLED.show after each color formation.

Right just noticed you are changing the same leds
(The code runs but you don’t see it)

Please correct your post above and add code tags around your code

Check out this guide: http://forum.arduino.cc/index.php/topic,148850.0.html

Post #7 is what @J-M-L mentioned in Reply #1

How are your hardware buttons wired?

Load up the StateChange example. Look at how it detects that the button is pressed when it previously wasn't. And how it is not pressed when it previously was.

These are the two events you need to use to set and un-set your LEDs.

Thank you for this info. I did changes and I also to fixed this by not using a "while" loop (I was stuck there) but used a "foor" loop and "else" at the end! Now the lights work right when I using one button. Now I just have to find out how I do it with all the buttons. :wink: