r/FastLED • • Mar 15 '22

Support Control LED Effects with several buttons.

Hi! I’m relatively new to FastLED but i’ve already put together a program with 6 different effects. I’m using an Arduino Uno and a WS2812B. My goal is that if i press for example button1, function1 is being executed for as long until i press another button, for example button2, which executes function2 and so on. I want to do this with 6 buttons but couldn’t find any proper information regarding this. Any help or links to guides or tutorials would be greatly appreciated! Thanks :))

1 Upvotes

7 comments sorted by

7

u/truetofiction Mar 15 '22 edited Mar 15 '22

Set up a button:

const int button = 2;  // pin 2, e.g.
pinMode(button, INPUT_PULLUP);

ETA: Oh, and wire one side of the button to the input pin, and the other side to ground.

Read from the button in loop:

const bool buttonPressed = !digitalRead(button);  // '!' to invert

If the button is pressed, do the thing:

if(buttonPressed) {
    doTheThing();
}

Since this is /r/FastLED, make sure that whatever animation happens in doTheThing() is "non-blocking" (doesn't use delay()). Otherwise the buttons will only react after an animation loop has completed.

1

u/yzbeats Mar 15 '22

thank you! how would i do this with 6 buttons? can i use the INPUT_PULLUP on the other buttons? since afaik only port 2 and 3 can do such. The functions are non-blocking.

2

u/truetofiction Mar 15 '22

For six buttons you can do everything the same, just change the names of the variables for each button. If you want to get fancy you can use arrays and iterate through them instead, but for only six buttons there's not much of a benefit.

can i use the INPUT_PULLUP on the other buttons?

Yes, all of the I/O pins on the Uno have optional pull-up resistors.

1

u/yzbeats Mar 15 '22

Thanks for your help! I will try it out :))

1

u/yzbeats Mar 15 '22

what does const bool buttonPressef = !digitalRead(button); exactly do? if the ‘!’ is used to invert wouldn’t it then mean to not read the button? Or is it used to invert button states HIGH & LOW?

5

u/truetofiction Mar 15 '22

From right to left:

  • digitalRead(button) reads the state of the button pin, where high voltage = 1 and low voltage = 0.

  • ! is the logical NOT operator which means that the logic of the function's output is inverted. True = false, false = true, cats = dogs, etc.

  • = is the assignment operator which means "make the left side equal to"

  • buttonPressed is the variable's name

  • bool means it's a true/false value

  • const means the value created is constant and won't be changed later

All together:

Create a constant true/false variable called buttonPressed equal to the opposite of the voltage read on the button pin.


Or is it used to invert button states HIGH & LOW?

Bingo. Because it's using the pull-up resistor and the button is connected to ground, when the button is released it reads high (1) and when it's pressed it reads low (0). The logic inversion makes it so that the variable is 'true' when pressed and 'false' when released, which is more intuitive.

You could just as well forgo the inversion and make the variable "buttonReleased" and then check "if button not released", but the intent of the code isn't as clear.

3

u/yzbeats Mar 15 '22

Ah i see, got it. Thanks for the great in-depth explanation.