Hello community,
loving the level of support here and tinkering with my first project. So i have another question that i was not able to figure out how to approach from other posts here or youtube tutorials.
I have this code (see below) where i have 3 buttons controlling a moving dot on a LED stip.
1st button is triggering the moving dot at slow speed, 2nd button is triggering the moving dot at a higher speed and the 3rd button turns the dot off.
Now i would like to introduce a predefined run time for how often or how long the moving dot moves from left to right before it then stops automatically.
I was looking into the millis function but was not able to figure out how i would be able to integrate this here. If this is even the right approach.
So lets say i want the following result. When i press the 1st or 2nd button i want the LED to start moving left and right (like now) but only for 10 repetitions or for 5 seconds, whatever will be easier to code.
Thankful for any guidance here.
#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 = 21;
int button2 = 20;
int button3 = 19;
int ledspeed;
int ledcolorR;
int ledcolorG;
int ledcolorB;
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);
pinMode(button1, INPUT); digitalWrite(button1, HIGH);
pinMode(button2, INPUT); digitalWrite(button2, HIGH);
pinMode(button3, INPUT); digitalWrite(button3, HIGH);
}
void loop()
{
if (digitalRead(button1) == LOW) {
ledspeed=70;
ledcolorR = 128;
ledcolorG = 255;
ledcolorB = 200;
}
if (digitalRead(button2) == LOW) {
ledspeed=100;
ledcolorR = 128;
ledcolorG = 255;
ledcolorB = 200;
}
if (digitalRead(button3) == LOW) {
ledcolorR = 0;
ledcolorG = 0;
ledcolorB = 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(ledcolorR, ledcolorG, ledcolorB);
// 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);
}```