Page 1 of 1

time built-in variable - variables copy or reference

Posted: 03 Oct 2017 21:05
by blindekinder
Hi!
I'm trying to deal with time for some applications: send a ping to a software every second, make some object blink. I tried something like that:

Code: Select all

decl timeA = floor(time) ;
while ( (floor(time)-timeA) < 1 ) { } //which should wait for 1sec 
maybe it's my arduino reflex...
but I noticed timeA and time are exactly the same, and (time - timeA) is alway 0, which means it use time by reference and not by copy.
How can I save 'time' value?
Or is there another way to manage time?

Re: time built-in variable - variables copy or reference

Posted: 04 Oct 2017 02:37
by phase_change
I'm not sure if I understand what you're trying to do, but I think if you change it to

Code: Select all

decl timeA = floor(time) ;
while ( (time-timeA) > 0 ) { } 
or

Code: Select all

decl timeA = floor(time) ;
while ( (time-timeA) = 0 ) { } 
it will work.
The first one is more similar to your original code. While the values are greater than 0 it will do foobar. I don't think this would send out a ping every second though, but rather 999 times per second. The second one seems to me more like what you're trying to do. When the clock hits 0, so every 1000 ms, it will send out a ping.
Where I'm confused is by the reference vs. copy part. If it wasn't storing the time variable by reference, it seems to me like either floor(time)-timeA would also always = 0 or that timeA would always = 0. Either way, I don't see how this would work in your code?