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
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.