PROJECT: Sound Sensor LED Strip Gauge
Step 1 - Build the Project
Using an LED Strip and a sound sensor, create a sound gauge that lights up more pixels when it's loud and fewer pixels when it's quiet.
Step 2 - Upload the Code
Step 3 - Read the Walkthrough
Above the setup, create an integer variable named numPixels. This will stand in for the number of LED pixels on your LED strip. Any time you use the integer numPixels, it will act as if you typed 15 in that place.
When you're using an LED strip, you need to tell the library about that strip. In the line LEDStrip strip = LEDStrip(numPixels, 12, 11), you are telling the library "I'm using an LEDStrip and I want to name it 'strip'. The strip has 'numPixels' number of pixels and its data pin is connected on pin 12. Its clock pin is connected on pin 11. That is called creating an object and it links the hardware to the library.
In the setup() section of the code, set the pinMode of the sound sensor to INPUT, so it sends a signal in to the Maker Board instead of waiting for commands from Maker Board.
In the loop of the code, first create two integers to hold the values of the minimum sound and maximum sound you want to record on your LED strip. These numbers can vary greatly in different environments, but they will always stay between 0 and 1023.
The next variable you create is going to hold an integer (whole number) value, but it has a few parts. Break it into parts to understand it. First, you'll see the word 'map'. The map function converts one scale of numbers to another scale. In this case, you're converting soundMin and soundMax onto the scale of 0-numPixels. The value that you want to plot on that scale is analogRead(A4)-the sound reading.
Now that you've mapped the reading to the 0-numPixels scale, you will constrain soundValue. The constrain function puts a minimum and maximum value on a variable. After you constrain soundValue, it cannot have a value lower than 0 or greater than numPixels.
You've converted your sound reading to a number between 0 and numPixels (15 in this case). Now use a for loop to draw each pixel on the strip. In the for loop you'll create an integer i and set it to 0, then as long as it is less than soundValue, you'll increase it by one each time you run the loop. Inside the loop, you'll set the pixel with the number 'i' to the color value 10. You also draw each new pixel as you set the color. drawing the pixels one at a time makes your sound gauge look more smooth.
If the noise in your environment is getting quieter, there should be fewer pixels that are illuminated. The second for loop starts at the tip of the LED strip and works it's way down to soundValue, drawing each pixel with a color value of -1, which is blank.
Finally, you use the strip.draw() command to draw all of the setPixel commands to the light strip. In this case, the illuminated pixels are already lit, so only the blank pixels are drawn with this line.