IF condition w/ math
AGCB
Posts: 327
I'm working on a thermostat program and would like to turn on the heat at 1/2 degree below the set point. When I try to add the math (-1/2)to this condition, the heat does not come on when inside temp is below set point
I'd like not to have to use floating point math.
Is my IF condition written correctly?
Sometimes a seemingly simple thing racks my brain.
Thanks
Aaron
if IS_Temp < (setTemp-1/2) AND (heatflag==0) '1/2° below set temp and not already on outa[16]~~ 'turn on heat relay heatflag:=1 'set flag
I'd like not to have to use floating point math.
Is my IF condition written correctly?
Sometimes a seemingly simple thing racks my brain.
Thanks
Aaron
Comments
If you want to avoid floating point, one trick is to multiply everything by ten. So 70 degrees (F) might be 700. Then half will become 5 and you can do all the maths as integers.
I'll give that a try
One of the things I like about the Spin compiler is that it allows the underscore character in numbers; I use this in place of a decimal point in situations like this.
this doesn't
or using decimal point
Thanks for the replies and I'll be back
ALL the numbers have to be 10x
if (IS_Temp*10 < (setTemp*10 - 0_5))
But best is to just read/store/write all temperature vars as 10x to start with.
You know that there is a xx.x decimal point in there, but if you don't tell math operation it will be our secret.
If ever needed to display the value you would force a char(".") in before that last digit.
Thanks for all the replies. You guys are great!
Aaron
I've posted a "DecPoint" method to the forum several times. It will insert the decimal point for you.
There are several other methods floating around which do the same thing.