Temperature display

Hi, given the temp on the glcd is about 2 degrees out due to the cpu rather than mod the location of the sensor I would like to drop it in the code.

I found this 

emonglcd.temperature = (int) (inttemp * 100);

so i tried

emonglcd.temperature = (int) (inttemp * 100 - 2);

But it didnt seem to work, any ideas? im using the code kindly put together by others found in this link

http://openenergymonitor.org/emon/node/2639

Robert Wall's picture

Re: Temperature display

You need to read up on operator precedence. In the C language, there are well-understood rules for the order in which mathematical operations are performed. I'll bet any money it did work. Hint 1: what is the  * 100 doing?  Hint 2: emonglcd.temperature is an integer and can't be changed, so it can accept only whole number values. Hint 3: You subtracted 2 hundredths of a degree so you couldn't see the difference.

dod's picture

Re: Temperature display

Thanks Robert,

My ever so helpful friend explains to me Operator precedence.

And in turn shows me how clever you guys are !

Okay, operator precedence. What you wrote is actually doing:

emonglcd.temperature = (int) ((inttemp * 100) - 2);

because multiplication has a higher precedence than subtraction, so it's always done first.

This means that you're only subtracting two hundredths of a degree, like the man said, so you're not getting the result you want.

You need to either do this:

emonglcd.temperature = (int) (inttemp * 100 - 200);

or

emoglcd.temperature = (int) ((inttemp - 2) * 100);

The second one does need the brackets, or it'll do 2*100 and subtract that from inttemp which is definitely not what you want!

For future reference, / and * have the same precedence, which is higher than + and - which are the same as each other. It's normal for multiplicative operators to have higher (or 'tighter') precedence than additive operators.

 

Thanks again

 

Dave

 

Robert Wall's picture

Re: Temperature display

That's all correct. I figured all you'd need was a QDM.

My preference would be emoglcd.temperature = (int) ((inttemp - 2) * 100); because it's the most obvious form. Remember, if 6 months on you can't figure it out, it was too complicated to begin with.

dod's picture

Re: Temperature display

Oddly it still reports 24.4 before and after the change?

before

emonglcd.temperature = (int) (inttemp * 100);

after

emonglcd.temperature = (int) ((inttemp - 2) * 100);

 

Current script attached

Robert Wall's picture

Re: Temperature display

"Reports"? Where to? emonglcd.temperature is the temperature you're passing on to emoncms. It's still displaying inttemp. Methinks oops! you did the right correction, but in the wrong place. (And I haven't even looked at your code yet!). It needs to go in a few lines up immediately after the temperature is got from the sensors (and in the startup too, to get max and min correct).

I don't see what rawtemp is doing - if the temperature is out of range at startup it displays zero, else the last good value. That doesn't strike me as being sensible, but if you want it that way, OK.

And being pedantic, a marginally better form would be rawtemp = rawtemp - 2.0;  (which is what you need after the getTemp)

The constant 2 is promoted to a double anyway by the compiler before it does the subtraction, writing 2.0 saves it the trouble.

dod's picture

Re: Temperature display

Hi Robert, Its not my code its martin & Pauls, im just using it as it covers 2 pv's & grid.

I noted that it was updating cms not the lcd, I will see if I can figure it out if not I will be back!

 

Thanks

Robert Wall's picture

Re: Temperature display

If you look at what I wrote earlier alongside the sketch, it ought to make sense!

dod's picture

Re: Temperature display

No I failed :-(

Robert Wall's picture

Re: Temperature display

The value is read from the sensor and it actually exists in 3 versions with 3 different names. What you need to do is track each version and see where it is used, and why. One is the raw version, one is used in the LCD display, and one is the number multiplied by 100 and sent on over the r.f. link to the base and onwards to emoncms. Therefore, you need to get at the temperature and apply your correction as soon as it is got from the sensor. Look again at my post on Tue, 27/08/2013 - 22:38. I actually spelled it out there.

dod's picture

Re: Temperature display

Ok so I changed the part that sends it to the pi back to 

emonglcd.temperature = (int) (inttemp * 100);

and I changed the line

 

inttemp = (sensors.getTempCByIndex(0)) ;

to

inttemp = (sensors.getTempCByIndex(0)) - 2;

It looks ok do I get a biscuit :-)

 

edit:  maybe not, on startup its fine but after about 30 seconds the temp jumps up two degrees.

 

 

dod's picture

Re: Temperature display

looks like i needed to also modify

 

double rawtemp = (sensors.getTempCByIndex(0)) - 2;

Robert Wall's picture

Re: Temperature display

I did warn you earlier - "It needs to go in a few lines up immediately after the temperature is got from the sensors (and in the startup too, to get max and min correct)."  Only one problem, it sounds as if you did it ONLY in the startup. It looks like you've found both places now.

What are you using to edit the sketch? Over the years, I've found that a really good editor that you know well and has good search (and search-and-replace) is indispensable. Needless to say, I don't use the editor in the Arduino IDE. I suspect you looked at the code and didn't do a search for the different mutations of temperature as the sketch progresses. I think that would have helped you here.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.