I was thinking... what's the easiest and i mean EASIEST way to create a "delay" without actually implementing a delay, for example, you
have a sensor sending the results between 0-1024.
but, when you do Serial.println(data);
so, what are your options?
Create a Delay time, measure how many seconds pass and if > 3 seconds, display(whatever); the other way is to simply
Serial.println(data)
delay(400);
but that might effect time critical routines... so a few weeks back it occurred to me ...
int n = random(500);
if (n==65)
Serial.println(data);
every couple of seconds or so, you see the data coming in, is using Random? taxing on the CPU? any benefits to using this method?
The only benefit I can see is that you output arrives at random intervals. If that's what you wanted, this achieves it.
If what you really want is to have the output arrive at defined regular intervals while also doing other things in the meantime, then the technique demonstrated in the 'blink without delay' example sketch shows you how to achieve that.
If your loop takes more than half a millisecond or so, won't you miss some of the times the last 9 bits of millis() are 0? It seems you'll have your println at some pseudo-random multiple of half a second.
Well the correct way to do it is only three lines:
static unsigned long lasttime;
if (millis() - lasttime > 1000) {
Serial.println();
lasttime = milis();
}
I suppose you could make a macro
#define RUNEVERY(n) for (static unsigned long lasttime; millis() - lasttime > (n); lasttime = millis())
and then use it like this:
void loop() {
int sens = analogRead(0);
RUNEVERY(500) {
Serial.print(F("Sens: "));
Serial.println(sens);
}
}
That's untested, since I don't have arduino on this computer. But if that works, I'd support it being defiend in Arduino.h --- it is extremely useful and would preempt a lot of questions on this forum. (Although there is something to be said for learning to do it the "right" way, the same can be said of "hibyte," "lowbyte," "shiftout," "bitRead," etc.)
That doesn't work either, though: static means something different there.
I also think that runEvery is a better name because it reads "Run every 500" (ms) which is exactly what it does. runAfter imples it only runs once, and has a built in delay.
_thistime is unnecessary if you do the += (n) trick, since then millis() wil only be called once.
Both the original (random) and my hack (bitwise and) don't use extra ram...
Whether they're suitable or not depends on unspecified requirements and behaviors.
is illegal. You might be able to do some strange things with a struct though:
According to the standard. 6.5.3 - 3
If the for-init-statement is a declaration, the scope of the name(s) declared extends to the end of the for-statement.
But I see the static now, . hmmm
Well I wasn't referring to it being a for loop, I was saying that you can't use commas to declare two variables with different types --- like one int and one float or one static int and one int.
You can give Google a helping hand. If you or anyone you know has a blog, add a paragraph or two about the macro and include a link to this topic.
Well then I'd feel special.
Is the playground a good place to put it? Or put a feature request in the google code place.