PDA

View Full Version : Compareison of two numbers in Pbasic



Cogburn
11-20-2009, 05:16 AM
I want to compare two values returned from RCTime for two photoresistors.. If these values are within 10 % of each other, I want the stamp to execute a loop to read RCTime again. If the value collected from the left photoresistor is 10% or more higher than the value collected for the right photoresistor, I want a stepper motor to move to the left by a small increment. If the value for the right photoresistor is more than 10% higher than the left one, I want the stepper motor to move to the right by a small increment. I have the stepper moving left and right at my command. I have the photoresistors reading values that I can compare but I can't figure out how to let Pbasic make the comparison. Any ideas?

SRLM
11-20-2009, 05:47 AM
Look at the "if" command...

Tracy Allen
11-20-2009, 12:53 PM
For example,



DO
' get RCTIME values here, then...
IF RCTL >= RCTR + (RCTR / 10) THEN
GOSUB move_Left
ELSEIF RCTR >= RCTL + (RCTL/10) THEN
GOSUB move_Right
ENDIF
LOOP



There are other ways to skin the cat, for example the 10% rule can be done with this formula
IF RCTL >= RCTR */ 282 THEN ' means, RCTL 10% larger than RCTR, look up the */ operator

Here is another way using the SELECT:CASE command and the */282 for 110% and */230 for 90%:


DO
' get RCTIME values here, then...
SELECT RCTL
CASE >= RCTR */ 282
GOSUB move_Right
CASE <= RCTR */ 230
GOSUB move_Left
ENDSELECT
LOOP




Or, another way using the LOOKDOWN command. For this one you have to create a dummy subroutine "do_nothing" that just RETURNs.


ix VAR Nib
DO
' get RCTIME values here, then...
LOOKDOWN RCTL, <=[RCTR */ 240, RCTR*/282, 65535], ix ' RCTL is <90% or >110% or in between, ix=0,1 or 2
ON ix GOSUB move_Right, do_nothing, move_Left
LOOP

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com (http://www.emesystems.com)