Our scrolling badge program

Let's get straight on with writing the program for our scrolling badge. In this example, I am going to use my name, but the text that you have scrolling across your badge is really up to you. If your Pi Zero was online at the time you were wearing your badge, you could have this badge fetch the latest news from your favorite news feed and scroll that across, or even the weather where you are at the time.

To display text across our Scroll pHAT HD, we will make use of the scroll() function. Create a new file by typing nano scrollBadge.py. In your Nano text editor, type the following code.

Take care when entering the lines inside the loop, as the number of spaces should match. I personally use four spaces for each level of indentation in my Python programs.

I have added comments into the code using the hash symbol to mark the start of each comment. They should explain what each step is doing:

#!/usr/bin/python3

import scrollphathd as shd #import our Scroll pHAT library
from time import sleep #import sleep from the time library

shd.clear() #clear any LEDs which are already lit
shd.show() #must call show after clear
shd.write_string("Hello, my name is Jon Witts! ", brightness=0.25)
#The message to scroll with 5 spaces at the end
#the brightness sets the LEDs to 25%

while True: #We start a while loop which goes on forever
shd.show() #Show message on the LEDs
shd.scroll(1) #Scroll along the x-axis 1 pixel at a time
sleep(0.05) #Wait for 0.05 of a second before returning
#to the start of the loop

Save your file by pressing Ctrl + O, followed by Enter, and then exit Nano by pressing Ctrl + X. Again, we now need to make our file executable by typing chmod +x ./scrollBadge.py; and then run it by typing ./scrollBadge.py.

All being well, you should see your message being scrolled across the LEDs on your Scroll pHAT HD. To stop your program from running, press Ctrl + C together. This program only really scratches the surface of what you can do with the Scroll pHAT HD. If you would like to know more then head over to the official documentation at http://docs.pimoroni.com/scrollphathd/.

So, we now have our message working on our Scroll pHAT HD, but we still have some work to do to be able to wear this as a badge!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.22.169