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.
- Open the project in BlocklyProp Solo, connect your board to your computer with the USB cable, and make sure the Launcher is running and can see the board's COMM port.
- Connect the external power supply to your your Activity Board WX, since the USB power does not supply enough current to run the servos.
- With the 2-position switch in position 1 to disable the servos, save the code to EEPROM.
- Hold up your Servo Youkai so the shoulders and hips can swivel freely, then put the switch in Position 2 and watch it spook!
How the Code Works
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.