Project #3 for() loop mystery

I've assembled and coded project #3 "love-o-meter" but am having trouble with for()loop.

everything seems to be in order. except that the only way I can get it to appear to work properly is if I change my for() loop.

The book says to code:

for(int pinNumber = 2; pinNumber<5; pinNumber++){
pinMode(pinNumber,OUTPUT);
digitalWrite(pinNumber, LOW);

However the LED's wont light unless I make one change last line to

digitalWrite(pinNumber, HIGH);

I then also need to change my baselineTemp to =13 rather than the actual 16C of room temp.

If I do these things it seems to operate exactly as its meant to.

I should note that the Serial Monitor is indicating everything is normal. The TMP is sensing around 16C.

I've been over the code several times and even disassembled and reassembled the circuit. Its also worth noting that I completed this project without this problem yesterday and only decided to revisit today for the practice. I am baffled!!

Hi confused1, Sorry it took so long to get to you.

The baselineTemp has to be set as close as possible to room temperature to be accurate.

Do you have the LED in the correct orientation and connected correctly?

As far as I read on the digitalWrite-function (and as far as I remeber 8)) it does depend here what you are trying to achieve.

Case 1: The LED has one side (anode side) connected to 5V and the other side (cathode) connected to "pinNumber". In that case the code is correct. It looks strange but the arduino does not need to read from the pin so it is an OUTPUT, which is in turn pulled LOW, so to 0 volt.

Case 2: The LED is has one side connected to GND (cathode side) and the other side (anode) is connected to "pinNumber". In this case, you want it to be OUTPUT and HIGH as is has to supply 5V to the LED.

Case 2 is the preferred, or most used, config. Let is know how your getting on.

As naneen said...

In this piece of code here:

const int sensorPin = A0;
const float baselineTemp = 20.0; // change temp here

You need to find whatever the room temperature is, write it in where I left you the note.

Hope this helps!

-marco