# This script is a sketch for a UV light sterilization project.
# It uses a display to show the time remaining, a relay module to turn on the
# LEDs, and a button to operate all the components.
# == IMPORTS ==
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
from time import sleep_ms
# Required imports for the script.
# == COMPONENTS DEFINITIONS ==
relay = Pin(19, Pin.OUT) # Relay module setup
button = Pin(5, Pin.IN, Pin.PULL_DOWN) # Button setup
screen = I2C(0, scl=Pin(1), sda=Pin(0), freq=200000) # I2C screen interface setup
pix_res_x = 128
pix_res_y = 64
oled = SSD1306_I2C(pix_res_x, pix_res_y, screen)
# Screen resolution and OLED display initialization.
time_duration = 90
# == FUNCTION TO DISPLAY THE COUNTDOWN ==
def countdown_display(oled):
for i in range(time_duration, -1, -1):
oled.fill(0) # Clear the screen.
oled.text(str(i), 5, 5) # Display the current time.
oled.show() # Update the display.
sleep_ms(1000) # Pause for 1 second.
oled.fill(0)
relay.value(0) # Turn off the relay.
oled.fill(0)
oled.show()
sleep_ms(500)
# Runs the countdown and updates the display.
current = new = button.value()
# Main loop that continuously monitors the button state,
# activates the relay and countdown when the button is pressed.
while True:
new = button.value()
if new != current:
current = new
if new == 1:
relay.value(1) # Relay turns on
countdown_display(oled) # Start countdown display
sleep_ms(10)