from machine import Pin
from time import sleep_ms
# GPIO pins for segments A-G (change these to your wiring)
SEG_PINS = [0, 1, 2, 3, 4, 5, 6, ]
# GPIO pins for digit select (two digits)
DIGIT_PINS = [8, 9]
# Button pin (active low)
BUTTON_PIN = 15
# Setup pins
segments = [Pin(pin, Pin.OUT) for pin in SEG_PINS]
digits = [Pin(pin, Pin.OUT) for pin in DIGIT_PINS]
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
# 7-segment digit patterns for 0-9 (common cathode)
digit_patterns = [
0b00111111, # 0
0b00000110, # 1
0b01011011, # 2
0b01001111, # 3
0b01100110, # 4
0b01101101, # 5
0b01111101, #
0b01111111, # 8
0b01101111 # 9
]
# Operation list and index
operations = ['ADD', 'SUB', 'MUL', 'DIV']
current_op = 0
# Fixed input numbers
num1 = 4
num2 = 2
def display_digit(digit_index, value):
# Turn off both digits
for d in digits:
d.value(0)
# Set segments for the digit
pattern = digit_patterns[value]
for i in range(7): # Only A-G segments, ignore DP here
segments[i].value((pattern >> i) & 1)
segments[7].value(0) # DP off
# Turn on the selected digit
digits[digit_index].value(1)
def get_result():
global num1, num2, current_op
if operations[current_op] == 'ADD':
return num1 + num2
elif operations[current_op] == 'SUB':
return num1 - num2
elif operations[current_op] == 'MUL':
return num1 * num2
elif operations[current_op] == 'DIV':
return num1 // num2 if num2 != 0 else 0
def button_pressed():
# Simple debounce
if button.value() == 0:
sleep_ms(20)
if button.value() == 0:
while button.value() == 0:
pass # wait for release
return True
return False
def main_loop():
global current_op
while True:
if button_pressed():
current_op = (current_op + 1) % len(operations)
result = get_result()
# Clamp result to 0-99 for display
if result < 0:
result = 0
elif result > 99:
result = 99
# Display result by multiplexing digits
tens = result // 10
ones = result % 10
di