LED Temperature Indicators

Code

The code in the editor below is ready to run! Plug your Code Lab in to your computer's USB port with the cable and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.


/* Light up single color LEDs depending on the temperature*/ void setup() { pinMode(A1,INPUT); //Temp sensor pinMode(2,OUTPUT); //LED - red for hot pinMode(3,OUTPUT); //LED - blue for cold pinMode(4,OUTPUT); //LED - green for comfortable } void loop() { int tempF = analogRead(A1)*.46; // convert the analogRead to a temperature value in Farenheit. if(tempF > 80){ //if the temperature is above 80 degrees digitalWrite(2,HIGH); //turn on LED 2 (red) digitalWrite(3,LOW); //turn off LED 3 digitalWrite(4,LOW); //turn off LED 4 } else if(tempF>70){ //otherwise, if the temperature is above 70, but not above 80 degrees digitalWrite(4,HIGH); //turn on LED 4 digitalWrite(2,LOW); //turn off LED 2 digitalWrite(3,LOW); //turn off LED 3 } else{ //otherwise, if the temperature is below 70 degrees... digitalWrite(3,HIGH); //turn on LED 3 digitalWrite(2,LOW); //turn off LED 2 digitalWrite(4,LOW); //turn off LED 4 } delay(500); //wait 500 milliseconds (0.5 seconds) betwen taking temperature readings } // (c) 2021 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Walkthrough Video

Watch the video for a line-by-line explanation of how the example program works. Then you'll be ready to make some changes of your own!

Video Notes for Code Lab Users:

  • You are using an LED in port 2 in place of 6, port 3 instead of 4, and port 4 instead of 2 for your LED temperature indicators.

Challenges

Can you complete the challenge? Change the code in your code editor above. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!

Concepts

These are the new code concepts covered in this example program. To become a great coder, read through these concepts to learn new vocabulary.

Quiz

If you're having trouble, try to run an experimental program or look at the example code to help you find the answer.

1. Which syntax ends the entire 'void loop' function?




2. Which syntax ends a single code command?