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 let's make some blinky! There is code in the sketch that will turn on the eye LEDs when someone gets near:
Download: file
...
   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.
Download: file
...
while (wave.isplaying) {
   volume = 0;
   for (i=0; i<8; 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:
This guide was first published on Sep 28, 2013. It was last updated on Sep 28, 2013. This page (Step 4: Wiring up the lights) was last updated on Jan 22, 2020.