so I am running a program on arduino with Fast.Led library to create a radar with leds (WS2811) I have been able to achieve the led pattern correctly, however, i can't seem to make the speed of the pattern increase depending on time increasing.
This is the code i have so far, any input will HIGHLY help as i am a newbie here hihi Thank You!
#include <FastLED.h>
#define LED_DT 3
#define COLOR_ORDER BRG
#define LED_TYPE WS2811
#define NUM_LEDS 15
uint8_t max_bright = 55;
struct CRGB leds[NUM_LEDS];
void setLEDs(int K, int N) {
FastLED.clear();
for (int i = 0; i < NUM_LEDS; i++) {
if (i % N == K) leds[i] = CRGB(200,0,0); // set red LED
}
FastLED.show();
}
void setup() {
Serial.begin(57600);
delay(1000);
FastLED.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(max_bright);
}
void loop() {
int N = 5;
unsigned long startTime = millis();
int initialDelay = 50;
int currentDelay = initialDelay;
for (int i = 0; i < N; i++) {
setLEDs(i, N);
delay(currentDelay);
}
// Increase delay every second
while (millis() - startTime < 1000) {
currentDelay = min(currentDelay + 10, 200); // Increase delay by 10 every second, capped at 200
//delay(50);
}
}
You are on the right track but you are kinda fighting with the main loop()!
Firstly you are declaring variables like currentDelay inside the loop() so every time the loop starts another cycle it will reset the variables.
Also you are making the code "blocking" by introducing things like "delay()" and a "while" loop inside which means that no other code can get executed after the while loop for at least a second.
Here is a quick example of what I think you want to achieve in a more non-blocking way.
//these variables are declared outside the loop() and we will change them as the loop keeps going and the time passes
unsigned long next_delay_increase = 0;
unsigned long next_round = 0;
int currentDelay = 50;
void loop() {
//execute this either straight away as the next_round = 0 or when time elapsed is greater/equal the currentDelay
//if you don't mind an initial 50ms delay then you can remove the "next_round == 0" check
if(millis() - next_round >= currentDelay || next_round == 0){
for (int i = 0; i < N; i++) {
setLEDs(i, N);
}
next_round = millis(); // remember the current time
}
// check that 1 sec has passed and increase the currentDelay
if(millis() - next_delay_increase >= 1000){
currentDelay = min(currentDelay + 10, 200); // Increase delay by 10 every second, capped at 200
next_delay_increase = millis(); //remember the current time
}
//the code continues here without being blocked
}