Setting direct variable values with buttons

Hi Arduino community,

i have recently started building some LED animations on a strip using the arduino nano.

I am using
int pos = beatsin16(ledspeed, 0, NUM_LEDS-1 );

in my code. Now I want to start working with buttons which will define what value ledspeed is going to be set at.

Somehow I was not able to find a video or forum thread here that would help me so far. But i assume this must be super basic so i assume i might not be looking for the right keywords.

What i would like to do basically is:

Press button 1 - ledspeed is set to 0
Press button 2 - ledspeed is set to 50
Press button 3 - ledspeed is set to 100

Would really appreciate any help of guidance towards the right tutorial to look into.

Thanks a lot.

Welcome to the forum

Something like

if (digitalRead(button1) == LOW)  //button is pressed
{
  ledspeed = 0;
}

I will leave you to add the details such as declaring the variables and setting the pinMode()

1 Like

Use the state change detection example code to count button presses. Use a switch case structure to select the ledspeed based on button press count.

Edit: misread the requirement.

That was my initial thought too but the requirement appears to be to use multiple buttons

No doubt @mrchikita will tell us when he replies

Thank you @UKHeliBob . I think i already tried this way and if im not mistaken this resulted only in a temporary change while the button is pressed. So i would need to keep it pressed for the variable to stay at that value. But maybe i m mixing something up here (as i have been trying this for a while now).

So i will give it another try in a bit :wink:

That depends entirely in how and where you initialise the value of the variable

Please post a complete sketch showing what you tried. Can you confirm whether you are using a single or multiple pushbuttons

...look for other places that set ledspeed:

ledspeed = XXXX ;

Please show your code and wiring.

The state change detection method will help with that. Also see the state change detection for active low button tutorial.

It could be that ledspeed isn't a persistent static or global variable-- scope - Arduino Reference

Or it could be that the buttons are wired up as normally LOW.

Thanks a lot everyone. I will give these hints a try and post my code once i m back at home.

@UKHeliBob I m trying to connect up to 3 push buttons.

@UKHeliBob your if loops really made it work. Thank you so much for the quick support.

I am glad that you got it working but for the sake of accuracy there is no such thing as an if loop, just an if test

Do you know why it worked for you this time when it didn't before ? What did you change ?

1 Like

Yes i was using way too complex examples before trying to build toggle logics and things while i never tried this clean straight forward approach.

Perhaps future members who come across this thread would appreciate seeing your solution.

1 Like

I did what @UKHeliBob suggested in the first reply.

For two buttons i did this to switch between 0 and 90:

if (digitalRead(button1) == HIGH)
{
  ledspeed = 90;
  
}

if (digitalRead(button2) == HIGH)
{
  ledspeed = 0;
}```

Thank you for sharing.

It is optional but, in my opinion, more useful for the newer members if you post a complete sketch that they can load and run to see how the code works, as a whole, and see the results.

Of course. Here is the sketch

#include <FastLED.h>

#define DATA_PIN    2
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB
#define NUM_LEDS    72
CRGB leds[NUM_LEDS];

#define BRIGHTNESS          96
#define FRAMES_PER_SECOND  180

int button1 = 12;
int button2 = 11;

int ledspeed;

void setup() {

  delay(3000); // 3 second delay for recovery
  
  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);

  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);
  
  FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);

}


void loop()
{

if (digitalRead(button1) == HIGH)
{
  ledspeed = 90;
  
}

if (digitalRead(button2) == HIGH)
{
  ledspeed = 0;
}

  // a colored dot sweeping back and forth, with fading trails
  fadeToBlackBy(leds, NUM_LEDS, 254);

   int pos = beatsin16(ledspeed, 0, NUM_LEDS-1 );
   leds[pos] += CRGB( 100, 127, 90);

  // send the 'leds' array out to the actual LED strip
  FastLED.show();  
  // insert a delay to keep the framerate modest
  FastLED.delay(50/FRAMES_PER_SECOND); 

}```
1 Like

Thank you. :slightly_smiling_face:

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