PROJECT: Light & Sound LED Strip Gauge
Step 1 - Build the Project

Using an LED strip, light sensor and sound sensor, create a double gauge that measures the sound from one end and the light from the other. 

Step 2 - Upload the Code

/* * An LED strip gauge for the light sensor and sound sensor. One * reading displays from each end towards the middle of the strip. */ //Include the LED_Strip 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(DI),clockPin(CI)) LEDStrip strip = LEDStrip(numPixels, 13, 12); void setup() { pinMode(A4,INPUT);//Center pin of sound trigger pinMode(A2,INPUT_PULLUP); //Light sensor } void loop() { //Limits for the sound sensor, change these for your environment const int soundMin = 400; const int soundMax = 800; //Map the light values of 50 to 950 onto lower half of LED strip const int lightValue = map(analogRead(A2),50,950,0,numPixels/2); //Map the soundValues onto upper half of LED strip int soundValue = map(analogRead(A4),soundMin,soundMax, numPixels,(numPixels/2)); //Constrain soundPixel's values to the upper half of the strip soundValue = constrain(soundValue,(numPixels/2),numPixels); //Starting at bottom, count up to lightPixel and set those pixels for (int i = 0; i < lightValue; i++){ strip.setPixel(i, 50); //50 is purple (colors are 0-300) } //Starting at tip of strip, count down to soundPixel and set pixels for (int i = numPixels; i > soundValue; i--){ strip.setPixel(i, 100); //100 is red (colors are 0-300) } //Starting at the top of lightPixel, count up to soundPixel and clear //the pixels in the middle of the strip. for(int i = lightValue; i < soundValue; i++){ strip.setPixel(i,-1); //-1 is clear (no color) } strip.draw(); //send all of the .setPixel() commands to the strip } //Change the color arguments in the setPixel commands to make the // colors change with the value of soundPixel and lightPixel //Hint: add some multiple of lightPixel and soundPixel as the color values //(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 trigger as an INPUT and the pinMode of the light sensor as an INPUT_PULLUP. Now your sensors are sending information in to Maker Board, not waiting for commands from it. 

In the loop of the code, create two integer variables soundMax and soundMin so you can easily adjust the range of sounds that will show up on the LED strip.You may have to change these variable values depending on the room you're in.

Create and map an integer variable called lightValue. The map() function takes in the light readings 50-950 and put them on the scale of 0 to numPixels/2. This line is what allows you to fit 900 potential different light values onto 7 pixels of the LED strip. Although there are many numbers to the right of the equals sign, lightValue will be an integer between 0 and numPixels/2 (7).

Next, you'll create and map a variable called soundValue. It's a similar process to lightValue, except you're using more variables here. You are still mapping an analogRead but you are mapping soundMin to the tip of the LED strip and soundMax to the middle of the strip.

Now you'll immediately modify the value of soundValue. The sound trigger 'bounces' when it senses a noise, so a noise may trigger it to jump to 800 and then fall to 100 for the same noise. When those values are mapped, they will fall outside the bounds of numPixels and numPixels/2.  Constrain applies a boundary to the soundValue variable so it will never equal less than numPixels/2 or more than numPixels.

All of your readings have been taken and modified to fit on the LED strip. Now it's time to draw and display them. Your first for loop creates an integer i and sets it equal to 0. As long as i is less than lightValue, the for loop will run and increase i by one. The first time through, i will set pixel 0 equal to color value 50. The next time, it will set pixel 1 to color value 50. 

The next for loop creates a new variable i and sets it equal to numPixels (the tip of the strip). As long as i is greater than soundValue, the for loop moves down the strip toward the middle and sets the pixel equal to color value 100.

If the light or sound values decrease between readings, the gauge will show fewer pixels. To create this effect, you use a third for loop that starts with an integer i at the top of the light gauge and, until it hits the top of the sound gauge, it will set every pixel equal to color value -1, which is clear/no color. The final command is strip.draw(), which displays every .setPixel command on the LED strip.