import network
import time
from machine import Pin, ADC, SoftI2C
from utime import sleep
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
# Function to connect to Wi-Fi
def connect_to_wifi():
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
# Wi-Fi connection
connect_to_wifi()
# Initialize I2C for the LCD
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)
i2c_addresses = i2c.scan()
print("I2C addresses found:", i2c_addresses)
if not i2c_addresses:
raise Exception("No I2C devices found")
lcd_address = i2c_addresses[0]
lcd = I2cLcd(i2c, lcd_address, 2, 16)
#potentiometer
potentiometer = ADC(Pin(34))
potentiometer.atten(ADC.ATTN_11DB)
#button
button = Pin(25, Pin.IN, Pin.PULL_UP)
# State variable
verification_done = False
# Function to read potentiometer and display value on LCD
def display_potentiometer_value():
global verification_done
while not verification_done:
if button.value() == 0: # Button pressed
pot_value = potentiometer.read()
set_hours = (pot_value / 4095.0) * 12
lcd.clear()
lcd.putstr("Hours set to {:.0f}H".format(set_hours))
sleep(1) # Display for 1 second
verification_done = True
else:
pot_value = potentiometer.read()
lcd.clear()
lcd.putstr("Set hours {:.0f}H".format((pot_value / 4095.0) * 24)) # Lets you choose the hours with the potentiometer
sleep(0.5)
# Call the function to start displaying the potentiometer value
display_potentiometer_value()
# Stop refreshing LCD and end program after verification
lcd.clear()
print("Verification complete. Hours set to {:.0f}H".format(set_hours))