Shop OBEX P1 Docs P2 Docs Learn Events
Comparing string? — Parallax Forums

Comparing string?

FalconFalcon Posts: 191
edited 2011-12-10 16:25 in BASIC Stamp
I pretty sure there is no way to actually compare a string but I'm looking for a workaround to the following situation:

I use the following routine to compare a bit from a sensor to check to see if there is a change, and act accordingly.
Sensor:
temp = Current1.BIT0                                          'load temp with current door state
  IF Saved (0) = temp THEN                                     'if door saved = door current = no change set to false
      Flag (0)= 0                                                'no change ensure flag is cleared
     ELSE
      Flag (0) = 1                                               'door changed states , set flag to true
      Saved (0) = temp                                           'save current state for compare next time through
     ENDIF
  IF Flag (0) = 1 THEN
     IF Saved (0) = 1 THEN

    DEBUG CRSRXY, 24, 14,"OPEN  "
    
  ELSE

    DEBUG CRSRXY, 24, 14,"CLOSED"
      ENDIF
      Flag (0) = 0
ENDIF
In this case compare works fine since I'm working with a BIT.

I have a PINK with a small webpage with variables that I can update with my Droid phone as either an OFF or ON.
I use the following code to retreive the variable from the PINK and display it:
  PAUSE 50
  SEROUT TX, PINKBaud, ["!NB0R01"]          ' Command To Read Variable 04
  SERIN  RX, PINKBaud, [STR NBVAR\3]
  DEBUG CRSRXY, 24, 27, STR nbvar, CR
The DEBUG command can easily send the nbvar string to the terminal window.

I want to use the ON and OFF strings to trigger X-10 actions using XOUT using my compare routine. The problem is getting the ON and OFF converted to a 1 or 0 since I cannot compare a string.

Any ideas how to check the string and set a BIT to 1 if it is ON, and 0 if it is OFF?

falcon

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-12-09 19:26
    string VAR Byte
    targetbit VAR string.BIT4
    
    string =%00010000
    
    IF targetbit = 1 THEN
      targetbit = 0
      ELSE
      targetbit = 1
      ENDIF
    
    
  • FalconFalcon Posts: 191
    edited 2011-12-10 06:24
    PJ,

    I appreciate the reply.
    I don't really understand how to use this snippit to determine if that 3 byte string is ON or OFF. I do see how I can pick a single BIT out of a BYTE and use that for a comparison.

    The string that is retrieved from the PINK is 3 bytes long. I was wondering if I could just check the second byte of the string (array actually) to see if it is an ASCII "N" (78) or "F" (70)?

    Would something like the following work?
    NBVAR         VAR      BYTE
    SEROUT TX, PINKBaud, ["!NB0R01"]          
    SERIN  RX, PINKBaud, [STR NBVAR\3]
    IF NBVAR(2) = 78 THEN temp = 1     ' 78 = ASCII N     
    IF NBVAR(2) = 70 THEN temp = 0     ' 70 = ASCII F
    

    falcon
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-12-10 07:14
    Why not branch to your XON or XOFF routine based on the test of NBVAR(2)?

    So, if NBVAR(2) is either $41 (A for Affirmative) or $4E (N for Negative) --
    IF NBVAR(2) = $41 THEN Lamp_on    '
    IF NBVAR(2) = $4E THEN Lamp_off
    Debug "Error", CR
    
    The test for NBVAR(2) = $4e isn't essential (in a perfect world if it's not A then it's N), but this guards against jumbled data, since there's no checksum or parity.
  • FalconFalcon Posts: 191
    edited 2011-12-10 08:55
    PJ,
    I worked that code into my project as seen below but used $4E to look for the N, and $46 to look for the F since the string will only ever be ON or OFF:
    nbvar           VAR     Byte(3)
    
    
      SEROUT TX, PINKBaud, ["!NB0R01"]          ' Command To Read Variable 04
      SERIN  RX, PINKBaud, [STR NBVAR\3]
    IF NBVAR(2) = $4E THEN X10_E1_On    '
    IF NBVAR(2) = $46 THEN X10_E1_Off
    DEBUG "Error", CR
    
    
    
    X10_E1_On:
    '  XOUT mPin,zPin,[houseE\Unit1]                              ' Talk to Unit.
    '  XOUT mPin,zPin,[houseE\UNITON]                             ' Tell it to turn ON.
    'PAUSE 100
     DEBUG CRSRXY, 24, 27, "ON", CR
     RETURN
    
    X10_E1_Off:
    '  XOUT mPin,zPin,[houseE\Unit1]                              ' Talk to Unit.
    '  XOUT mPin,zPin,[houseE\UNITOFF]                             ' Tell it to turn OFF.
    'PAUSE 100
     DEBUG CRSRXY, 24, 27, "OFF", CR
     RETURN
    

    I have the XOUT commands inhibited until I get the arguments sorted out so right now I'm just trying to get the correct feedback with the DEBUG statements.

    As written, I get the error message repeatedly.
    I'm not 100% sure which BYTE would be the first read from the PINK but in the case of the OFF, the second BYTE should alwys be an F no matter which end it the first BYTE read.
    I also tried the arguments with $6E and $66 in case the PINK was returning lower case letters but that didn't change the result.

    I checked the PINK variable page and the variables are updating correctly (and immediately) when changed with my mobile phone. The PINK is mostly used to provide variable data to the webpage from the BS2PX, and that function works flawlessly.

    Any othe ideas for those arguments?

    Thanks for your help.

    falcon
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-12-10 11:08
    From both X10_E1_on and X10_E1_off you are RETURNing, so it will fall through to the DEBUG "Error" line every time.
    Sort_it:
      SEROUT TX, PINKBaud, ["!NB0R01"]          ' Command To Read Variable 04
      SERIN  RX, PINKBaud, [STR NBVAR\3]
      IF NBVAR(2) = $4E THEN X10_E1_On    '
      IF NBVAR(2) = $46 THEN X10_E1_Off
      DEBUG "Error", CR
    
    X10_E1_On:
      '  XOUT mPin,zPin,[houseE\Unit1]                              ' Talk to Unit.
      '  XOUT mPin,zPin,[houseE\UNITON]                             ' Tell it to turn ON.
      'PAUSE 100
      DEBUG CRSRXY, 24, 27, "ON", CR
      GOTO Sort_it
    
    X10_E1_Off:
      '  XOUT mPin,zPin,[houseE\Unit1]                              ' Talk to Unit.
      '  XOUT mPin,zPin,[houseE\UNITOFF]                             ' Tell it to turn OFF.
      'PAUSE 100
      DEBUG CRSRXY, 24, 27, "OFF", CR
      GOTO Sort_it
    
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-12-10 11:19
    If you're unsure which/what your NBVAR(x) results are then DEBUG those, too.
  • FalconFalcon Posts: 191
    edited 2011-12-10 14:49
    PJ,

    That helped. I was using NBVAR(2) in my argument but I needed to use NBVAR(1) since (1) was the middle of the three BYTES.

    The DEBUG statements are changing based on inputs from my phone via the PINK.

    Now if I can just get the X-10 PL513 working. The LED is staying on all the time, even with no phone line plugged in.

    Thanks for all your help.

    falcon
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-12-10 16:25
    (0) is the first, (1) is the second, and (2) is the third
    I thought that you had a handle on that.

    I posted extensively on X10 and the PL513 a few years ago, but nothing comes up with the site's search.
    I guess there's a way to search the forum via google, but the address escapes me just now.

    Found It!
    http://forums.parallax.com/showthread.php?110688-X10-power-line-interface-to-BS2-Question!
Sign In or Register to comment.