from machine import Pin, I2C
from utime import sleep
# Initialize I2C on GP0 (SDA) and GP1 (SCL)
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=100000)
RTC_ADDR = 0x68
# Set the led for pin 16
led = Pin(16, Pin.OUT)
led.value(0) # Ensure LED starts turned off
sleep(0.01) # Wait for USB to connect
print("Hello, Pi Pico!")
def bcd_to_dec(bcd):
"""Convert Binary-Coded Decimal to standard decimal"""
return (bcd // 16) * 10 + (bcd % 16)
# Ask the user for input to turn on led
try:
target_hour = int(input("Enter target hour (0-23, e.g., 18 for 6 PM): "))
target_minute = int(input("Enter target minute (0-59): "))
print(f"Success! LED will turn on at {target_hour:02d}:{target_minute:02d}.")
except ValueError:
print("Invalid input! Defaulting to 18:00 (6 PM).")
target_hour = 18
target_minute = 0
# Ask the user for input to turn off led
try:
user_input_off = input("Enter the number of seconds before the LED turns off: ")
target_time_seconds_off = int(user_input_off)
print(f"Success! LED will turn off after {target_time_seconds_off} seconds.")
except ValueError:
print("Invalid input! Defaulting to 10 seconds.")
target_time_seconds_off = 10
elapsed_seconds = 0
led_has_turned_on = False
while True:
try:
# Read 7 bytes starting from register 0x00 (Seconds, Minutes, Hours, Day, Date, Month, Year)
data = i2c.readfrom_mem(RTC_ADDR, 0x00, 7)
sec = bcd_to_dec(data[0] & 0x7F) # Mask out the Clock Halt (CH) bit
minute = bcd_to_dec(data[1])
hour = bcd_to_dec(data[2] & 0x3F) # Assume 24-hour mode
date = bcd_to_dec(data[4])
month = bcd_to_dec(data[5])
year = bcd_to_dec(data[6]) + 2000
# Print the formatted date and time
print(f"Date: {year}-{month:02d}-{date:02d} | Time: {hour:02d}:{minute:02d}:{sec:02d} | Elapsed: {elapsed_seconds}s")
except OSError:
print("Failed to communicate with RTC. Check your wiring!")
hour, minute = 0, 0 # Fallback if I2C hiccups
# Check if the target clock time has arrived
if not led_has_turned_on and hour == target_hour and minute == target_minute:
led.value(1)
print("Target time reached! LED is now ON.")
led_has_turned_on = True
elapsed_seconds = 0 # Reset elapsed seconds so the off-timer starts counting from zero right now!
# Turn off led after set amount of time once it has turned on
if led_has_turned_on and elapsed_seconds >= target_time_seconds_off:
led.value(0)
print("Target time reached! LED is now off.")
break
# Increment the elapsed time counter by 1 second each loop (only matters after LED turns on, but safe to keep running)
if led_has_turned_on:
elapsed_seconds += 1
sleep(1)
print("Program has finished.")Loading
ili9341-cap-touch
ili9341-cap-touch