if command

Bloody-orc

Senior Member
why is this if statement not allowed:

symbols
...
if temp > (balldis-2) OR temp < (balldis+2) then
...

both temp and balldis are variables.
they both are ultrasonic module readout values and as we all know they tend to vary +-2 or so... so i need to use this command instead. or is there another way to if this:
if temp = balldis +- 5 then
 

Technical

Technical Support
Staff member
You cannot do mathematics within other commands (such as if...then). You must do the mathematics first

let temp1 = balldis - 2
let temp2 = balldis + 2

if temp > temp1 OR temp < temp2 then
 

Bloody-orc

Senior Member
that aint normal! please add this to user wish list and try to add this to new prog. editor please.

Especially when we have just 14 bytes to use...

Edited by - bloody-orc on 13/11/2006 14:31:04
 

Technical

Technical Support
Staff member
Actually this is completely normal for microcontroller based languages!

Microcontroller programming languages are very different to 'computer based' programming languages. A computer has a large stack for storing 'intermediate' calculation results, microcontrollers do not.
 

Bloody-orc

Senior Member
I've been doing this for ages on AVR chips (well this seems to be PIC's problem then ;). Or maybe this is just something that mr. C has to offer to they're users... oh well... got to live with this then and must find a place to get those 2 extra variables...
 

Technical

Technical Support
Staff member
If you haven't got two spare variables do the maths on temp instead, that way you don't need any more variables

if temp > (balldis-2) OR temp < (balldis+2) then skip

is the same as

temp = temp + 2
if temp > balldis then skip
temp = temp - 4
if temp < balldis then skip

remember to correct temp value (if necessary) afterwards
 

Jeremy Leach

Senior Member
I might be wrong here, but I think you're trying to establish if temp is in the range Balldis +/- 2? If so I think you need an AND not an OR: ie <i>IF temps above the lower limit of the range AND below the upper limit of the range, THEN ... </i>


You can do this another way though, by expressing the opposite as <i>IF temps above the higher limit of the range OR below the lower limit of the range, THEN ... </i>


Edited by - jeremy leach on 13/11/2006 17:28:33
 
Top