import machine
import time
#######################################
# Pin and constant definitions
#######################################
SEG_PINS = [0, 1, 2, 3, 4, 5, 6] # GPIO pins for the seven segments (a, b, c, d, e, f, g)
DP_PIN = 7 # GPIO pin for the decimal point
DIG_PINS = [11, 10, 9, 8] # GPIO pins for each digit (1, 2, 3, 4)
ANALOG_PIN = 26 # ADC pin for analogue components (potentiometer)
BUTTON_PIN = 16 # Pushbutton pin
#######################################
# Global variables
#######################################
last_button_press_time = 0
debounce_delay = 200 # Debounce delay in milliseconds
max_voltage = 3.3 # Maximum voltage of the ADC input
#######################################
# Segment mapping for digits 0-9
#######################################
segment_map = {
'0': [1, 1, 1, 1, 1, 1, 0],
'1': [0, 1, 1, 0, 0, 0, 0],
'2': [1, 1, 0, 1, 1, 0, 1],
'3': [1, 1, 1, 1, 0, 0, 1],
'4': [0, 1, 1, 0, 0, 1, 1],
'5': [1, 0, 1, 1, 0, 1, 1],
'6': [1, 0, 1, 1, 1, 1, 1],
'7': [1, 1, 1, 0, 0, 0, 0],
'8': [1, 1, 1, 1, 1, 1, 1],
'9': [1, 1, 1, 1, 0, 1, 1],
' ': [0, 0, 0, 0, 0, 0, 0], # Blank
}
#######################################
# Function definitions
#######################################
# Function to read the ADC pin and
# convert the digital value to a voltage level in the 0-3.3V range
def read_analog_voltage(adc):
sum_values = 0
num_samples = 16 # Number of samples to average
for _ in range(num_samples):
sum_values += adc.read_u16() # Read ADC value as a 16-bit unsigned integer
average_value = (sum_values / num_samples) * max_voltage / 65535 # Convert ADC reading to voltage
print("Analog Voltage: {:.2f} V".format(average_value)) # Print voltage in volts
return average_value
# Function to display a digit on the seven-segment display
def display_digit(digit, position):
# Set all digit pins to high (disable all digits)
for pin in DIG_PINS:
machine.Pin(pin, machine.Pin.OUT).value(1)
# Set the appropriate segments for the digit
segments = segment_map[digit]
for i in range(7):
machine.Pin(SEG_PINS[i], machine.Pin.OUT).value(segments[i])
# Enable the current digit
machine.Pin(DIG_PINS[position], machine.Pin.OUT).value(0)
# Function to display the voltage on the seven-segment display
def display_voltage(voltage):
voltage_str = "{:.2f}".format(voltage) # Format voltage as a string with 2 decimal places
voltage_str = voltage_str.replace('.', '') # Remove the decimal point for display
if len(voltage_str) < 4:
voltage_str = ' ' * (4 - len(voltage_str)) + voltage_str # Zero-fill to ensure 4 characters
# Display each digit on the seven-segment display
for position, digit in enumerate(voltage_str):
display_digit(digit, position)
time.sleep(0.005) # Small delay to ensure each digit is displayed
# Function to setup GPIO/ADC pins and polling loop
def setup():
global last_button_press_time
last_button_press_time = 0
# Initialize ADC pin and other necessary pins
adc = machine.ADC(machine.Pin(ANALOG_PIN))
button = machine.Pin(BUTTON_PIN, machine.Pin.IN, machine.Pin.PULL_UP)
# Initialize seven-segment display pins
for pin in SEG_PINS + DIG_PINS:
machine.Pin(pin, machine.Pin.OUT).value(0)
voltage = 0 # Initialize the voltage variable
# Main loop for polling button and updating display
while True:
# Poll button press
if button.value() == 0: # Button pressed (low)
current_time = time.ticks_ms()
if current_time - last_button_press_time > debounce_delay: # Debounce delay
voltage = read_analog_voltage(adc)
last_button_press_time = current_time
# Continuously refresh the display
display_voltage(voltage)
time.sleep(0.001) # Adjust delay as needed to avoid flickering
if __name__ == '__main__':
setup()