PROJECT: Tricolor Temperature Indicator
Step 1 - Build the Project

Using three LEDs or one multicolor LED, set three temperature thresholds. Turn on a different LED depending on the temperature band you're in. 

Step 2 - Upload the Code

/* * Display temperature as three different colors */ void setup() { //Check the temp sensor pinout, getting it backwards ruins it pinMode(A3,INPUT); //Center pin of temp sensor to A3 //LED pins pinMode(9,OUTPUT); pinMode(10,OUTPUT); pinMode(11,OUTPUT); } void loop() { /* * Conversion constants 0.88 and -58 turn the analog reading * to Farenheit. Cast tempValue to a float. */ const int tempF = int(0.88*float(analogRead(A3)) - 58.0); //Temp less than 70 degrees is 'cool' if (tempF < 70){ //Write first LED HIGH, others LOW digitalWrite(11,HIGH); digitalWrite(10,LOW); digitalWrite(9,LOW); } //Temp less than 75, but above 70 is comfortable else if (tempF < 75){ //Write second LED HIGH, others LOW digitalWrite(11,LOW); digitalWrite(10,HIGH); digitalWrite(9,LOW); } //Temp greater than 75 degrees is 'warm' else{ //Write last LED HIGH, others LOW digitalWrite(11,LOW); digitalWrite(10,LOW); digitalWrite(9,HIGH); } } //Try to edit the else if block so that all LED colors turn on for // a specific temperature // (c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Step 3 - Read the Walkthrough

First, set the pinMode of your temperature sensor's center pin as an INPUT. Ensure that you have the outer legs plugged in the correct orientation to + and - or the sensor will get very hot.Set the LED pins as OUTPUTs.

The integer in the loop  converts the analog reading into a Fahrenheit value that you can use. The numbers 0.88 and 58.0 are specific to this sensor, so don't worry about those. Look at "float(analogRead(A3))" in the middle of the declaration. That is called a cast. It means "for this one line of code only, take the reading(an int) and use it as a float". If you don't do that, analogRead(A3) will continue to act just like an int, cutting off all decimal places in the math that you're doing. 

Here's a curveball: Just to the right of the = sign next to tempF, you see the letters "int". That means you're re-casting the reading to an int. The steps are as follows: Take and analog reading. Make it a temporary float. Do some decimal-math with tempValue as a float. Then, turn that result back into an int. The value of that integer is your tempF variable. 

Now that you've done all the math to get a temperature value from analogRead, you can compare that temperature to your thresholds and act on it using if statements. Pay attention to the order of these statements, because a different order would have a different effect on the code.

The first if statement checks if the tempF variable is lower than 70 degrees. If it is, one LED is written HIGH with digitalWrite, turning the LED on. The other two LEDs are turned off. It's important to turn both of the inactive LEDs off in each 'if' statement. Although unlikely, imagine that the temperature dropped from 80 to 60 degrees almost instantly because of a blast of cold air. If your code didn't turn both off the pin for the 75+ degree LED, it would remain on.

The else-if statement is only evaluated if the if statement above it is false. This statement essentially captures the band of temperatures above 70 degrees and below 75 degrees. 

The final statement is an else, so it will occur if none of the if-else if statements above it are true. Think of it as an 'otherwise' statement. 

Ordering these statements is important to get the outcome you want. Imagine if the top 'if' statement checked for temperatures below 75 degrees. If it was 60 degrees, that statement is true, so the code would run. The code for the temperatures below 70 would never be run because the first statement is checked first. Whenever you're creating multiple outputs from a single input, consider the order in which you want to check the input.