I was wondering if there is any simpler method to generate a periodic delay without using the actual arduino 'delay' function
I have been using blink without delay, but once one tries to add more than two the code becomes a huge mess of variables!
Any solution?
Additionally: I've read a thread some time ago that explained there was an error on the blink without delay function and sugested replacing something on it: Basically using the time the function was scheduled to happen again, rather than the time since it last happened or similar. i cant seem to find that topic again. Anyone knows what i am talking about?
You could try encapsulating the variables into functions as static variables instead of globals. As long as they only need to be accessed in that function, this will save you some naming conflicts and make the code more easily moved between projects.
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
boolean delay_without_delaying(unsigned long time) {
// return false if we're still "delaying", true if time ms has passed.
// this should look a lot like "blink without delay"
static unsigned long previousmillis = 0;
unsigned long currentmillis = millis();
if (currentmillis - previousmillis >= time) {
previousmillis = currentmillis;
return true;
}
return false;
}
boolean delay_without_delaying(unsigned long &since, unsigned long time) {
// return false if we're still "delaying", true if time ms has passed.
// this should look a lot like "blink without delay"
unsigned long currentmillis = millis();
if (currentmillis - since >= time) {
since = currentmillis;
return true;
}
return false;
}
unsigned long ledtime = 0;
unsigned long atime, btime, ctime, nltime;
void loop() {
static int ledstate = false;
if (delay_without_delaying(ledtime, 500)) {
ledstate = !ledstate;
digitalWrite(13, ledstate);
}
if (delay_without_delaying(atime, 100)) {
Serial.print("A");
}
if (delay_without_delaying(btime, 200)) {
Serial.print("B");
}
if (delay_without_delaying(ctime, 30)) {
Serial.print("C");
}
if (delay_without_delaying(nltime, 1000)) {
Serial.print("\n");
}
}
Whats the maximum time I can define without having to declare the variables as long? I am not very familiar with the functions used by the time library, but assuming each variable step equals to one milli, a uint16_t should be good for 65 seconds which is way above anything i will use these kind of functions for.
Unless of course i need to load the exact time of the mili functions, that rolls over every [is it 75 minutes?].