How it works...

The first function, readMessageFile(), will open and read the contents of a given file. This will then use processFileContent() to return a Python dictionary containing the corresponding patterns for the letters defined in the file provided. Each line in the file is processed, ignoring any line containing a # character and checking for " characters to indicate the name of the LED pattern that follows after. After the file has been processed, we end up with a Python dictionary that contains LED patterns for the '_', 'C', 'B', 'K', and 'O' characters:

'_': [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] 
'C': [[0, 1, 1, 1, 0], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1]] 
'B': [[1, 1, 1, 1, 1], [1, 0, 1, 0, 1], [0, 1, 0, 1, 0]] 
'K': [[1, 1, 1, 1, 1], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1]] 
'O': [[0, 1, 1, 1, 0], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [0, 1, 1, 1, 0]] 

Now that we have a selection of letters to choose from, we can create a sequence of LED patterns using the createBuffer() function. As the name suggests, the function will build up a buffer of LED patterns by looking up each letter in the message and adding the related pattern row by row. If a letter isn't found in the dictionary, then a space will be used instead.

Finally, we now have a list of LED patterns ready to display. To control when we start the sequence, we will use the TILT module and check the status of the tilt switch:

The tilt switch position when not moving (left) and moving (right)

The tilt switch consists of a small ball bearing enclosed in a hollow, insulated cylinder; the connection between the two pins is closed when the ball is resting at the bottom of the cylinder. The tilt switch is open when the ball is moved to the other end of the cylinder, out of contact of the pins:

The tilt switch circuit with the switch closed and with the switch open

The tilt switch circuit shown previously will allow GPIO pin 24 to be connected to the ground when the switch is closed. Then, if we read the pin, it will return False when it is at rest. By setting the GPIO pin as an input and enabling the internal pull-up resistor, when the tilt switch is open, it will report True.

If the tilt switch is open (reporting True), then we will assume the unit is being moved and begin displaying the LED sequences, incrementing the current position each time we display a row of the LED pattern. Just to make the pattern a little more colorful (just because we can!) we repeat each row in another color. As soon as the TILT.tilt_moving() function reports that we have stopped moving or that we are moving in the opposite direction, we will reset the current position, ready to start the whole pattern all over again:

The message is displayed by the RGB LEDs - here, we are using green and blue together

When the RGB LED module and tilt switch are moved back and forth, we should see the message displayed in the air!

Try experimenting with different color combinations, speeds, and arm waviness to see what effects you can produce. You could even create a similar setup mounted on a wheel to produce a continuous POV effect.

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

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