Shop OBEX P1 Docs P2 Docs Learn Events
How Is This Possible???? — Parallax Forums

How Is This Possible????

NavicNavic Posts: 38
edited 2010-01-27 03:37 in BASIC Stamp
I have a BS2px hooked up to 3 sets of IR TX/RX's that work just fine. I'm transmitting data via a serial connection to another device, and after that data is sent the BS2px waits for a received byte. Everything seems to work just fine until two of my four conditionals repeat instantly every time for no apparent reason. Here is my code:

IRL VAR Byte
IRR VAR Byte
IRF VAR Byte
ard VAR Byte

DO
  GOSUB IR_Left
  GOSUB IR_Right
  GOSUB IR_Front
  IF IRL = 0 THEN
    DEBUG "Left", CR
    SEROUT 0, 396, [noparse][[/noparse]"2"]
    PAUSE 50
    SERIN 1, 396, [noparse][[/noparse]WAIT("@"), ard]
    DEBUG "Response from Left", CR
  ELSEIF IRR = 0 THEN
    DEBUG "Right", CR
    SEROUT 0, 396, [noparse][[/noparse]"3"]
    PAUSE 50
    SERIN 1, 396, [noparse][[/noparse]WAIT("@"), ard]
    DEBUG "Response from Right", CR
  ELSEIF IRF = 0 THEN
    DEBUG "Front", CR
    SEROUT 0, 396, [noparse][[/noparse]"4"]
    PAUSE 50
    SERIN 1, 396, [noparse][[/noparse]WAIT("@"), ard]
    DEBUG "Response from Front", CR
  ELSEIF (IRL = 0) AND (IRR = 0) THEN
    DEBUG "Both", CR
    SEROUT 0, 396, [noparse][[/noparse]"1"]
    PAUSE 50
    SERIN 1, 396, [noparse][[/noparse]WAIT("@"), ard]
    DEBUG "Response from Both", CR
  ENDIF
  IRF = 1
  IRL = 1
  IRR = 1
LOOP
END


IR_Left:
  FREQOUT 7,6,6385
  IRL = IN6
  RETURN


IR_Right:
  FREQOUT 5,6,6385
  IRR = IN4
  RETURN


IR_Front:
  FREQOUT 15,6,6385
  IRF = IN14
  RETURN





Here's what I get in my debug window when this is running and the 'front' condition is met:

Front
Front
Response from Front

This happens with the 'both' condition also:

Both
Both
Response from Both

But when 'left' or 'right' conditions are met, everything works as I envisioned:

Left
Response from Left

Right
Response from Right

I've tried reordering the IF statements, setting IRR, IRL, IRF to 1 before looping (as you can see), tried adding timeout to the SERIN to jump to a label that pauses for 5 seconds, pausing randomly during the code, changing the GOSUB orders, I just can't see why 'front' and 'both' repeat readings! Every time it should wait for a response, 'left' and 'right' work fine, what is going on here??

Thanks for your help, I'm not a master programmer but I think my code is correct.... this is driving me crazy!!

