Saturday, October 13, 2012

Halloween Speaking Pumpkin using Arduino chip

Halloween pumpkin
 Source: http://www.ladyada.net/make/pumpkin/pumpkin.html


This is a modified version of the project that is a little 'accellerated' for easy videoing
Here is a quick project for an electronic halloween pumpkin. With a bit of hacking a $1 plastic pumpkin is upgraded: a sensor embedded in the nose detects when people get close and will play scarey sounds and animates LEDs on the face. The sounds are stored on an SD card so its easy to change and customize what the pumpkin says, while the code is written for an Arduino so its easy to modify the behavior. I'm going to have this pumpkin outside my door to freak out the little kids who go to daycare nearby. Boo!
What you will need
This isn't a particularly tough project, but you'll need to know basic Arduino/Microcontroller programming and soldering
Sound effects
There are tons of CDs and mp3 packages available for purchase that contain a large assortment of sounds. Here are some free download sites that you can scour:
Here is my collection (in a zip file) of converted sounds: they're ready to go and have been tweaked to improve the sound where possible
Step 1: Playing sounds through the speaker
Assemble the Wave Shield kit and get your Arduino board up & running. Copy the zip of example halloween wave's onto the SD card and run the DAP example sketch. Each sound should play in sequence. Look at the debug log if there are any issues.
Next, solder two wires to the speaker and connect them to the shield as shown, you should be able to hear the effects pretty well
If you want a power boost, you can 'piggyback' another amplifier chip (TS922) on top of the existing one. This will approximately double the maximum volume
Step 2: Choose and connect sensor
Next, we will connect up a distance sensor. There are two basic 'flavors' of distance sensing: infrared and sonar. IR works better for close range (about a meter or more) and sonar works better for longer ranges (up to 10 meters)

