Temperature Sensor LED Strip Gauge
Step 1 - Build the Project
Using an LED Strip and a temperature sensor, create a temperature gauge that lights up more pixels when it's warm and fewer pixels when it's cool.
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 temperature 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 a variable called tempRead and set its value to the analogRead(A3). Now any time you use the variable tempRead, you're using the analog reading of pin A3. Keep in mind that the variable only updates its value on this line, not each time you use it. You're using the same single analogRead for an entire loop.
Next, you'll create an integer variable called tempAverage. The value of this variable is the result of the equation to the right of the equals sign. The first operation multiplies the previous value of temperature value by 0.9. The second operation has two parts. First, tempRead is cast as a float. This means that for this calculation only, tempRead will be treated as a float variable, not an integer. You need a cast here because multiplying an integer by a decimal will just cut off the decimal portion of the product. After tempRead is cast, it is multiplied by 0.1. In effect, each time you run this line of code, your new temperature reading makes up 10% of your tempAverage.
Up to this point, your tempAverage variable has only held a voltage reading. Here you create an integer tempF to convert that voltage reading to a temperature reading in degrees Fahrenheit. These values are specific to the component, so don't modify this line. You can see two more casts here, though: One casts tempAverage to a float, the other casts the variable's value back to an int.
The .clear() method clears out the pixels and colors that were on the strip so you can write new values to the strip.
Now you'll create an integer called pixel. The value of this variable is the result of the map function. The map function takes the temperatures 60-80F and maps those values onto the range 0-numPixels (15 pixels in this program). So a temperature of 60 equals 0 pixels and a temperature of 80 equals 15 pixels. Once this transformation has happened, that new value between 0 and 15 is assigned to the variable pixel.
The next step is to set the correct number of pixels to light up. You'll do this with a for loop. To create the for loop, you create a new integer variable called i and set it equal to 0. Now, as long as i is less than the value of pixel, i will increase by one (++) each time the for loop runs.
Inside the for loop, the .setPixel() method sets the pixel 'i' to the color value of 200. The first time the for loop runs, it will use a value of 0 for i. The second time it runs, the for loop will use a value of 1. This will continue until i is equal to the variable pixel.
Once the for loop has completed, the code moves on. The next method is .draw(), which displays the pixels that are set in the .setPixel() method. Because .draw() comes at the end, all of the pixels will appear to show up at once.