Maker.io main logo

Make a Cute Alarm Clock with Mood Light

2022-07-26 | By Pimoroni

License: See Original Project

Courtesy of Pimoroni

Guide by Pimoroni

So, you've got your mood light kit... this tutorial shows you how to make your ‎mood light into an alarm clock that goes yellow when it's time to get up and ‎stays a subdued shade of indigo when it's sleeping time.‎

Useful for children who haven't learned to tell the time yet, for those who ‎hate noisy alarms, or just because you can, you can use the principles of ‎scheduling events to use it as an activity timer, or just a reminder that you ‎need to take a break.)‎

Starting the Code

First off, we're going to need three things - the instructions for talking to the ‎Unicorn pHAT, a way of telling the time, and a way of making things happen ‎when we want.‎

Copy Code
import time
import unicornhat
import schedule

Luckily, Daniel Bader wrote a whole user-friendly library we can use, ‎called Schedule, and that's what I'm going to use here. Other libraries are ‎available! :)‎

If you haven't heard of it or used it before, you'll probably want to install it, so ‎go to a Terminal and type:‎

Copy Code
sudo pip install schedule

‎... which should install everything you need!‎

Back to the code. We'll need two parts, one that schedules what to do when, ‎and one that makes the Unicorn pHAT light up in the right color. Let's start ‎with the colors.‎

Copy Code
unicornhat.set_layout(unicornhat.PHAT)
unicornhat.rotation(0)
unicornhat.brightness(0.5)

So... we've told the Unicorn HAT code that it's only pHAT sized, that it's the ‎right way up, and that we don't want to be blinded by the brightness, thank ‎you very much! If you find it's too bright or too dull, you can change the ‎brightness overall here, or in the color definitions if you like.‎

Now we need to set some rules. We're going to make a set of instructions for ‎making it yellow, and another one for making it indigo, so we can just bring ‎them in with a quick word later.‎

Setting Up the Instructions for Colors

Yellow is (255, 255, 0) in the red, green, blue color system. Indigo is ‎approximately (51, 0, 51).‎

colors_1

If we want all of the pixels to light up the same color, we just use the ‎command set_all and the color we want. As a precaution, I've added in two ‎more lines to make sure it clears the display before changing it to the new ‎color. If you wanted the yellow to be brighter than the indigo, you could ‎change it here.‎

Copy Code
def yellow ():
unicornhat.clear()
unicornhat.show()
unicornhat.set_all(255, 255, 0)
unicornhat.show()

def indigo ():
unicornhat.clear()
unicornhat.show()
unicornhat.set_all(51, 0, 51)
unicornhat.show()

Making It Do Things at the Right Time

Now the scheduler comes in. We want it to go yellow when it's time to wake ‎up, and blue when it's time to go to bed.‎

Copy Code
schedule.every().day.at("07:30").do(yellow)
schedule.every().day.at("19:00").do(indigo)

‎Reading through that, it's quite easy to see that every day at 7.30am I have ‎asked it to go yellow (get up time), and every day at 7pm I have asked it to ‎go indigo (bedtime).‎

If you wanted to change the times, or add in more colors for more events, ‎you just alter the times in brackets, but remember to use the 24-hour clock. If ‎you want it to do another task, add in another line for the scheduler, but don't ‎forget to define what you want it to do, like we did with the yellow and indigo!‎

Making It Check to See What To Do

Finally, to finish off the program, we need to make sure that it keeps ‎checking if it has to do something.‎

Copy Code
while True:
schedule.run_pending()
time.sleep(10)

So... while the program is running, it will set off anything with the right time, ‎checking every ten seconds to see if anything has changed. You could make ‎it check more often if you want something bang on time to the second, just ‎change the number in the sleep brackets.‎

What If I Want to Unplug It?‎

No problem, use Cron to run your program when the Pi Zero W switches on - ‎there's a guide here.‎

Why is Yours in a Cute Box?‎

I re-used the box the Mood Light Kit comes in - it's sold separately as ‎a Pirate-Brand Loot Box as well. I cut a small hole in one side for the power ‎cable to go in and drew a little picture on some plain paper to go in the front ‎and diffuse the light.‎

box_2

box_3

box_4

I cut the hanging tab off the box with scissors.‎

tab_5

Here is the inside of the box, without the picture in.‎

inside_6

More Ideas

How about making a cute case for the light if you want something a bit ‎different?‎

Print a coloring sheet and use it inside a Ferrero Rocher chocolate box (it's a ‎shame, but someone has to eat them first). Paint the inside top with a mix of ‎PVA glue and white paint to diffuse the light.‎

Use the box the Mood Light Kit came in! You could decorate the outside of ‎the box with stickers, pom-poms, gems, whatever takes your fancy and really ‎claim ownership of it!‎

For a more grown-up take, what about using different lights or patterns to ‎show your daily schedule?‎

If you work from home, keep work and home separate by having work time / ‎you time different colors.‎

For more information on Schedule, have a look at its GitHub page.‎

The Full Code for Cut-and-Paste Joy

Copy Code
import time
import unicornhat
import schedule

unicornhat.set_layout(unicornhat.PHAT)
unicornhat.rotation(0)
unicornhat.brightness(0.5)

def yellow ():
unicornhat.clear()
unicornhat.show()
unicornhat.set_all(255, 255, 0)
unicornhat.show()

def indigo ():
unicornhat.clear()
unicornhat.show()
unicornhat.set_all(51, 0, 51)
unicornhat.show()

schedule.every().day.at("07:30").do(yellow)
schedule.every().day.at("19:00").do(indigo)

while True:
schedule.run_pending()
time.sleep(10)

That's all folks!‎

制造商零件编号 PIM498
UNICORN HAT MINI
Pimoroni Ltd
制造商零件编号 PIM259
MOOD LIGHT PI ZERO W PROJECT KIT
Pimoroni Ltd
制造商零件编号 SC1176
SBC 1.0GHZ 4 CORE 512MB RAM
Raspberry Pi
制造商零件编号 SC0218
AC/DC WALL MNT ADAPTER 5.1V 15W
Raspberry Pi
Add all DigiKey Parts to Cart
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.