Here is my full code (for what i did last night).
#include <FastLED.h>
#define LED_PIN 2
#define NUM_LEDS 50
#define BRIGHTNESS 255
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
int onOff;
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 30;
void setup() {
Serial.begin(9600);
//delay( 3000 ); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
startMillis = millis();
}
void loop() {
if (Serial.available()) {
onOff = Serial.read();
}
reactLEDStrip();
FastLED.delay(1000 / UPDATES_PER_SECOND);
}
//NOT WORKING -- NEED TO BYPASS CYCLE LOOP
//reacts to led
void reactLEDStrip () {
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
if (onOff == 1) {
for (int i = 0; i < NUM_LEDS; i++)
{
leds[i].setHue(random(256));
FastLED.show();
leds[i].setRGB( 0, 0, 0);
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
}
}
FastLED.delay(1000 / UPDATES_PER_SECOND);
}
}
i will try what you suggested now and see if it works. It makes sense to me as far as im reading it. As per my onOff values, they are coming from MAX MSP which keeps track of music, whenever the low end spikes it send a value of 1 to the arduino serial which is then read and updated to a variable. Thats how my onOff gets it values
sterretje:
One of the reasons why I asked for full code is that I don't know if you ever update previousMillis and how you obtain onoff.
Below is a reworked version of your function. I did not implement onoff. It uses a number of static variables so it's self-contained. Static variables are like global variables, but are only known in the function where they are declared. I hope that the code is sufficiently documented for you to understand; if not, ask.
The function returns true once all leds are done, else false.
I've used print statements to print what is happening
/*
non-blocking update of strip
Returns:
true if function completed one cycle, else false
*/
bool reactLEDStrip()
{
// variable to remember that last time that the strip was updated; replace your previousMillis
static uint32_t lastUpdateTime;
// variable to remember the last updated led; replaces i in your for-loop
static uint16_t ledCnt;
// current time
uint32_t currentTime = millis();
// check if it's time to update the strip
if (currentTime - lastUpdateTime >= interval)
{
// print some info
Serial.print(F("Updating led "));
Serial.print(ledCnt);
Serial.println(F(" in strip"));
// update the last time that the strip was updated
lastUpdateTime = currentTime;
// your original code here
leds[ledCnt].setHue(random(256));
FastLED.show();
leds[ledCnt].setRGB( 0, 0, 0);
// update the led count
ledCnt++;
// if at end of strip
if (ledCnt == NUM_LEDS)
{
// inform user
Serial.println(F("End of strip reached"));
// start from the beginning
ledCnt = 0;
return true;
}
}
return false;
}
You can use the function as shown below (function itself not included). This makes use of the return value so you can check in loop() if a cycle was completed.
#include <FastLED.h>
#define NUM_LEDS 20
#define DATA_PIN 6
uint32_t interval = 500;
CRGB leds[NUM_LEDS];
void setup()
{
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
Serial.begin(57600);
}
void loop()
{
bool isCompleted;
isCompleted = reactLEDStrip();
if (isCompleted == true)
{
Serial.println(F("Completed"));
}
}