from machine import Pin, WDT
from time import sleep
# Create a Watchdog Timer with a 2-second timeout
watchdog_timer = WDT(timeout=2000)
# Define the LED on pin 2
led = Pin(2, Pin.OUT)
# Define the button on pin 4
btn = Pin(12, Pin.IN, Pin.PULL_UP)
while True:
try:
# Turn on the LED for 1 second
led.value(1)
sleep(1)
# Turn off the LED for 1 second
led.value(0)
sleep(1)
# Reset the Watchdog Timer
watchdog_timer.feed()
# Check the button state
if btn.value() == 0:
print('Error!')
# Attempt to divide by zero to trigger an error
x = 0 / 0
except ZeroDivisionError:
# Handle the division by zero error
print("Caught a division by zero error!")
# Reset the Watchdog Timer
watchdog_timer.feed()