Comments

  • Spiral_72Spiral_72 Posts: 791
    edited 2010-01-25 17:47
    Hello.

    For what it's worth: See if this works. It's your code written in a different way. I've never been a big fan of multiple else conditions. It seems they have never worked properly for me. Also, I can't understand why it ever run "Both" in the first place!? It should have read your Left condition and dropped out of the IF-ELSE block. Wierd..... anyways, that's why I put the "Both" test first.


    IRL VAR Byte
    IRR VAR Byte
    IRF VAR Byte
    ard VAR Byte

    main:
    GOSUB IR_Left
    GOSUB IR_Right
    GOSUB IR_Front

    IF (IRL = 0) AND (IRR = 0) THEN
    DEBUG "Both", CR
    SEROUT 0, 396, [noparse][[/noparse]"1"]
    PAUSE 50
    SERIN 1, 396, [noparse][[/noparse]WAIT("@"), ard]
    DEBUG "Response from Both", CR
    goto main

    IF IRL = 0 THEN
    DEBUG "Left", CR
    SEROUT 0, 396, [noparse][[/noparse]"2"]
    PAUSE 50
    SERIN 1, 396, [noparse][[/noparse]WAIT("@"), ard]
    DEBUG "Response from Left", CR
    goto main

    IF IRR = 0 THEN
    DEBUG "Right", CR
    SEROUT 0, 396, [noparse][[/noparse]"3"]
    PAUSE 50
    SERIN 1, 396, [noparse][[/noparse]WAIT("@"), ard]
    DEBUG "Response from Right", CR
    goto main

    IF IRF = 0 THEN
    DEBUG "Front", CR
    SEROUT 0, 396, [noparse][[/noparse]"4"]
    PAUSE 50
    SERIN 1, 396, [noparse][[/noparse]WAIT("@"), ard]
    DEBUG "Response from Front", CR
    goto main

    IRF = 1
    IRL = 1
    IRR = 1
    goto main
    END


    IR_Left:
    FREQOUT 7,6,6385
    IRL = IN6
    RETURN


    IR_Right:
    FREQOUT 5,6,6385
    IRR = IN4
    RETURN


    IR_Front:
    FREQOUT 15,6,6385
    IRF = IN14
    RETURN
  • NavicNavic Posts: 38
    edited 2010-01-25 18:27
    Although this looks like my first version, I did paste it in (added the ENDIFs) and gave it a shot - same thing. I originally did use an IF...ENDIF for each condition and changed to the ELSEIFs later. I was hoping it would work due to the manual looping with GOTO rather than the DO...LOOP but unfortunately it does still repeat 'front' and 'both'
  • NavicNavic Posts: 38
    edited 2010-01-25 18:42
    Added a PAUSE 1000 to the beginning of main, before the GOSUB readings and something interesting happens: The left and right work fine but the both and front do not show the debug message reporting the receipt of the SERIN command as well as operating only once instead of twice!! The 1 second pause is completely inefficient for my project so that must go, but seeing the strangeness in slowing down the operation stumps me even more!
  • stamptrolstamptrol Posts: 1,731
    edited 2010-01-25 20:13
    Haven't had a chance to go thru the logic step by step, but I'd get rid of the PAUSE 50 after the SEROUT.

    SERIN takes some execution time to set up, so the extra time may be causing you to miss the WAIT character within a particular loop.

    Cheers,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • NavicNavic Posts: 38
    edited 2010-01-25 21:18
    Same results.
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-01-25 23:42
    1. What is sending data to pin 1? If that pin is "open", it will recieve 'noise', which might give it a false 'hit'.

    2. Have you connected a ground to the device sending to pin 1? If not, that signal may always look like 'noise'.
  • NavicNavic Posts: 38
    edited 2010-01-25 23:58
    The pin is connected to an Arduino, all grounds are connected. It's just the 'both' and 'front' conditions that are repeating, the other two 'left' and 'right' work perfectly all the time.
  • FranklinFranklin Posts: 4,747
    edited 2010-01-26 03:40
    Try running it without the serout and serin code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-01-26 14:04
    Does the Ardwino 'drive' the signal all the time? Or does it let it 'float' at times?

    Oh, and in your original code, 'Both' would never be reached, but you already knew that.
  • NavicNavic Posts: 38
    edited 2010-01-26 21:03
    Franklin - I commented out the serout and serin, front and both are still generated twice in a row while left and right work fine. allanlane - I don't know what driving and floating are, but it sounds like that has something to do with it because the Arduino takes longer to respond from the front and both data than the left and right... Could you please explain driving and floating? Thanks guys!!!
  • FranklinFranklin Posts: 4,747
    edited 2010-01-26 21:58
    Could you use the POST REPLY button and the attachment manager to attach the actual program you are loading into the stamp? That way we could run it and see what we get. Leave the serin/serout commands commented out. Thanks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • NavicNavic Posts: 38
    edited 2010-01-26 22:07
    Here's the attached program.

    Post Edited (Navic) : 1/26/2010 11:07:06 PM GMT
  • FranklinFranklin Posts: 4,747
    edited 2010-01-27 03:37
    ·This is what I get if I remove the serial commands. Don't see repeats
    Both
    Serial going Out for Both
    Response from Both
    Front
    Serial going Out for Front
    Response from Front
    Left
    Serial going Out for Left
    Response from Left
    Right
    Serial going Out for Right
    Response from Right
    Both
    Serial going Out for Both
    Response from Both
    Front
    Serial going Out for Front
    Response from Front
    Left
    Serial going Out for Left
    Response from Left
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
Sign In or Register to comment.