The Code
Constants are similar to variables in that they allow you to uniquely name things in the program, but unlike variables they cannot change. Name the analog input for easy reference, and create another named constant to hold the baseline temperature.
For every 2 degrees above this baseline, the LED will turn on. The temperature is being stored as a float, or floating-point number. This type of number has a decimal point, and is used for numbers that can be expressed as fractions.
In the setup you’re going to use a new command, Serial. begin(). This opens up a connection between the Arduino and the computer, so you can see the values from the analog input on your computer screen.
The argument 9600 is the speed at which the Arduino will communicate, 9600 bits per second. You will use the Arduino IDE’s serial monitor to view the information you choose to send from your microcontroller. When you open the IDE’s serial monitor verify that the baud rate is 9600.
Next up is a for() loop to set some pins as outputs. These are the pins that you attached LEDs to earlier. Instead of giving them unique names and typing out the pinMode() function for each one, you can use a for() loop to go through them all quickly. This is a handy trick if you have a large number of similar things you wish to iterate through in a program. Tell the for() loop to run through pins 2 to 4 sequentially.
The function Serial.print() sends information from the Arduino to a connected computer. You can see this information in your serial monitor. If you give Serial.print() an argument in quotation marks, it will print out the text you typed. If you give it a variable as an argument, it will print out the value of that variable.
Last updated