Have you ever killed a servo? This animated servo spirit - spooky or sweet - might come back to haunt you! This project uses most of the parts in the Parallax Animation Kit - Limited Edition. Though it is and minimalistic and quick to put together, the approach and code are easy to repurpose for other projects.
This project shows photos from a spooky spirit with a single eyeball and streamer draped arms, and also an angelic spirit with gauze-draped wings.
Parts List
Tools Used
For your protection practice safe handling of sharp tools. When working with hot glue, and tools with moving parts, always wear safety glasses!
The body of the Servo Spirit is made from the two Parallax Standard Servos in the Animation Kit. The hollow, transluscent plastic eyeball serves as a head, backlit by the Neopixel ring.
There is no need to build circuits on the breadboard for this project. Instead, it takes advantage of the Activity Board WX's 3-pin headers for the servos, and its D/A socket to drive the magnetic speaker.
Propeller I/O pin connections
Which wire is which? Before you wrap your jumper wires together with electrical tape, get your code built and tested, and then label each wire. This is especially handy if you plugged two wires together of different colors to make a longer lead.
If you used an eyeball and are going for a scary theme, as I did with mine, you might like this Servo Youkai code—download it below.
Project60123-Servo-Youkai.svg [4]
The code’s first task is to initialize the RGB LEDs. The rest of the code is in a repeat forever block.
This outermost infinite loop holds a repeat while check pin 3 block that monitors the PIR sensor. If motion is detected the PIR sends a high signal, and so this block returns a 1 (true) and the loop executes.
Once the PIR sensor detects motion, the first task is to set a color for the RGB LEDs, and then use the RGB-LED update block to turn them on. (It is really easy to forget that block and suspect your wiring job for no reason...)
Next, a repeat item block counts from 45 to 135 using a variable named up. The value of up increments the servo angles to swivel hips and shoulders. A frequency block scales the value of up into an audible range for the speaker, sending the signal to P26 (D/A 0 socket).
Then, another repeat item loop does much the same thing with a variable named down. This time however, down is subtracted from 180 to set the servo angles, so they start at 135 where they left off in the previous loop. Likewise, the frequency block both scales the value of down and subtracts it from 3375, the maximum frequency reached in the previous repeat item loop.
If the PIR sensor is no longer sensing motion, code execution exits the repeat while loop. Then the RGB-LED set and RGB-LED update blocks turn off the Neopixel ring. Your Servo Spirit is still, quiet, and dark until it senses motion again.
If you are going for something sweet, the Servo Angel code download below might be a good choice. Instead of a scary sound effect, it plays the first 8 notes in a tune called Antioch, perhaps better known as the carol "Joy To The World." You can change the note frequencies and durations for any tune you like; see the Circuit Practice with BlocklyProp - Piezo Beep tutorial [5] for more information.
Project63292-Servo-Angel.svg [6]
First, the code initializes and fills up two arrays with 8 elements each. JoyNotes holds a list of frequencies that will be played in order on the speaker. JoyTime holds a list of durations that determines how long to play each note, in milliseconds. These values will be used in the Sing function to play the tune.
Next, the code initializes the RGB LEDs in the Neopixel ring, noting it has 12 LEDs and its input is connected to P1.
Before we get to the main code, a new processor block launches the function "Sing." It goes in its own processor so the speaker can play the notes at the same time the servos move.
The rest of the code is in a repeat forever loop. First thing in the loop, the check PIN 3 block checks if the PIR sensor has detected motion. If it has, a 1 is stored in the PIR variable.
Next is an if...do condition. If the PIR variable holds a 1, the RGB LED set and RGB LED update blocks turn on the Neopixel ring. That is followed by two repeat loops. The first repeat item block counts from 45 to 135 using the variable named up. The value of up increments the servo angles to swivel hips and shoulders. The second repeat item block does much the same thing with a variable named down. This time however, down is subtracted from 180 to set the servo angles, so they start at 135 where they left off in the previous loop. The pause 20 in each block sets the appropriate servo signal refresh rate.
Afer the servos are done moving, or if the PIR variable was holding a 0 instead of a 1, the code executes the lower RGB LED set and RGB LED update blocks turn off the Neopixel ring. Then, the code starts over at the top of the repeat forever loop.
Meanwhile the Sing function is executing its own repeat forever loop in a separate processor. It also holds an if...do condition that starts by checking if the PIR variable holds a 1. If it does, a repeat loop increments the PlayJoy variable from 0 to 7, making 8 trips through the loop. PlayJoy is used as an index to select values from the JoyTime and JoyNotes arrays for the frequency block. PlayJoy starts at 0, because arrays start counting elements at zero, not at 1!
Links
[1] http://www.parallax.com/product/910-28088
[2] http://www.parallax.com/product/32912
[3] http://www.parallax.com/product/750-00009)
[4] https://learn.parallax.com/sites/default/files/content/AB-Blockly/Projects/Servo-Youkai/Project60123-Servo-Youkai.svg
[5] http://learn.parallax.com/tutorials/language/blocklyprop/circuit-practice-blocklyprop/piezo-beep
[6] https://learn.parallax.com/sites/default/files/content/AB-Blockly/Projects/Servo-Youkai/Project63292-Servo-Angel.svg
[7] https://solo.parallax.com/