# ----------------------------------------
# LAB5-03 (7-Segments LED)
# ----------------------------------------
from machine import Pin
from utime import sleep
#Common anode 7-segment display digit patterns
digits = [
[0, 0, 0, 0, 0, 0, 1, 1], # 0
[1, 0, 0, 1, 1, 1, 1, 1], # 1
[0, 0, 1, 0, 0, 1, 0, 1], # 2
[0, 0, 0, 0, 1, 1, 0, 1], # 3
[1, 0, 0, 1, 1, 0, 0, 1], # 4
[0, 1, 0, 0, 1, 0, 0, 1], # 5
[0, 1, 0, 0, 0, 0, 0, 1], # 6
[0, 0, 0, 1, 1, 1, 1, 1], # 7
[0, 0, 0, 0, 0, 0, 0, 1], # 8
[0, 0, 0, 1, 1, 0, 0, 1], # 9
[0, 0, 0, 1, 0, 0, 0, 1], # a
[1, 1, 0, 0, 0, 0, 0, 1], # b
[0, 1, 1, 0, 0, 0, 1, 1], # C
[1, 0, 0, 0, 0, 1, 0, 1], # d
[0, 1, 1, 0, 0, 0, 0, 1], # E
[0, 1, 1, 1, 0, 0, 0, 1], # F
]
# a, b, c, d, e, f, g, dp
gpio = (15, 4, 18, 19, 21, 22, 23, 5) # gpio pins
pins = [] # list empty
for i in range(0, len(gpio)):
pins.append(Pin(gpio[i], Pin.OUT)) # adds an item to list.
switch = Pin(13,Pin.IN) # Create input pin on GPIO12
def reset():
"""Turns off all segments on the 7-segment display."""
for pin in pins:
pin.value(1)
reset()
while True:
if switch.value() == 1:
# Ascending counter
for i in range(len(digits)):
if switch.value() == 0:
break
for j in range(len(pins) - 1):
pins[j].value(digits[i][j])
sleep(0.5)
else:
# Descending counter
for i in range(len(digits) - 1, -1, -1):
if switch.value() == 1:
break
for j in range(len(pins)):
pins[j].value(digits[i][j])
sleep(0.5)