I develop my first Arduino project which is my first embed project.
Usually I write higher code (like Java) and calling Time functions is very painful.
On this board, can I call Millis() in real time?
I.E.
void loop()
{
...
if (millis()>x) {
...
}
...
if (millis()>y) {
...
}
...
if (millis()>z) {
...
}
}
Or should I write this:
void loop()
{
unsigned long time = millis();
...
if (time>x) {
...
}
...
if (time>y) {
...
}
...
if (time>z) {
...
}
}
The only difference is in the first example, it is possible that the value of millis() can change between each if statement. With the second example, all of your if statements will be testing against the same value.
Whether that difference is important or not would depend on what exactly you are looking for.
Ok, but if reading the value of a variable costs 1 CPU cycle, 4 bytes for the value and 2 bytes for the address of the value. (?)
Calling Millis() costs only 1 CPU cycle and 4 bytes for the value? (I would like to compare the value and/or to apply bitmasks)
While it is always good to consider efficiency and optimizations, you are early in the process. The time it takes to call millis() is very likely to be on of your last concerns.
The larger concern was already mentioned. Do you want the if-statements to evaluate against the same timestamp or not?
Not often I get two mistakes in one post. No, it doesn't. First I was thinking byte when I typed int. Second, the "function" is a macro, with no defined type. So, it can take any argument - byte, int, long, float, pointer, etc. Of course, not all make sense.