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: AustraliaPosts: 54 |
Hi its probably been asked before (sorry) can you use > < in a statment like if pin(2) > 3 < 2.5 then else if pin(2) >2.5 < 2 then and so on? Thanks Martin Heppy |
||||
paceman Guru Joined: 07/10/2011 Location: AustraliaPosts: 1329 |
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: ThailandPosts: 2209 |
[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. Microblocks. Build with logic. |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6098 |
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: AustraliaPosts: 1329 |
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 |