This project will build off of the Teathering CodeBug with Python and Raspberry Pi Controlled CodeBug with I2C projects. Rather than taking advantage of the Blockly interface and emulation plug in on http://www.codebug.co.uk. we'll be bringing some Python code into the mix.
Follow the steps below to get started on this advanced project Thomas Macpherson-Pope pulled together from CodeBug.org.uk
What you need:
Before we get started on this project we will need to make sure we have followed the 10 CodeBug Projects in 10 Days: Raspberry Pi Controlled CodeBug with I2C project. We'll also need to make sure to have the CodeBug connected in the correct position on the Raspberry Pi's GPIO before attempting this project.
NEVER connect the Micro USB or use a battery with CodeBug while it is fitted to a Raspberry Pi.
We can create some amazing projects, by controlling the CodeBug with Python using the Raspberry Pi’s I2C port.
This guide will walk through how to create a scrolling digital CodeBug clock.
Your Python program needs to get the time and date from the Raspberry Pi and scroll it on CodeBug’s display.
Open a Terminal and start a new Python program by typing:
Enter the following code into the file and save it:
import codebug_i2c_tether import datetime import time # make a connection with CodeBug cb = codebug_i2c_tether.CodeBug() cb.open() # function for scrolling messages on CodeBug’s display def scroll_message(message): length = len(message) for i in range(0,length*-5,-1): cb.write_text(i, 0, message, direction="right") time.sleep(.15) while True: # get the time and scroll it time_str = datetime.datetime.now().strftime("%H:%M:%S") scroll_message(" Time " + time_str) # get the date and scroll it date_str = datetime.datetime.now().strftime("%d/%m/%Y") scroll_message(" Date " + date_str)
Next, lets test the clock by entering the following command into a Terminal:
Exit the program by pressing Ctrl C.
If the CodeBug is displaying the wrong time, we'll need to change the timezone on the Raspberry Pi or update the time by being connected to the Internet. To change the timezone we can open a terminal and type:
Using the arrow keys to navigate through the list and select Internationalization Options then select Change Timezone and select the region and closest large city.
We can run the clock automatically whenever we turn on the Raspberry Pi (even without logging in)!
To do this we'll need to edit a system file with this command:
sudo nano /etc/rc.local
Enter the following line directly
above exit 0 line:
python3 /home/pi/codebug_i2c_clock.py &
Now restart the Raspberry Pi and the clock will begin to run before you log in.
When we want the clock to stop running we can do so by typing:
Next, we'll find the process that has python3 /home/pi/codebug_i2c_clock.py in the end column, and take a note of its process id (from the second column). Let's run:
sudo kill -9 your-process-id
sudo kill -9 2887
If we want to remove the clock, we can simply edit the rc.local file and remove the line we just inserted:
sudo nano /etc/rc.local
Now that the clock is set up, let's think about other ways to program the clock. How about an alarm clock? Weather report? Let us know how you would apply this project!
See more CodeBug projects and learn how you can get one of your own by visiting: 10 CodeBug Projects in 10 Days