Easy! Why is this the opposite of what I expect?

I'm new to the game, just going through the sparkfun inventor's kit, with a few additions here and there for fun. This is an easy situation. I have a working temperature sensor; I wanted to have two LEDs (one red, one yellow) show if the temperature is going up or down.

The goal was to have the Red LED come on when the temperature was going up, and the Yellow LED to come on when the temperature is going down.

So I put this little bit in the loop:

void setup()
{
  float lastvoltage = 0;

  pinMode(YellowLED, OUTPUT);
  pinMode(RedLED, OUTPUT);
  digitalWrite(YellowLED, LOW);
  digitalWrite(RedLED, LOW);
}

void loop()
{
   float voltage, degreesC, degreesF;
   voltage = getVoltage(temperaturePin);
  //A function that reads the sensor and returns a Float to "voltage"
  
  if(voltage < lastvoltage)
  {
    digitalWrite(YellowLED, HIGH);
    digitalWrite(RedLED, LOW);  
  }

  if(voltage > lastvoltage) {
    digitalWrite(RedLED, HIGH);
    digitalWrite(YellowLED, LOW);
}

  lastvoltage = voltage;
  delay(1000);
}

float getVoltage(int pin)
{
 return (analogRead(pin) * 0.004882814);
 }

The strange thing to me is that the opposite of what I expected happened- when the console was reading temperatures going up, the YELLOW LED was coming on, instead of the red one, and vice versa.

Obviously when I reverse the "<" and ">" it works, but I wanted to know why it was backwards.

As temperature goes up, the variable "voltage" goes up, so if "lastvoltage" was saving the last reading before the sensor returns again and the next reading is higher, then voltage should be > lastvoltage and the RED LED should turn on.

It has confounded me so much that I had to post it here. I'm sure it's a simple reason, my simple brain just can't figure it out.

Thanks!

As the code sample you supplied does not compile I cannot be sure of problem but your defining 'float lastvoltage = 0;' in setup() instead of globally and it goes out of scope when in loop() (though this should throw a compile error)

Easy! Why is this the opposite of what I expect?

That is easy. You are expecting the wrong thing.

There is no reason to be performing all that float math. The action to light one LED or the other could just as easily be done based on the sensor reading.

Serial.print() statements would probably clear up the mystery.

First we have to know how you wired up your leds to your output pins to determine if a high or a low causes them to turn on.

Lefty

Well another obvious question is how have you wired up the thermistor and which thermistor are you
using? (datasheet always a good idea).

I took out the code that was not helpful to the question, so I do have serialprint showing the temperature reading on the console. Using this code, when the voltage goes up, the yellow LED turns on, which still boggles me.

How would you wire the LEDs so that "LOW" turns them on?

Thanks!

How would you wire the LEDs so that "LOW" turns them on?

Cathode to Arduino, anode to the resistor, resistor to 5v.


Rob

And, as said, you don't need to do all that conversion to a voltage for the comparison. Just read the analog value as between 0 and 1023 and act on that.

A copy of your circuit would be good information.