Shop OBEX P1 Docs P2 Docs Learn Events
Compareison of two numbers in Pbasic — Parallax Forums

Compareison of two numbers in Pbasic

CogburnCogburn Posts: 62
edited 2009-11-20 04:53 in BASIC Stamp
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?

Comments

  • SRLMSRLM Posts: 5,045
    edited 2009-11-19 21:47
    Look at the "if" command...
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-11-20 04:53
    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, <=[noparse][[/noparse]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
Sign In or Register to comment.