Shop OBEX P1 Docs P2 Docs Learn Events
Question on CASE Statements — Parallax Forums

Question on CASE Statements

StevenL1124StevenL1124 Posts: 5
edited 2010-05-18 20:47 in BASIC Stamp
this is a segment of code from my groups senior design project, and we seem to have run into a little trouble...

the code works (for the most part) as we intend, however we are having minor issues with one small part, and I believe it has to do with my limited knowledge about CASE statements. At the bottom of this post, you will find a bit of our code. The problem we run into is that we want the subroutine to have two cases, one where we press the power button on a sony IR TV remote, and It jumps us to another portion of the program (which works great). The other case we want is that if the remote doesn't detect any signal from the remote, to run the subroutine labeled " Subroutine - Tracking System." What we found is that the "CASE ELSE" statement isn't exactly what were looking for (I think), as subroutine "Tracking System" only works if the system detects signal from the remote that is NOT the power button. Basically, if we want that subroutine to run, I have to hold down a random button (that isn't the power button). Can anyone help out with this? THANKS SO MUCH!

Steven


CODE:





'
[noparse][[/noparse] Subroutine - Get_Ir_Remote_Code ]
' SONY TV IR remote subroutine loads the remote code into the
' remoteCode variable.
Get_Ir_Remote_Code:
remoteCode = 0 ' Clear all bits in remoteCode
' Wait for resting state between messages to end.
DO
RCTIME IrDet, 1, irPulse
LOOP UNTIL irPulse > 1000
' Measure start pulse. If out of range, then retry at Get_Ir_Remote_Code.
RCTIME 1, 0, irPulse
IF irPulse > 1125 OR irPulse < 675 THEN GOTO Get_Ir_Remote_Code
' Get data bit pulses.
RCTIME IrDet, 0, irPulse ' Measure pulse
IF irPulse > 300 THEN remoteCode.BIT0 = 1 ' Set (or leave clear) bit-0
RCTIME IrDet, 0, irPulse ' Measure next pulse
IF irPulse > 300 THEN remoteCode.BIT1 = 1 ' Set (or leave clear) bit-1
RCTIME IrDet, 0, irPulse ' etc
IF irPulse > 300 THEN remoteCode.BIT2 = 1
RCTIME IrDet, 0, irPulse
IF irPulse > 300 THEN remoteCode.BIT3 = 1
RCTIME IrDet, 0, irPulse
IF irPulse > 300 THEN remoteCode.BIT4 = 1
RCTIME IrDet, 0, irPulse
IF irPulse > 300 THEN remoteCode.BIT5 = 1
RCTIME IrDet, 0, irPulse
IF irPulse > 300 THEN remoteCode.BIT6 = 1
' Adjust remoteCode so that keypad keys correspond to the value
' it stores.
IF (remoteCode < 10) THEN remoteCode = remoteCode + 1
IF (remoteCode = 10) THEN remoteCode = 0
RETURN

'
[noparse][[/noparse] Subroutine - Tracking System ]
Tracking:

PAUSE 200

DO
GOSUB Get_Ir_Remote_Code
SELECT remoteCode


CASE Power
PAUSE 500
DEBUG "power pressed"
GOSUB Main



CASE ELSE <<< LOOK HERE!!!
IF (irdetectleft = 0) AND (irdetectright = 0) THEN 'NO detection


ELSEIF (irdetectleft = 0) THEN 'detection on bottom
IF (position < 950) THEN

GOSUB pan_dwn
LOW 0

ELSE
DEBUG "DOWN LIMIT "

ENDIF


ELSEIF (irdetectright = 0) THEN 'detection on top
IF (position > 610) THEN

GOSUB pan_up
LOW 0

ELSE
DEBUG "TOP LIMIT "
ENDIF


ENDIF

' GOSUB check_power
GOSUB check_irs


'CASE Power
'GOSUB Main

ENDSELECT



LOOP

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2010-05-18 16:27
    Well, that "DO" loop at the top of "Get_IR_Remote_Code" will run UNTIL it sees some kind of IR signal.
    You probably want to increment a variable there, and when the variable is 3 or so, return with
    RemoteCode set to zero. Otherwise, you'll HAVE to hit a button on the remote to get out of that loop.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-05-18 16:41
    The subroutine to read the Ir is going to lock up in its first DO:LOOP, until it sees activity on the remote.
    Get_Ir_Remote_Code:
    remoteCode = 0 ' Clear all bits in remoteCode
    ' Wait for resting state between messages to end.
    DO
    RCTIME IrDet, 1, irPulse
    LOOP UNTIL irPulse > 1000
    ' ...
    



    If it needs to detect the lack of a signal, it will need a timeout in that loop. How long? The "tracking" structure with the long mix of SELECT and IF statements is confusing.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • StevenL1124StevenL1124 Posts: 5
    edited 2010-05-18 19:59
    oh I see, so how would I go about creating a timeout? I'm thinking a very small delay would be best, as the tracking system should be tracking as rapidly as possible.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-05-18 20:47
    The RCTIME command creates its own timeout. It returns a value of zero if it times out if no edge is detected within 0.131071 second. So you might do something like this:
    look4irActivity:
      RCTIME IrDet, 1, irPulse
      IF irPulse=0 THEN GOTO importantTrackingStuff   ' come back ASAP to look4irActivity
    '  ... if here, there is IR activity, look for start bit
    



    or give it a little more time, like the 3 iterations allenlane5 suggested,
    ook4irActivity:
    FOR idx=1 to 3   ' give it about 0.4 second to look for a code.
       RCTIME IrDet, 1, irPulse
       IF irPulse THEN GOTO look4StartBit  ' this could have its own timeout
    NEXT
    GOTO  importantTrackingStuff  '  ' come back ASAP to look4irActivity
    



    Your program will be ping-ponging rapidly between two modes, looking for and decoding IR, and the tracking. Don't give it any opportunity to linger in either mode.

    What kind of remote is this, timing of the pulses?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.