dark mode light mode Search Menu
Search

Make a Clock Timer

Pascal on Flickr

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

https://www.youtube.com/watch?v=fkaoRl2BcQ4

Related Posts