import machine
import time
import tm1637
from neopixel import NeoPixel
import network
import urequests
import ntptime
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!")
display = tm1637.TM1637(clk=machine.Pin(13), dio=machine.Pin(12))
display.numbers(99, 99, colon = True)
pin = machine.Pin(5, machine.Pin.OUT)
num_leds = 12
# Create a NeoPixel object with the pin and number of neopixels
np = NeoPixel(pin, num_leds)
for i in range(num_leds):
np[i] = (0, 0, 255)
np.write()
button1 = machine.Pin(21, machine.Pin.IN, machine.Pin.PULL_UP)
button2 = machine.Pin(19, machine.Pin.IN, machine.Pin.PULL_UP)
button3 = machine.Pin(18, machine.Pin.IN, machine.Pin.PULL_UP)
rtc = machine.RTC()
rtc.datetime((2000, 1, 1, 0, 0, 0, 00, 0))
print(time.localtime())
ntptime.settime()
time.sleep(2)
dstadjust = 1
time.sleep(2)
alarm_hour, alarm_minute = time.localtime()[3:5]
alarm_hour = alarm_hour + 1
print("Alarm time set to:", alarm_hour, ":", alarm_minute)
display.numbers(alarm_hour, alarm_minute, colon = True)
while True:
# Check button 1 to increment hours
if not button1.value():
alarm_hour = (alarm_hour + 1) % 24
display.numbers(alarm_hour, alarm_minute, colon = True)
print("Alarm time set to:", alarm_hour, ":", alarm_minute)
time.sleep(0.5)
# Set all neopixels to green
for i in range(num_leds):
np[i] = (0, 0, 255)
# Update the neopixels to show the new brightness level and color
np.write()
# Check button 2 to increment minutes
if not button2.value():
alarm_minute = (alarm_minute + 1) % 60
print("Alarm time set to:", alarm_hour, ":", alarm_minute)
display.numbers(alarm_hour, alarm_minute, colon = True)
time.sleep(0.5)
# Set all neopixels to green
for i in range(num_leds):
np[i] = (0, 0, 255)
# Update the neopixels to show the new brightness level and color
np.write()
# Check button 3 to activate alarm
if not button3.value():
# Calculate the number of seconds until the alarm
now = time.localtime()
current_hour = now[3] + dstadjust
current_minute = now[4]
if alarm_hour < current_hour or (alarm_hour == current_hour and alarm_minute < current_minute):
# If the alarm time is earlier than the current time, set the alarm for tomorrow
seconds_until_alarm = ((alarm_hour + 24) * 3600) + (alarm_minute * 60) - (current_hour * 3600) - (current_minute * 60) - now[5]
else:
seconds_until_alarm = (alarm_hour * 3600) + (alarm_minute * 60) - (current_hour * 3600) - (current_minute * 60) - now[5]
time.sleep(0.5)
#show an alarm
print ("alarm activated")
display.scroll("alarm activated", delay=200)
display.numbers(alarm_hour, alarm_minute, colon = True)
# Set all neopixels to green
for i in range(num_leds):
np[i] = (255, 0, 0)
# Update the neopixels to show the new brightness level and color
np.write()
# Wait until the alarm time
time.sleep(seconds_until_alarm)
# Give the alarm
print("ALARM!")
# Set all neopixels to green
for i in range(num_leds):
np[i] = (0, 255, 0)
# Update the neopixels to show the new brightness level and color
np.write()