Make a Clock Timer

Image by Pascal on Flickr
Make a kitchen timer with this quick coding lab!
The great thing about computers is that they can substitute for a wide variety of other gadgets — especially if you can program them to do new things! You may already have a clock and countdown timer in the house, but what if you didn’t? With Mini Micro (a free app at https://miniscript.org/MiniMicro) and a few minutes, you can make one!
Launch Mini Micro, use the edit command to open the code editor, and then carefully type in Listing 1. Click the Close & Run button at the top of the editor, or exit the editor and type run at the prompt, to give it a try. If you get any errors, edit again and double-check your typing. Be sure to match the capitalization and punctuation shown. When working properly, you should get a menu that lets you choose between a clock that shows the current time, and a countdown timer. If you pick the countdown timer, you can tell the program how many minutes until it should sound the alarm.
The program is divided into several functions. The printHuge function on lines 4-7 prints a short string of text, but does so using the “large” font, and scales the graphics layer to five times its normal size. The result is huge text that can be easily seen from across the room. Try changing color.green to something like color.lime or color.pink to get a brighter display.
Next, the doClock function (lines 9-14) runs if you pick the clock option at the menu. This method is pretty simple, because the dateTime.now method does all the work of getting the current time as a string. The code “HH:mm:ss” specifies that we want hours, minutes, and seconds. If you prefer your clock without seconds, just remove the “:ss” part. Or, to get 12-hour time instead of 24-hour time, change “HH” to “h”.
The doTimer function (lines 16-30) has to work a bit harder. It asks the user how many minutes they want, multiplies by 60 to convert that into seconds, and adds it to the current time. This is stored in a variable called endTime. Then within the loop, we can calculate secLeft (seconds left) by subtracting time from endTime. This works because time is actually a function that returns the number of seconds since the program began. When there is no time left, it prints a message and plays a sound on lines 21-22. Feel free to change the message here, or load a different alarm sound on line 2 (use dir “/sys/sounds” to explore your options).
Finally, the main loop on lines 32-41 prints out the options, waits for the user to make a choice, and then runs the appropriate function (or breaks out of the loop).
Next time you’re baking cookies, boiling eggs, or just need a nice big clock, consider firing up Mini Micro and using your very own custom clock/timer. Happy coding!
import "dateTime" alarm = file.loadSound("/sys/sounds/twinkle.wav") } printHuge = function(s) clear; gfx.scale = 5 gfx.print s, 96 - s.len * 10, 54, color.green, "large" end function } doClock = function while not key.available or key.get != char(27) printHuge dateTime.now("HH:mm:ss") wait end while end function } doTimer = function endTime = input("How many minutes? ").val * 60 + time while not key.available or key.get != char(27) secLeft = round(endTime - time) if secLeft < 0 then printHuge "TIME UP!" alarm.play else minutes = floor(secLeft / 60) seconds = secLeft % 60 printHuge minutes + ":" + ("00" + seconds)[-2:] end if wait end while end function } while true clear print "Select function:" print " 1. Clock" print " 2. Countdown Timer" choice = key.get if choice == "1" then doClock if choice == "2" then doTimer if choice == "q" or choice == char(27) then break end while
Listing 1. Clock/Timer program.
Learn More
https://www.youtube.com/watch?v=0lV3qRXCpbc
Create a Countdown Clock and Stopwatch Timer
https://www.youtube.com/watch?v=eWb3xZG_YSI
Making a timer in Scratch
Also In The August 2022 Issue

This method of detecting marine waste is truly out of this world!

Looking for free computing resources for your classroom? Check out Computing at School!

Never be late again with the help of this neat coding project!

From bulletproof vests to stronger tires, Kevlar is a really versatile material!

Time to learn how humans have kept track of time over the centuries

Check out how programmers are competing to make an even simpler Wordle!

Your dreams of having a robotic friend may be closer than you think!

Check out some of the cool ways virtual reality is being used in medicine!

A sandy problem requires a sandy solution!

Learn how you can use retro game design to help kids embrace their creativity!

Make a kitchen timer with this quick coding lab!

Perlin noise is as random as it gets!

Leave your mark on the world wide web with your own website!

cURL up on the couch and check out this cool open-source tool!

You know leap forward and fall back, now add a little kick with a leap Second

Check out some of the cool tech being invented for pets!

Discover the true origins of the great poop emoji!

Pokemon in Minecraft? Tell me more!

Collection of the Learn More links from all of the August 2022 stories, in one place.

Interesting stories about computer science, software programming, and technology from August 2022.