I'm pretty well on with the build, but looking at my calendar (I'm off for a week, and then in Rome for the Maker Faire as soon as I get back) I'm starting to think that it'll be a bit close to the bone in terms of getting it finished for Halloween. So I was hoping I could solicit a bit of assistance from you guys, who are invariably better with coding than I am! I'm pretty good with the hardware, but I fumble when it comes time to code.
Here's the project, followed by a specification I was hoping you guys could help me out with for programming the actual CodeBug.
Ghostbusters Trap - Desk Lamp
I'm a sucker for the pop culture references, and I've been working building a ghost trap from Ghostbusters, complete with working trap door mechanism, LED lighting, a footswitch and a miniature smoke machine. You know the one I mean (WARNING: don't look directly into the trap):
This will all be run by a CodeBug, of course, and I've had a degree of success in proving its ability to drive a servo to open the doors. However, the code I used seems a bit flaky (sometimes works, sometimes doesn't -- not sure if it's the CodeBug or the servo, to be honest), and I'd rather it didn't run the servo backwards and forwards if you hold the button down.
The actual trap is coming along very nicely -- it's mostly built, with a 5V power supply, and the servo in place to open and close the trap doors. I've hacked up an e-cig cartomizer along with a small priming pump to create the fog (using a blend of water and sugar glycerine to create the smoke -- no nicotine or strange poisons involved!), and I've butchered three ultra-cheap LED torches to get a very neat array of 27 white LEDs, all of which I'll post in a build along when it's all up and running.
But here's where I could use a little assistance...
Who Ya Gonna Code?
My efforts to pull everything together into a reasonably optimised block of code for the CodeBug have been limited, and time's against me. So here's what I'd like the CodeBug to do, which I was hoping someone with better coding chops could help out with.
On Power Up
Right at the outset, the CodeBug should set the servo's PWM to 170 via Leg 1, which closes the trap doors. This is just in case the doors were open when the trap was powered off.
Turning It On
The trap is turned on using a latching foot switch (first press closes the switch, second press opens it again). I was thinking of using Leg 0 as an input, which is grounded by the latching foot switch for the duration of the trap being on (on = doors open, lights on). When the foot switch is pressed (or latched) the following should happen, in this order:
- The fog machine is switched on for 5 seconds to generate a bit of atmospheric smoke. This will be achieved by switching a 5V relay and/or a transistor via Leg 3, as the fog machine takes more current than I suspect the CodeBug could handle (it gets a little hot).
- After five seconds, the PWM for the servo is set to 60 (opening the trap doors on top), which outputs via Leg 1. (Leg1 is connected to the signal lead on the servo).
- At the same time, the LEDs are switched on using a 5V relay and/or a transistor via Leg 4.
- The following animation is displayed on the CodeBug: http://www.codebug.org.uk/explore/codebug/1240/
At this point, the LEDs remain lit, the above animation cycles, and the doors remain open until the foot switch is pressed again.
Turning It Off
Once the foot switch is unlatched by pressing it a second time, Leg 0 is no longer grounded and the following happens in this order.
- The PWM for the servo is set to 170 (closing the trap doors on top), which outputs via Leg 1.
- The fog machine is switched on for 5 seconds to generate a bit of more smoke that'll seep out of the trap (as if there was a ghost inside).
- The LEDs are turned off.
- The following animation is displayed on the CodeBug for around two minutes: http://www.codebug.org.uk/explore/codebug/1241/
Ps. The full build along is coming soon!