# imports
from machine import Pin, ADC
import math
import time
#######################################
# Pin and constant definitions
#######################################
button_pin = Pin(16, Pin.IN, Pin.PULL_UP)
reader_pin = 26
start_7seg_pin = 0
digit_count = 4
digit_list = [
[0,0,0,0,0,0,1], # 0
[1,0,0,1,1,1,1], # 1
[0,0,1,0,0,1,0], # 2
[0,0,0,0,1,1,0], # 3
[1,0,0,1,1,0,0], # 4
[0,1,0,0,1,0,0], # 5
[0,1,0,0,0,0,0], # 6
[0,0,0,1,1,1,1], # 7
[0,0,0,0,0,0,0], # 8
[0,0,0,0,1,0,0] # 9
]
digit_list_test = [ #This should show you "HI ('')" ← this is a face
[1,1,0,0,1,0,1,1],
[1,0,1,0,0,1,1,0],
[1,1,1,1,0,0,1,1],
[1,0,0,1,0,0,0,1],
]
#######################################
# Global variables
#######################################
display_value = 0
digit_pins = []
seg_pins = []
#######################################
# Function definitions
#######################################
# Function to read the ADC pin and
# to convert the digital value to a voltage level in the 0-3.3V range
# This function updates the value of the display_value global variable
def read_analogue_voltage(pin): # We can use the pin parameter if needed
global display_value
display_value = ADC(pin).read_u16()*3.3/65535
pass
# Function to disable timer that triggers scanning 7 segment displays
def disable_display_timer():
pass
# Function to enable timer that triggers scanning 7 segment displays
def enable_display_timer():
pass
# Function to handle scanning 7 segment displays
# Display the value stored in the display_value global variable
# on available 7-segment displays
def scan_display(timer_int):
global display_value
while True:
if button_pin.value() == 0:
read_analogue_voltage(reader_pin)
for i in range(0, digit_count):
voltage = str((display_value * pow(10, 2 - i) - int(display_value * pow(10, 2 - i))) * 10)
display_digit(int(voltage[0:1]), i)
time.sleep(.05)
pass
# Function display the given value on the display with the specified index
# dp_enable specifies if the decimal pooint should be on or off
def display_digit(digit_value, digit_index, dp_enable = False):
digit_pins[digit_index].value(1)
for s in range(7):
seg_pins[s].value(digit_list[int(digit_value)][s])
seg_pins[7].value(dp_enable if digit_index == 3 else 1)
digit_pins[digit_index].value(0)
for s in range(8):
seg_pins[s].value(1)
pass
# Function to test avaiable 7-segment displays
def display_value_test():
global digit_list_test
while True:
for i in range(digit_count):
digit_pins[i].value(1)
for s in range(7):
seg_pins[s].value(digit_list_test[i][s])
digit_pins[i].value(0)
for s in range(7):
seg_pins[s].value(1)
time.sleep(.01)
pass
# Function to setup GPIO/ADC pins, timers and interrupts
def setup():
for i in range(start_7seg_pin, start_7seg_pin + 8):
pin = Pin(i, Pin.OUT, value=1)
seg_pins.append(pin)
for i in range(start_7seg_pin + 8, start_7seg_pin + 8 + digit_count):
pin = Pin(i, Pin.OUT)
digit_pins.append(pin)
pass
if __name__ == '__main__':
setup()
button_pin.irq(trigger=Pin.IRQ_FALLING, handler=scan_display)
#display_value_test()