Santa Scrollbot....

If you have a Scroll Bot kit you can add some festive touches and make a Santabot to count down the time left until ... that day. You know, that one. Alternatively, you could change the date in the code and have it count down to your birthday, the next time you get to see someone special, or until the end of the world (dates may vary).

santa baby

How do you do a countdown in Python?

Thankfully this is fairly straightforward, because again there is a really handy library we can use, called datetime. It can tell you the date and the time down to the microsecond, which is a millionth of a second. More about it here.

If you want to find out the current local time now, for instance, the Python code is:

datetime.datetime.now()

To find out how long it is until a certain date, you can write:

datetime.datetime(2017, 12, 25) - datetime.datetime.now()

You can add further information, if you like, so if you used (2017, 12, 25, 14) it would calculate the time until 2pm on Christmas Day.

Coding the Countdown

import datetime
import time
import signal
import scrollphathd
from scrollphathd.fonts import font5x7

We need a lot of things! datetime is for the countdown, and all of the others are to make the Scroll pHAT HD scroll the results as a readable display.

Next we need to do a bit of tweaking. When the message scrolls, we are usually just scrolling the same thing over and over again. With this, it will be updating as it scrolls. This is the code equivalent of jumping into a moving plane, only thankfully slightly easier to complete at home.

str_len = 0
scroll_x = 0

We need to set up how long the string (message) is to start with, and from what position it will start scrolling.

We'll come back to the position later - for now onwards with the actual message!

while True:
    xmas = datetime.datetime(2017, 12, 25) - datetime.datetime.now()
    daysleft = xmas.days
    hoursleft = xmas.seconds/3600

It's important that the calculation is in a loop, otherwise it will only work out how long is left on the countdown once, and never update!

The code above defines the time until Christmas by doing the calculation we talked about earlier, then uses this to work out what that is in days and hours.

Now to make the Scroll Bot display this information, festively

thunder, thunder, thundercats!

First off, make sure the Scroll pHAT HD doesn't blind us by setting the brightness, and clear anything that may have been there before.

scrollphathd.set_brightness(0.5)
scrollphathd.clear()

And now... for the longest line I've ever written.

str_len = scrollphathd.write_string("Ho ho ho! It's %i days and %i hours until xmas!" %(daysleft, hoursleft), x=0, y=0, font=font5x7)
scrollphathd.scroll_to(scroll_x, 0)
scrollphathd.show()
time.sleep(0.05)
scroll_x += 1
if scroll_x >= str_len:
    scroll_x = 0

Remember the bit about jumping into a moving plane? This is it. The code is looking to see if the string has changed, and keeping tabs on where the scrolling has got to.

The code is finished now, so time to print the beard and hat, or other accessories for your Scroll Bot.

verily and lo it was festive

I held mine on with Blu Tack, but you could innovate.

Here's the full code for copy-and-paste joy:

import datetime
import time
import signal
import scrollphathd
from scrollphathd.fonts import font5x7

str_len = 0
scroll_x = 0

while True:
    xmas = datetime.datetime(2017, 12, 25) - datetime.datetime.now()
    daysleft = xmas.days
    hoursleft = xmas.seconds/3600

    scrollphathd.set_brightness(0.5)
    scrollphathd.clear()

    str_len = scrollphathd.write_string("Ho ho ho! It's %i days and %i hours until xmas!" %(daysleft, hoursleft), x=0, y=0, font=font5x7)
    scrollphathd.scroll_to(scroll_x, 0)
    scrollphathd.show()
    time.sleep(0.05)
    scroll_x += 1
    if scroll_x >= str_len:
        scroll_x = 0

Shopping basket

Need something for this project? You can use the links below to add products to your Pimoroni Shop basket for easy checkout.

Raspberry Pi Zero W
Zero W only £9.60
Raspberry Pi Zero W
Zero W + Adaptors £14.00
Raspberry Pi Zero W
Zero W + Adaptors + Pibow Zero W £19.00
Official Raspberry Pi Universal Power Supply
£8.00
Scroll pHAT HD
£12.00
Scroll Bot - Pi Zero W Project Kit
£35.00
Want to checkout or change something? Click here to view your cart.

Tutorial
Beginner
Pi Zero, Festive Makes, LEDs

Tanya Fish

tanya@pimoroni.com
@tanurai
http://tanyafishmakes.wordpress.com
Formerly, Tanya worked in various Sheffield schools, teaching maths and physics and shouting loudly at paperwork. Now a crew member at Pimoroni - working on learning materials for schools, workshops, and lasering anything she can get her hands on. Gets very giddy at most things STEAM.