The Woke | Chapter 3
- zuodan

- Jan 6, 2020
- 1 min read
The Code: Part I - Alarm
The most important feature of any alarm clock — waking the user up at designated times. This can be done using various methods, but we decided to take the conventional route with this prototype; continuously making loud obnoxious noises.
As such, I decided to write the code beginning with this feature.
Below is the initial code that I came up.
Code
import time from datetime import datetime def InitTime(): timeInit = input("Enter current time (24H-Clock)(HH:MM): ") if timeInit == "Now" or timeInit == "now": timeInit = datetime.now().strftime("%H:%M") timeH = timeInit[0:2] timeM = timeInit[3:] return timeH, timeM timeAlarm = input("Enter time to ring (24H-Clock)(HH:MM): ") (timeH, timeM) = InitTime() try: while True: timeN = timeH + ":" + timeM if timeN == timeAlarm: print("*****ALARM RINGS*****") print(timeN) time.sleep(60)
if int(timeM) < 59: timeM = str(int(timeM) + 1) if len(timeM) == 1: timeM = "0" + timeM else: timeM = "00" timeH = str(int(timeH) + 1) if int(timeH) == 24: timeH = "00" if len(timeH) == 1: timeH = "0" + timeH except KeyboardInterrupt: print("Cleaning up!")
Features
First, it prompts the user for the alarm time followed by the current time.
The program then enters a loop that will continuously display the new time every 60 seconds. The displayed time follows the 24 hour clock format.
However, the program does not continuously print "*****ALARM RINGS*****" until a condition is fulfilled. Rather, it prints it once and just continues with the loop.
This works well as a skeleton for the actual code; the print("*****ALARM RINGS*****") line can simply be replaced with other code in order to get a good ringing system. But for now, this is what I've come up with.
And with that, the code for just the alarm portion is complete! It is simple, but it gets the job done.
Next week I'll be interfacing an LCD display with the program to display the time on it, so stay tuned for that! ^_^

Comments