from machine import Pin, ADC, PWM
import time
#Define GPIO pin numbers for seg1 and seg2
seg1_pins = [25, 32, 33, 26, 27, 12, 14]
seg2_pins = [2, 0, 4, 17, 5, 19, 18]
#Display init
seg1 = [Pin(pin, Pin.OUT) for pin in seg1_pins]
seg2 = [Pin(pin, Pin.OUT) for pin in seg2_pins]
#Define the mapping for each digit (0-9)
digits = [
[0, 0, 0, 0, 0, 0, 1], # 0
[1, 0, 0, 1, 1, 1, 1], # 1
[0, 0, 1, 0, 0, 1, 0], # 2
[0, 0, 0, 0, 1, 1, 0], # 3
[1, 0, 0, 1, 1, 0, 0], # 4
[0, 1, 0, 0, 1, 0, 0], # 5
[0, 1, 0, 0, 0, 0, 0], # 6
[0, 0, 0, 1, 1, 1, 1], # 7
[0, 0, 0, 0, 0, 0, 0], # 8
[0, 0, 0, 0, 1, 0, 0], # 9
]
def display_digit(digit, segment_pins):
for pin, state in zip(segment_pins, digits[digit]):
pin.value(state)
def read_pot_value():
pot_value = adc.read()
countdown_time_minutes = int(pot_value * 59 / 4095)
print("Countdown Time (minutes):", countdown_time_minutes)
return countdown_time_minutes
def read_switch_state():
return switch.value()
adc = ADC(Pin(13)) #Set pin 13 to analog
adc.atten(ADC.ATTN_11DB) #Analog to Digital
countdown_time = 0
last_pot_value = -1
last_change_time = time.ticks_ms() #Record Current Time!
buzzer_pin = Pin(16, Pin.OUT)
#Initialize slide switch Pin object
switch = Pin(34, Pin.IN, Pin.PULL_UP) # Adjust pin number as needed
#Initialize buzzer Pin object
buzzer_pwm = PWM(buzzer_pin)
buzzer_pwm.duty(0)
#Loooooooooop
while True:
switch_state = read_switch_state()
if switch_state == 1: # Slide switch is ON
countdown_time_minutes = read_pot_value()
display_digit(countdown_time_minutes // 10, seg1) # Display ten digit
display_digit(countdown_time_minutes % 10, seg2) # Display last digit
current_pot_value = adc.read()
if current_pot_value != last_pot_value:
last_pot_value = current_pot_value
last_change_time = time.ticks_ms()
else:
# If there is no change for 3000 ms (3 sec), start count down
if time.ticks_diff(time.ticks_ms(), last_change_time) >= 3000: # 3 secs
print("Starting countdown...")
while countdown_time_minutes >= 0:
# Read slide switch state
switch_state = read_switch_state()
# If switch is OFF, stop countdown
if switch_state == 0:
print("Countdown stopped by slide switch")
break
display_digit(countdown_time_minutes // 10, seg1) # Get the ten digit
display_digit(countdown_time_minutes % 10, seg2) # Get the last digit
time.sleep(60) # Sleep for 60 seconds (1 minute)
countdown_time_minutes -= 1
print("Countdown finished")
buzzer_pwm.freq(500)
buzzer_pwm.duty(512) #Set duty cycle to 50%
time.sleep(1) # Buzzer rings for 1 second
buzzer_pwm.duty(0)
last_pot_value = -1
time.sleep(3)
buzzer_pwm.freq(1000)
buzzer_pwm.duty(512)
time.sleep(1)
buzzer_pwm.duty(0)
else:
for pin in seg1 + seg2:
pin.off()
time.sleep(1) # Update display