Question on CASE Statements
StevenL1124
Posts: 5
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
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
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.
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
or give it a little more time, like the 3 iterations allenlane5 suggested,
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