-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryCounter.py
executable file
·63 lines (55 loc) · 1.81 KB
/
binaryCounter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'''binaryCounter.py
June 14, 2021
Author: Larry Priest email: larrytpriest@gmail.com
My binary counter. Displays binary count-up on a string of n LED's.
Hardware:
RaspberryPi Pico
breadboard or proto/perf board
n 220 ohm resistors, n 5mm red LED's
pin 1/GP0 is the High bit in my wiring.
'''
import board
import digitalio
import time
TOTALBITS = 15 # start at zero
delay = 0.25 # seconds
count = 0xffff # any interger (hex 'cause it makes sense)
LEDbits = []
class binaryCounter():
def __init__(self):
# Setup LED list
subcommand = 'LEDbits.append(digitalio.DigitalInOut(board.GP'
for i in range(TOTALBITS+1):
fullcommand = subcommand + str(i) + '))'
exec(fullcommand)
LEDbits[i].direction = digitalio.Direction.OUTPUT
# main loop
def bicounter():
for i in range(count):
counterBits = bin(i)[2:] # leading zeros missing
# byte = format(i, '08b') # gives full 8 bits
print(counterBits) # reality check
byte_list = []
for bit in iter(counterBits):
byte_list.append(bit)
list.reverse(byte_list) # gits the list to the right
bit_count = TOTALBITS # reverse if u wired the other way up
for i in iter(byte_list):
if i == '1':
LEDbits[bit_count].value = True
else:
LEDbits[bit_count].value = False
bit_count -= 1
time.sleep(delay)
def bicounterClose():
print('Closing outputs.')
for i in range(len(LEDbits)):
LEDbits[i].deinit()
if __name__ == '__main__':
binaryCounter()
try:
binaryCounter.bicounter()
except KeyboardInterrupt:
pass
finally:
binaryCounter.bicounterClose()