Skip to content

Commit dabf4ec

Browse files
zitterbewegungdpgeorge
authored andcommitted
Add counter.py example script, to count number of button presses.
Simple counter for the BBC micro:bit MicroPython for pedagogical purposes.
1 parent 7e7cb72 commit dabf4ec

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

examples/counter.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
counter.py
3+
~~~~~~~~~~
4+
Creates a counter that increments on pressing button a.
5+
Scrolls the current count on the led display.
6+
"""
7+
8+
import microbit
9+
ctr = 0 # Initialize counter
10+
while True: # Loop forever
11+
ctr = ctr + microbit.button_a.get_presses() # Count the amount of presses
12+
microbit.button_a.reset_presses()
13+
if ctr > 0: # Only execute if not zero
14+
microbit.display.scroll(str(ctr)) # Display the counter
15+
microbit.sleep(100) # Sleep for 100ms

0 commit comments

Comments
 (0)