# imports
from machine import Pin,ADC,Timer
import time
import math
#######################################
# Pin and constant definitions
button =Pin(16,Pin.IN,Pin.PULL_DOWN)
BUTTON_COUNT = 1
BUTTON_start = 2
BUTTON_LIST = []
BUTTON_PRESSED = []
adc = ADC(26)
dig_1 = 11
dp = 14
#######################################
displayCodes = [
0xC0, #0 -> 0b11000000
0xF9, #1
0xA4, #2
0xB0, #3
0x99, #4
0x92, #5
0x82, #6
0xF8, #7
0x80, #8
0x90 #9
]
#######################################
# Global variables
#######################################
display_value = 0
seven_segment_value = 0
timer = Timer()
#######################################
def interrupt_callback(pin):
# Function definitions
for i in range(BUTTON_COUNT):
button_pin = machine.Pin(BUTTON_start + i, machine.Pin.IN, machine.Pin.PULL_DOWN)
BUTTON_LIST.append(button)
button_pin.irq(trigger=machine.Pin.IRQ_FALLING, handler=interrupt_callback)
# Function to read the ADC pin
adc = machine.ADC(26)
# 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
raw_value = adc.read_u16()
voltage = (raw_value / 65535) * 3.3
display_value = int(voltage * 100)
return voltage
# 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
def seven_segment():
raw_sensor = seven_segment_value
for i in range(3, -1, -1):
seven_segment(int(temp_display_value%10), i, i==0)
time.sleep(0.5)
Pin(dig_1 + i, Pin.OUT).off()
Pin(dp, Pin.OUT).value(1)
temp_display_value //= 10
def display_digit(digit_value, digit_index, dp_enable=False):
for i in range(7):
Pin(i, Pin.OUT).value((1<<i) & displayCodes[digit_value])
Pin(dig_1 + digit_index, Pin.OUT).on()
if dp_enable:
Pin(dp, Pin.OUT).value(0)
def read_value():
global seven_segment_value
seven_segment_value = adc
def main():
while True:
seven_segment()
read_value()
# Display the value stored in the display_value global variable
# on available 7-segment displays
def scan_display(timer_int):
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):
for i in range(7): #0->7
Pin(i, Pin.OUT).value((1<<i) & displayCodes[digit_value])
Pin(dig_1 + digit_index, Pin.OUT).on()
if dp_enable:
Pin(dp, Pin.OUT).value(0)
# Function to test avaiable 7-segment displays
def display_value_test():
for i in range(100, 401):
global display_value
display_value = i
time.sleep(0.05)
# Function to setup GPIO/ADC pins, timers and interrupts
def setup():
read_analogue_voltage()
enable_display_timer()
def display_value_test():
while True:
read_analogue_voltage()
time.sleep(0.1)
if __name__ == '__main__':
setup()