Loading...
Pages: [1]   Go Down
Author Topic: Easy! Why is this the opposite of what I expect?  (Read 156 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Ohm
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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!

Logged

Norfolk UK
Offline Offline
Edison Member
*
Karma: 23
Posts: 1320
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 314
Posts: 35507
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 279
Posts: 15316
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

0
Offline Offline
Tesla Member
***
Karma: 71
Posts: 6624
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 2
Ohm
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 71
Posts: 6826
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How would you wire the LEDs so that "LOW" turns them on? 
Cathode to Arduino, anode to the resistor, resistor to 5v.

______
Rob

Logged

Rob Gray aka the GRAYnomad http://www.robgray.com

UK
Offline Offline
Sr. Member
****
Karma: 9
Posts: 351
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Print
 
Jump to: