Hi everyone, I want to change the period of EVERY_N_SECONDS macro.

Hi everyone,
I want to change the period of EVERY_N_SECONDS macro. I want to get the desired period from serial communication and store it in a variable, say “period” and then use EVERY_N_SECONDS(period){…} , but it does not work. Any ideas?
Thanks.

char str[6];
int period;
void loop() {
if(Serial.available() > 0)
{
delay(2); //wait a while to complete receive serial data(dependent to your data length)
Serial.readBytes(str, Serial.available());
Serial.flush();
period=atoi(str);
}
EVERY_N_SECONDS(period)
}

Thank you Alireza, I’ve exactly done the same thing, but it does not work. It only works if I store the “period” value in EEPROM and restart the Arduino.

Here’s an example by Mark on how to set the period: https://gist.github.com/kriegsman/841c8cd66ed40c6ecaae

And here’s a conversation where this is discussed: https://plus.google.com/u/0/+NOLAMike/posts/ZQhK2uWbt7R

Thank you @Jason_Coon , It was what I wanted, and worked fine (however I couldn’t compile the entire sketch). I used this function myself (similar to one of Arduino core examples)

if (EveryNSec( DURATION )) { nextPattern(); }

boolean EveryNSec(uint8_t period){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= period*1000) {
previousMillis = currentMillis;
return true;
}
else {return false;}
}

But I don’t know how performance efficient it is compared to FastLED macro.