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

/* * An LED strip gauge for the sound sensor. The louder it is, the * more pixels will be illuminated. */ //Include the LEDStrip library to use the commands #include "LEDStrip.h" const byte numPixels = 15; //number of pixels on a strip //Make an LED strip object: LEDStrip(numPixels,dataPin(DO),clockPin(CO)) LEDStrip strip = LEDStrip(numPixels, 13, 12); //The average noise level float averageNoise; void setup() { pinMode(A4,INPUT); //Center pin of sound trigger //Get initial sound measurement to 'seed' the average averageNoise = analogRead(A4); } void loop() { //Compute the moving average of the noise level //Each new reading contributes 70% to the average averageNoise = 0.7*(float)(analogRead(A4)) + 0.3*(averageNoise); //soundValue is cast to a float so it can be multiplied by decimals strip.clear(); //Make the strip blank after every reading //Map the sound values of 250 to 750 onto the LED strip const byte pixel = map(int(averageNoise),250,750,0,numPixels); //Pixel will have a value between 0 and numPixels //Turn on each pixel up to that number for (int i = 0; i < pixel;i++){ strip.setPixel(i, 10); //Colors range 0-300, 10 is blue } strip.draw(); //Tell the strip to display the .setPixel commands } //Re-map noise level (300-450) to color (0-300), and display on all // LEDs with the strip.ALL in place of 'pixel' in setPixel //Hint: Replace the for loop and pixel map with one line of setPixel // (c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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.