[C.CC USERS] 3 Binkin lights not in parallel

cljacobsen at gmail.com cljacobsen at gmail.com
Sun Feb 27 11:55:58 GMT 2011


Hi Lar,

The 'problem' you are seeing has to do with how we have implemented
the blink process. In fact, your three blink processes _are_ running
in parallel, they are just not blinking at the exact same rate, i.e.,
they are not blinking in lock-step. This has to do with how the blink
process performs waiting for a timeout. In pseudo code the blink
process currently looks a bit like this:
  init {
    delay_time = 500
  }
  do_forever {
    turn_on_led()
    now = read_time()
    wait_until(now + delay_time)
    turn_off_led()
    now = read_time()
    wait_until(now + delay_time)
  }
The problem here is that when we run three of these processes in
parallel that there might be small variations in how long it takes to
execute the different statements in do_forever. Thus, what might
happen is that while there should be a delay of delay_time between
turning the LED on and off, in fact there is going to be a delay that
is slightly more than delay_time, and for each of the processes, this
error is going to slightly different such that eventually the LEDs
will be blinking out of sync. The problem is essentially that we keep
on reading the time in the loop which means that we accumulate error
in the delay each time around the loop.

The way we could implement blink in order to fix this would be to do
something like the following:
  init {
    delay_time = 500
    start_time = read_time()
    wait_until_time = start_time
  }
  do_forever {
    turn_on_led()
    wait_until_time = wait_until_time + delay_time
    wait_until(wait_until_time)
    turn_off_led()
    wait_until_time = wait_until_time + delay_time
    wait_until(wait_until_time)
  }
What you will notice here is that: while it will still take a
different amount of time to execute the individual statements we are
not, as in the code above, accumulating an error in the delay. This is
because we are only reading the time once and always delaying in
relation to the first time value we got. So, while in reality the
actual delays will never be exactly 500, this is ok in this version as
we not accumulate any error because we are not reading the time each
time we go round in the loop as we do in the first snippet of code.

The only thing we could do better now is to, instead of reading the
time in each blink process, to pass in the same starting time to all
the processes. As the second bit of code is now, the processes would
all read a starting time that is slightly offset from each other, but
when you are just dealing with a few processes this delay is probably
so small that you don't notice by just looking at the blinking.

Hope that helps,
  Christian


On 27 February 2011 06:35, ljam10 at juno.com <ljam10 at juno.com> wrote:
> Ok,
>
> I was able to set up "2 blinkin lights" and the 2 leds seemed to operate in sync, but after a while the observer had the sense that they were beginning to be out of sync.
>
> I set up a program for "3 blinkin lights", and found that the 3 lights were definitely not running in parallel after a count of 10 flashes. The longer it ran, the more the lights were out of sync for parallel. Ld on Pin 10 is faster than those on Pins 12 and 13. It becomes evident over time that Pins 12 and 13 are also not running in parallel.
>
> Arduino Duemilanove, PC with Windows XP, SP3, latest concurrency download (Windows 20110201.1855 (dev)).
>
> Image attached.
>
> Any ideas?
> Lar
> ____________________________________________________________
> Globe Life Insurance
> $1* Buys $50,000 Life Insurance. Adults or Children. No Medical Exam.
> http://thirdpartyoffers.juno.com/TGL3131/4d69e2f9deeb54bad24st06vuc
> _______________________________________________
> users mailing list
> users at concurrency.cc
> http://www.concurrency.cc/mailman/listinfo/users
>
>



More information about the users mailing list