Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 17:38 26 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : less than greater than

Author Message
heppy36

Regular Member

Joined: 29/07/2011
Location: Australia
Posts: 54
Posted: 11:30pm 06 Dec 2012
Copy link to clipboard 
Print this post

Hi its probably been asked before (sorry)
can you use > < in a statment
like
if pin(2) > 3 < 2.5 then
Print
else if pin(2) >2.5 < 2 then
print

and so on?
Thanks Martin
Heppy
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 01:30am 07 Dec 2012
Copy link to clipboard 
Print this post

  heppy36 said   Hi its probably been asked before (sorry)
can you use > < in a statment
like
if pin(2) > 3 < 2.5 then
Print
else if pin(2) >2.5 < 2 then
print


Heppy,

Not like that - you have to state each test separately and use the logical operators like AND, OR, NOT etc. For example:

If pin(2)> 3 <2.5 then.........has to be stated:

If pin(2)> 3 OR pin(2) <2.5 then......in this case obviously an AND is impossible.

Greg
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 02:56am 07 Dec 2012
Copy link to clipboard 
Print this post

[quote]
If pin(2)> 3 OR pin(2) <2.5 then
[/quote]
this could however still give a problem. :)

A problem that can hide itself for years, but of course it will show itself when you least expect it.
When reading a pin value you should save it in a variable before doing multiple tests on it.
Because in the time between the test for (pin(2) > 2) and the next test (pin(2) < 2.5) the value of that pin can change.
Analog values can be interpreted wrong that way.

In the example above the first time pin(2) can return a value of 2 and the next value of pin(2) can be 3 causing the test to be false.
If pin(2) is stored in a variable first then the value of 2 would evaluate to a true.

With a digital input it only flips between 0 and 1 but it can be a disaster when that 0 or 1 will activate something else that really shouldn't.

Best practice:
Only compare variables with other variables or literals/constants

These bugs can be very sneaky especially when the code has been revised.
You can start with:
if pin(2) = 3 then....
and later decide to add a:
if pin(2) = 4 then ....

only to discover at some moment that the code is not doing what you want.
Therefore better to do this:
Value = pin(2)
if Value = 3 then .....

Later you would probably add:
if Value = 4 then ....

Assigning the value to a variable before helps to prevent bugs.



Edited by TZAdvantage 2012-12-08
Microblocks. Build with logic.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6098
Posted: 09:44am 07 Dec 2012
Copy link to clipboard 
Print this post

  heppy36 said   Hi its probably been asked before (sorry)
can you use > < in a statment
like
if pin(2) > 3 < 2.5 then
Print
else if pin(2) >2.5 < 2 then
print

and so on?
Thanks Martin

Rather than doing two tests for each condition, if they can be 'stacked up' correctly you may be able to use one '>' for each.

This is the wind direction sub from my weather station program.
(The resistors used in the sensor make the wind direction voltages jump all over the place!)


_windBrg:
rwm=pin(2)
IF rwm>3.10 THEN
w= 17
ELSEIF rwm > 2.98 THEN : w = 12 'W
ELSEIF rwm > 2.81 THEN : w = 14 'NW
ELSEIF rwm > 2.73 THEN : w = 13
ELSEIF rwm > 2.60 THEN : w = 16 'N
ELSEIF rwm > 2.40 THEN : w = 15
ELSEIF rwm > 2.25 THEN : w = 10 'SW
ELSEIF rwm > 2.00 THEN : w = 11
ELSEIF rwm > 1.70 THEN : w = 2 'NE
ELSEIF rwm > 1.40 THEN : w = 1
ELSEIF rwm > 1.10 THEN : w = 8 'S
ELSEIF rwm > 0.90 THEN : w = 9
ELSEIF rwm > 0.70 THEN : w = 6 'SE
ELSEIF rwm > 0.50 THEN : w = 7
ELSEIF rwm > 0.40 THEN : w = 4 'E
ELSEIF rwm > 0.35 THEN : w = 3
ELSE
w = 5
ENDIF
return

The closest thing to a 'select case' function.

Jim
VK7JH
MMedit   MMBasic Help
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 01:57pm 07 Dec 2012
Copy link to clipboard 
Print this post

Hi TZ & TassyJim,

Hmm - yes, very true and I think I should have known that from a correction that James (Deakin) made for me on some code a couple of months ago (guess I get a fail there James!)

Greg
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024