import machine
import math
import time
from machine import ADC, Timer, Pin
seven_segment_pins = [machine.Pin(i, machine.Pin.OUT) for i in range(7)]
dp_pin = machine.Pin(7, machine.Pin.OUT)
seven_segment_digit_pins = [machine.Pin(i, machine.Pin.OUT) for i in range(8, 12)]
adc_pin = machine.ADC(28)
button_pin = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_UP)
button_pressed = False
display_value = 0
seven_segment_timer = machine.Timer(-1)
seven_segments_values = [
0xC0,
0xF9,
0xA4,
0xB0,
0x99,
0x92,
0x82,
0xF8,
0x80,
0x90,
]
debounce_period = 200
last_button_press = time.ticks_ms()
def read_analogue_voltage(pin):
global display_value
adc_value = adc_pin.read_u16()
voltage = adc_value * (3.3 / ((2**16) - 1))
print('voltage', voltage)
display_value = voltage
def disable_display_timer():
global seven_segment_timer
seven_segment_timer.deinit()
def enable_display_timer():
global seven_segment_timer
seven_segment_timer.init(mode=Timer.PERIODIC, freq=33, callback=scan_display)
def scan_display(timer_int):
global display_value
value = int(display_value * 1000)
for i in range(4):
digit = value % 10
dp_enable = (i == 3)
display_digit(digit, i, dp_enable)
value = value // 10
def display_digit(digit_value, digit_index, dp_enable=False):
for i in range(4):
seven_segment_digit_pins[i].value(0)
for i in range(7):
seven_segment_pins[i].value((seven_segments_values[digit_value] >> i) & 0x01)
dp_pin.value(not dp_enable)
seven_segment_digit_pins[digit_index].value(1)
def display_value_test():
global display_value
while True:
read_analogue_voltage(adc_pin)
time.sleep(0.5)
def button_handler(pin):
global button_pressed
global last_button_press
if time.ticks_ms() - last_button_press > debounce_period:
button_pressed = True
last_button_press = time.ticks_ms()
read_analogue_voltage(adc_pin)
print('button press')
def setup():
global button_pin
enable_display_timer()
button_pin.irq(trigger=Pin.IRQ_FALLING, handler=button_handler)
if __name__ == '__main__':
setup()
while True:
time.sleep(1)