# imports
from machine import ADC, Pin
import math
import time
#######################################
# Pin and constant definitions
SEVEN_SEGMENT_START_PIN = 0
DISPLAY_COUNT = 4
DECIMAL_PRECISION = 4
ANALOGUE_INPUT_PIN = 26
BUTTON_PIN = 16
last_button_press_time = 0
#######################################
# HEX values for 7 segment display values
digit_list_hex = [
0x40, # 0
0x79, # 1
0x24, # 2
0x30, # 3
0x19, # 4
0x12, # 5
0x02, # 6
0x78, # 7
0x00, # 8
0x10, # 9
0x08, # A
0x03, # B
0x46, # C
0x21, # D
0x06, # E
0x0E, # F
0x7F # Empty
]
#######################################
# Global variables
#######################################
display_value = 0
segment_pins = []
display_select_pins = []
current_display_index = DISPLAY_COUNT -1 # to keep track of which digit is currently being displayed
display_timer = None
#######################################
# 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
sum_values = 0
num_samples = 16
for _ in range(num_samples):
sum_values += pin.read_u16()
average_value = (sum_values / num_samples) * 3.3 / 65535
voltage_mv = int(average_value * 1000)
print("Analog Voltage: {} mV".format(voltage_mv))
# Function to disable timer that triggers scanning 7 segment displays
def disable_display_timer():
global display_timer
display_timer.deinit()
# Function to enable timer that triggers scanning 7 segment displays
def enable_display_timer():
global display_timer
display_timer.init(period=30, mode=machine.Timer.PERIODIC, callback=scan_display)
# 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 current_display_index, display_value
# Extract the digit corresponding to the current display index
digit = int((display_value // math.pow(10, current_display_index))) % 10
# Display the digit,
# enable the decimal point if the current digit index equals to the set decimal precision
display_digit(digit, current_display_index,
current_display_index == DECIMAL_PRECISION and 0 != DECIMAL_PRECISION)
# Move to the next display
current_display_index = (current_display_index - 1)
if current_display_index < 0:
current_display_index = DISPLAY_COUNT -1
# 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):
if digit_value < 0 or digit_value > len(digit_list_hex):
return
for pin in display_select_pins:
pin.value(1)
mask = digit_list_hex[digit_value]
for i in range(7):
segment_pins[i].value((mask >> i) & 1)
segment_pins[7].value(0 if dp_enable == False else 1)
if digit_index == -1:
for pin in display_select_pins:
pin.value(0)
elif digit_index >= 0 and digit_index < DISPLAY_COUNT:
display_select_pins[digit_index].value(0)
# Function to test avaiable 7-segment displays
def display_value_test():
global display_value
disable_display_timer()
current_display_index = 0
for i in range(0, len(digit_list_hex)):
display_digit(i, -1, i % 2 != 0)
time.sleep(0.5)
for i in range(0, len(digit_list_hex)):
display_digit(i, DISPLAY_COUNT - 1 - (i % DISPLAY_COUNT), True)
time.sleep(0.5)
display_digit(16, -1, False)
enable_display_timer()
def count_display_value(timer_int):
global display_value
display_value += 1
if display_value > 9999:
display_value = 0
#print(display_value)
for i in range(8):
segment_pins.append(machine.Pin(SEVEN_SEGMENT_START_PIN + i, machine.Pin.OUT))
for i in range(DISPLAY_COUNT):
display_select_pins.append(machine.Pin(SEVEN_SEGMENT_START_PIN + i + 8, machine.Pin.OUT))
analogue_voltage_pin = machine.ADC(machine.Pin(ANALOGUE_INPUT_PIN))
display_timer = machine.Timer()
enable_display_timer()
# Function to setup GPIO/ADC pins, timers and interrupts
def setup():
global last_button_press_time
last_button_press_time = 0
adc = machine.ADC(machine.Pin(ANALOGUE_INPUT_PIN))
button = machine.Pin(BUTTON_PIN, machine.Pin.IN, machine.Pin.PULL_UP)
while True:
if button.value() == 0:
current_time = time.ticks_ms()
if current_time - last_button_press_time > 200:
read_analogue_voltage(adc)
last_button_press_time = current_time
time.sleep_ms(10)
if __name__ == '__main__':
setup()
#display_value_test()