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.
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).
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.
#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);
}```