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.
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.
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:
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 colour. Let's start with the colours.
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 colour 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.
Yellow is (255, 255, 0) in the red, green, blue colour system. Indigo is approximately (51, 0, 51).
If we want all of the pixels to light up the same colour, we just use the command
set_all and the colour 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 colour. If you wanted the yellow to be brighter than the indigo, you could change it here.
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()
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.
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 (bed time).
If you wanted to change the times, or add in more colours 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!
Finally, to finish off the program, we need to make sure that it keeps checking if it has to do something.
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.
No problem, use Cron to run your program when the Pi Zero W switches on - there's a guide here.
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.
I cut the hanging tab off the box with scissors.
Here is the inside of the box, without the picture in.
How about making a cute case for the light, if you want something a bit different?
Print a colouring 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 colours.
For more information on Schedule, have a look at its GitHub page.
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)
Need something for this project? You can use the links below to add products to your Pimoroni Shop basket for easy checkout.
Mood Light - Pi Zero W Project Kit
Raspberry Pi Zero W
Raspberry Pi Zero
Pi Zero W Starter Kit
Official Raspberry Pi Universal Power Supply