Quantcast
Channel: Raspberry Pi Spy » Pi-Lite
Viewing all articles
Browse latest Browse all 5

Happy Halloween With A Raspberry Pi And Pi-Lite

$
0
0

It’s that time of year when mad inventors start creating Halloween themed electronics projects. I’ve never managed to be organised to do a seasonal project but this year I decided to give it a try.

So this year I decided to take what I’ve learnt about the Pi-Lite and create a simple Halloween project I can stick in the window along side our more traditional carved pumpkins.

This post follows on from my previous Pi-Lite articles :

TPi-Lite Happy Halloween Exampleo re-create this project you’ll need :

  • Raspberry Pi
  • Pi-Lite
  • SD card (I used the latest Raspbian image)
  • Power supply
  • My Python script

Python Script

The following script will display an animated object and then scroll “Happy Halloween !” across the screen. It repeats until CTRL-C is pressed.

#!/usr/bin/env python

import serial
import time

# Define lists which contain frames of animations
face   = ['000000000000000000010011000011000100011000110000010110000110110000110110000010110011000110011000100010011000000000000000000000',
          '000000000000000000010000011011000110011000110000010110000110110000110110000010110011000110011000110010000011000000000000000000']
bat    = ['010000000011000000001100000000110000110110000011111011001111100001111100011111011110110000000110000001100000011000000010000000',
          '000000100000001100000011000000110000110110001011111010001111100001111100011111010110110001000110000000011000000001100000000100']
ghost  = ['000000000000000000000011111001100001010000010100100010100000001100100001100000010010000010001100001000011111000000000000000000',
          '000000000000000000000011111001100010010000010100000001100100001100000010100100010010000001001100001000011111000000000000000000']
spider = ['001001000010010010100100101010101000001011110000111001000111000000111000000111001001011110010101000100100101010010010001001000',
          '010010010100100101101001000100101010011011101000111000000111000000111000000111000011011101100101010101001000100100101010010010']

# Combine into dictionary object
objects = {'face':face,'bat':bat,'ghost':ghost,'spider':spider}

# Define text message to scroll
message = "Happy Halloween !\r"

# Configure Pi serial port
s = serial.Serial()
s.baudrate = 9600
s.timeout = 0
s.port = "/dev/ttyAMA0"

def showObject(name,count,delay):
  # Function to display frames from a specified object
  # name  - object to display
  # count - number of times to display object
  # delay - ms to wait between frames
  object = objects[name]
  for x in range(count):
    for frame in range(len(object)):
      command = "$$$F" + object[frame] + "\r"
      s.write(command)
      time.sleep(delay)
  # Clear display
  s.write("$$$ALL,OFF\r")

try:
    # Open serial port
    s.open()
except serial.SerialException, e:
    sys.stderr.write("could not open port %r: %s\n" % (port, e))
    sys.exit(1)

# Turn off all LEDs
s.write("$$$ALL,OFF\r")
time.sleep(0.5)

try:

  while True:
    # Get list of object names from dictionary
    mykeys = objects.keys()

    # Loop through each object
    for x in range(len(mykeys)):
      showObject(mykeys[x],5,0.5)
      time.sleep(1)
      s.write(message)
      # Wait for scrolling message
      time.sleep(8)

except KeyboardInterrupt:
  print("Quit")

Here is a video of the script in action :

Script Downloads

Rather than cutting-n-pasting the above script you can also download it directly to your Pi using :

wget https://bitbucket.org/MattHawkinsUK/rpispy-pi-lite/raw/master/pi_lite_happy_halloween.py

Or if you use Git on your Pi grab all my Pi-Lite example scripts by cloning my Pi-Lite repository on BitBucket :

git clone https://bitbucket.org/MattHawkinsUK/rpispy-pi-lite.git

Modifications

There are lots of ways you could modify and improve this script. You can change the scrolling message but make sure you adjust the “time.sleep” delay to match. There are more details about how I estimate this time in my hello world scrolling text post. As a rough estimate it is 0.5 seconds per character in your message.

You could also add other objects by defining new lists and adding them to the dictionary. To generate the 126 character string for each frame you can use my 14×9 LED Matrix Sprite Generator (opens in new window/tab).

Happy Halloween!


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images