Counters
2 minute read
Embedded code like on a Gemma or Micro:bit often involves going around a main loop forever. We can’t use sleep/wait/delay functions because they’ll stop everything else from happening. We can use counters to make multiple actions seem to happen at once.
Here is an example. In this case, you can see in the flowchart below that we will use
a counter called n
, and initialise it to 0. Each time around the loop we will check
if n
has reached 1000 yet. If it has we will reset it to 0. Otherwise, it will
increment n
(add 1 to it).
flowchart LR
a([Start])
b[Set n = 0]
c[Switch light off]
d{n == 1000?}
e[Set n = 0]
f[Flick switch]
g[Increment n]
a-->b-->c-->d
d--True-->e-->f-->d
d--False-->g-->d
Here is the equivalent Python code to run on your Gemma. Note that this loop goes around 1000 times before it flicks the switch each time. Observe how fast the light is flashing; remember that computers are really, really, really, really fast!
|
|
A wild example
Here is a crazier example with four counters, one each for the built-in led, and each of the red, green and blue channels of the dotstar.
Each counter is reset and its respective switch “flicked” after a given amount of time. Try changing the counter values to make different patterns.
|
|