I’ve had a bit of a change around this weekend, shuffling the various boxes in my media TV unit and generally fiddling purely for the sake of it. One of the more interesting changes I made was to put my RaspBMC media centre Pi into the Short Crust case that was sent to me by PiSupply.com, along with the PiLITEr board I had in my bits box.
#!/usr/bin/python
#
# PiLITEr scanner script by the "Average Man"
#
# Check out http://AverageManVsRaspberryPi.com for more PiLITEr code examples
#
#import
import RPi.GPIO as GPIO
import time
import os
#Turn off annoying warnings
GPIO.setwarnings(False)
# Define PiLITEr to GPIO mapping
LED1 = 7
LED2 = 11
LED3 = 13
LED4 = 12
LED5 = 15
LED6 = 16
LED7 = 18
LED8 = 22
# Main program
def main():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED1, GPIO.OUT) #Set GPIO pin to output (to 'give' power)
GPIO.setup(LED2, GPIO.OUT) #Set GPIO pin to output (to 'give' power)
GPIO.setup(LED3, GPIO.OUT) #Set GPIO pin to output (to 'give' power)
GPIO.setup(LED4, GPIO.OUT) #Set GPIO pin to output (to 'give' power)
GPIO.setup(LED5, GPIO.OUT) #Set GPIO pin to output (to 'give' power)
GPIO.setup(LED6, GPIO.OUT) #Set GPIO pin to output (to 'give' power)
GPIO.setup(LED7, GPIO.OUT) #Set GPIO pin to output (to 'give' power)
GPIO.setup(LED8, GPIO.OUT) #Set GPIO pin to output (to 'give' power)
#Set scanner speed
speed = 0.1
#Scanner Element
while 1:
#LED sequence down
GPIO.output(LED1,GPIO.HIGH)
time.sleep(speed)
GPIO.output(LED1,GPIO.LOW)
GPIO.output(LED2,GPIO.HIGH)
time.sleep(speed)
GPIO.output(LED2,GPIO.LOW)
GPIO.output(LED3,GPIO.HIGH)
time.sleep(speed)
GPIO.output(LED3,GPIO.LOW)
GPIO.output(LED4,GPIO.HIGH)
time.sleep(speed)
GPIO.output(LED4,GPIO.LOW)
GPIO.output(LED5,GPIO.HIGH)
time.sleep(speed)
GPIO.output(LED5,GPIO.LOW)
GPIO.output(LED6,GPIO.HIGH)
time.sleep(speed)
GPIO.output(LED6,GPIO.LOW)
GPIO.output(LED7,GPIO.HIGH)
time.sleep(speed)
GPIO.output(LED7,GPIO.LOW)
GPIO.output(LED8,GPIO.HIGH)
time.sleep(speed)
#LED sequence up
GPIO.output(LED8,GPIO.LOW)
GPIO.output(LED7,GPIO.HIGH)
time.sleep(speed)
GPIO.output(LED7,GPIO.LOW)
GPIO.output(LED6,GPIO.HIGH)
time.sleep(speed)
GPIO.output(LED6,GPIO.LOW)
GPIO.output(LED5,GPIO.HIGH)
time.sleep(speed)
GPIO.output(LED5,GPIO.LOW)
GPIO.output(LED4,GPIO.HIGH)
time.sleep(speed)
GPIO.output(LED4,GPIO.LOW)
GPIO.output(LED3,GPIO.HIGH)
time.sleep(speed)
GPIO.output(LED3,GPIO.LOW)
GPIO.output(LED2,GPIO.HIGH)
time.sleep(speed)
GPIO.output(LED2,GPIO.LOW)
if __name__ == '__main__':
main()
Rather than updating the number in each instance of ‘time.sleep(1)’ I’ve used a global speed setting by defining the name ‘speed’ with the time, and just used ‘time.sleep(speed)’ across the script instead. This saves a lot of time when changing the speed of the scanner light.
Starting the Script at Reboot with Cron
In short, and in this context, it allows me to run scripts at set times, perfect for what we’re doing here as I want this script to run at reboot.
Cron is usually straight forward, however additional steps are needed with RaspBMC due to the way the build is set up. By default, Cron is disabled, so let’s sort that bit out first (you won’t have to do this with Raspbian usually).
Let’s enable Cron first. Go to the settings directory by using the following command:
cd /home/pi/.xbmc/userdata/addon_data/script.raspbmc.settings/
Now that you’re in the right directory, open the settings file for editing (using nano):
sudo nano settings.xml
Navigate to the following line and change ‘false’ to ‘true’
false" />
It should now look like this:
true" />
Press the usual ‘Ctrl + X’ and save the file under the same name.
That’s Cron enabled, so now we just need to tell Cron which script to start up on reboot. To do this, use the following command to open up Cron for editing:
sudo crontab -e
You should now see this screen:

Cron, in all its glory
Ignore all the text in there already, just tab down to the bottom and add a new line like I have here. The line needs to point to your script – mine is in a directory called ‘piliter’ as you can see in the code below and in the screenshot:
@reboot python /home/pi/piliter/test.py &