Sonar and IR sensors
Since I want this pumpkin to activate from a few meters away, I'm going with a sonar sensor. The nicest one I've seen so far is the Maxbotics EZ1 which has both digital and analog signal outputs, runs from a range of voltages and can detect more than 6 meters away. If you're using a different sonar, the code may be different so you will have to first get that device working and then come back once you have functional interface code
The maxbotix is easy to use. Connect three wires: Power, Ground and Analog signal as shown.
Finally connect the sonar to the shield so that the analog signal feeds into Analog In #0 and the power and ground wires are connected to power and ground.
Load up the sketch above into the Arduino. Note the following chunks of code.
In this bit we read 8 seperate measurements from the sonar and average them. This avoids one or two oddball readings:
void loop() { 
...
   distsensor = 0;
   for (i=0; i<8 8="8" analogread="analogread" codefragment="codefragment" delay="delay" distsensor="distsensor" ensor="); Serial.println(distsensor);
...</pre>
      </blockquote>
    </div>

    <p>After that we have some 'repeating' if-then statements. Here, we 
see how large the reading was. A reading of 500 indicates more than 6 
meters away and in that case we will pick a random number between 0 and 
199 and if its 0 (1/200 times) then we'll play one of three scary sounds</p>
    <div class=" i="i" putstring_nl="putstring_nl">
      
...
     if (distsensor > 500) {
     ...
     pumpkinstate = 1;
     // nobody there. one out of 200 times play one of the scary sounds (once every few minutes)
     i = random(200);
     if (i == 0) {
       i = random(3);
       if (i == 0) {
           playcomplete("CACKLE.WAV");
       } else if (i == 1) {
           playcomplete("GOSTMOAN.WAV");
       } else {
           playcomplete("CATSCREM.WAV");   
       }
     }
...
The 'pumpkinstate' variable is used to keep track of how far the target was last we checked. This will let us know if they are getting closer or farther away over time.
Next get yourself one of those $1 plastic pumpkins from your local drug store.
They are hollow, making them perfect for stashing electronic wiring!
Cut a hole in the nose so that the sonar can be press-fit in
Set up the sonar so that it is pointing at a large open space (the readings are consistantly over 500) Then experiment with walking near and around it. Adjust the randomness if you want to make it more or less noisy. You can also change what audio files get played when.
Step 3. Adding LEDs
Its not a real electronics project without some blinky LEDs! Lets add some LEDs for the pumpkins eyes and mouth.
Punch 5mm holes in the eyes and mouth. In this case I'm using 2 for the eyes and 5 for the mouth.
Next heat up a hot glue gun and secure the LEDs so they dont slip out. In general I dont suggest hot glue for -anything- but in this case its OK.
Next its time to wire up the LEDs. I'm going to control the eye LEDs at once and divide the mouth into three symmetric sections so that I can do some simple animations with the mouth. Since red LEDs have a voltage of about 2 volts and the Arduino runs at 5V, I can stack two at a time to simplify wiring:
The LEDs are controlled from the analog in pin that aren't used - don't forget that they can also be used as digital input or outputs! The 2 eyes are connected to analog pin #4, the two outer mouth LEDs to analog #5, the middle-outer mouth LEDs to #2 and the one remaining LED from the center goes to analog #3. (It doesn't particularly matter which pins go to which LEDs, just that this ended up being what I did)
I used some leftover ribbon cable to do the wiring. Watch that you don't burn your fingers or the pumpkin!
If you have a handy 5v power supply kicking around, you can test the LEDs - I used my benchtop supply
Step 4. Wiring up the lights
Finally connect the LEDs to the Arduino as indicated in the schematic
The plan is to make the pumpkin's mouth animate with the sound. Although we could do this all in 'software' it turns out to be a heck of a lot easier to just use an analog input to measure the output of the amplifier. So connect a wire from the 1.5K resistor back to analog input #1
Now lets make some blinky! There is code in the sketch that will turn on the eye LEDs when someone gets near:
...
   if (distsensor <= 500) {
     digitalWrite(eyeleds, HIGH); 
   } 
   if (distsensor > 500) {
     digitalWrite(eyeleds, LOW);  
...

And then in the wave playing code, we will animate the pumpkin. First we take a few readings from the analog sensor. The audio signal is centered around 2.5 (ranges from 0 to 5) so we subtract 512 (half of the maximum analog read value) and then invert it if it is negative. This gives the 'absolute value' volume. Then we take the maximum value of 8 reads. Then depending on how loud it is we light up the mouth. You can mess with the values a bit if you want to have a different effect.
...
while (wave.isplaying) {
   volume = 0;
   for (i=0; i<8 br="br" i="i">     v2 = analogRead(1) - 512;
     if (v2 < 0) 
        v2 *= -1;
     if (v2 > volume)
       volume = v2;
     delay(5);
   }
   if (volume > 200) {
     digitalWrite(outermouthleds, HIGH);
   } else {
     digitalWrite(outermouthleds, LOW);
   }
   if (volume > 150) {
     digitalWrite(midmouthleds, HIGH);
   } else {
     digitalWrite(midmouthleds, LOW);
   } 
   if (volume > 100) {
     digitalWrite(mouthleds, HIGH);
   } else {
     digitalWrite(mouthleds, LOW);
   } 
   //putstring("vol = "); Serial.println(volume, DEC);
  }
...
I wrote a version of the sketch that is a little 'accellerated' for easy videoing. But basically it will look and act just like this
Step 5. Finishing up
Time to wrap up the project. You'll have to decide how to power the pumpkin. If you want to have it be fully contained you can use a battery pack. Either make one from 4 AA batteries or use a Mintyboost kit. Basically you'll want about 4.5-6V
Or you can cut a small hole in the back of the pumpkin and use a 9-12V DC adapter.
Finally, use a plastic bowl (preferrably orange, all I had in the kitchen was a blue one!) and put some candy in there! (Like leftover altoids gum)
Happy halloween!

No comments: