Stuck trying to convert BS2 to SX with WORD VAR
Hello,
I am trying to convert a BS2 program from the understanding signals book to SX code. Reading the SX help file it looks like you cannot build an array with word. The compiler gives you an invalid parameter error. I thought I could just make individual VARiables ie; IR_pulse0 - IR_Pulse12 but, when it reaches IR_Pulse9 I get variable exceed available ram. What I want to do is to capture and indentify the key I pressed on my remote. The final objective is to build a learning remote to combine the 5 remotes I currently have. (I know I can buy one but this would be a great learning exersize.)· If I can ever get this working I will post the final project in the sand box. Here is the program I'm trying to convert. Comments from me are preceeded by ***
Thanks,
Jim W.
'
[noparse][[/noparse] Program Title and Description ]
' Understanding Signals - DecodeSonyIRRemote.bs2
' Decode 38 kHz Sony IR TV remote control signal.
' Author: Andy Lindsay, Parallax, Inc.
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[noparse][[/noparse] I/O Definitions ]
IR_detect········ PIN··· 8·················· ' IR detector output -> P8.
'
[noparse][[/noparse] Constants ]
active_high······ CON··· 1·················· ' Used to set PULSIN commands
active_low······· CON··· 0·················· ' to detect +/- pulses.
'
[noparse][[/noparse] Variables ]
' This program reads all the pulses delivered by the remote, but in
' practice, only the first two to five pulses are required.· This can be
' used To save seven To 9 Words in RAM (in this section) and the same
' number OF PULSIN commands in the Process IR Pulses subroutine.
IR_pulse········· VAR··· Word(12)·· '***This will cause Invalid parameter
counter·········· VAR··· Nib·'*** SX doesn't seem to like Nib's changed to bytes
type············· VAR··· Nib
IR_message······· VAR··· Byte
'
[noparse][[/noparse] Initialization ]
DEBUG CLS··································· ' BOE reset clears display.
'
[noparse][[/noparse] Main Routine ]
DO
· DO········································ ' Wait for IR detector output
· LOOP UNTIL IR_Detect = 0·················· ' to go low.
· GOSUB Display_Heading
· GOSUB Find_and_Display_Start_Pulse
· GOSUB Process_IR_Pulses
· GOSUB Display_IR_Pulse_Values
· GOSUB Convert_to_Binary_Number_Display
LOOP
'
[noparse][[/noparse] Subroutine - Display Heading in Debug Terminal ]
Display_Heading:
· DEBUG HOME·················· ·' *** I can·display stuff like this on my hc4led
· DEBUG "IR Remote Messages ", CR, CR
· DEBUG "Pulse Duration Value", CR
· DEBUG "
", CR
· RETURN
'
[noparse][[/noparse] Subroutine - Find and Display Start Pulse ]
' Packets are delivered around 20 times/second while a given button on the
' remote is pressed and held.· This program extracts a start pulse from
' an earlier packet.· The Process IR Pulses subroutine pickes up the rest
' of the pulse values a few packets later.· In remote controlled
' the duration of the start pulse can simply be discarded.
Find_and_Display_Start_Pulse:
· FOR counter = 0 TO 15
··· PULSIN IR_detect,active_low,IR_pulse(0)
··· IF IR_pulse(0) > 900 THEN
····· DEBUG "Start"
····· DEBUG " = ", DEC5 IR_pulse(0) * 2, " us "
····· DEBUG " Start Bit", CR
····· EXIT·································· ' Exit FOR...NEXT after start
··· ENDIF··································· ' pulse is detected.
· NEXT
· RETURN
'
[noparse][[/noparse] Subroutine - Process IR Pulses ]
Process_IR_Pulses:
· DO
··· PULSIN IR_detect,active_high,IR_pulse(0)
· LOOP UNTIL (IR_pulse(0) > 1400) AND (IR_pulse(0) <> 0)
· ' The BASIC Stamp 2p and 2SX modules are fast enough to load these
· ' values using a FOR...NEXT loop, but all other modules should load the
· ' pulse values as a sequence of PULSIN Commands.
· PULSIN IR_detect,active_low,IR_pulse(0)'*** Here is where I'm Stuck
· PULSIN IR_detect,active_low,IR_pulse(1)
· PULSIN IR_detect,active_low,IR_pulse(2)
· PULSIN IR_detect,active_low,IR_pulse(3)
· PULSIN IR_detect,active_low,IR_pulse(4)
· PULSIN IR_detect,active_low,IR_pulse(5)
· PULSIN IR_detect,active_low,IR_pulse(6)
· PULSIN IR_detect,active_low,IR_pulse(7)
· PULSIN IR_detect,active_low,IR_pulse(8)
· PULSIN IR_detect,active_low,IR_pulse(9)
· PULSIN IR_detect,active_low,IR_pulse(10)
· PULSIN IR_detect,active_low,IR_pulse(11)
· RETURN
'
[noparse][[/noparse] Subroutine - Display IR Pulse Values ]
Display_IR_Pulse_Values:
· FOR counter = 0 TO 10
··· DEBUG " ", DEC2 counter
··· DEBUG " = ", DEC5 IR_pulse(counter) * 2, " us "
··· IF IR_pulse(counter) > 450 THEN
····· DEBUG " Binary-1", CR
··· ELSE
····· DEBUG " Binary-0", CR
··· ENDIF
· NEXT
· RETURN
'
[noparse][[/noparse] Subroutine - Convert to Binary Number Display ]
Convert_to_Binary_Number_Display: ' Subroutine
· FOR counter = 0 TO 10
··· IF (IR_pulse(counter) < 450) THEN
····· IR_message.LOWBIT(counter) = 0
··· ELSE
····· IR_message.LOWBIT(counter) = 1
··· ENDIF
· NEXT
· DEBUG CR,CR,"Binary Value: ", BIN8 IR_message, CR
· DEBUG "Decimal Value: ", DEC3 IR_message, CR
· DEBUG "Without bit-7: "
· DEBUG " ",DEC3 IR_message & %01111111,CR
· RETURN
I am trying to convert a BS2 program from the understanding signals book to SX code. Reading the SX help file it looks like you cannot build an array with word. The compiler gives you an invalid parameter error. I thought I could just make individual VARiables ie; IR_pulse0 - IR_Pulse12 but, when it reaches IR_Pulse9 I get variable exceed available ram. What I want to do is to capture and indentify the key I pressed on my remote. The final objective is to build a learning remote to combine the 5 remotes I currently have. (I know I can buy one but this would be a great learning exersize.)· If I can ever get this working I will post the final project in the sand box. Here is the program I'm trying to convert. Comments from me are preceeded by ***
Thanks,
Jim W.
'
[noparse][[/noparse] Program Title and Description ]
' Understanding Signals - DecodeSonyIRRemote.bs2
' Decode 38 kHz Sony IR TV remote control signal.
' Author: Andy Lindsay, Parallax, Inc.
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[noparse][[/noparse] I/O Definitions ]
IR_detect········ PIN··· 8·················· ' IR detector output -> P8.
'
[noparse][[/noparse] Constants ]
active_high······ CON··· 1·················· ' Used to set PULSIN commands
active_low······· CON··· 0·················· ' to detect +/- pulses.
'
[noparse][[/noparse] Variables ]
' This program reads all the pulses delivered by the remote, but in
' practice, only the first two to five pulses are required.· This can be
' used To save seven To 9 Words in RAM (in this section) and the same
' number OF PULSIN commands in the Process IR Pulses subroutine.
IR_pulse········· VAR··· Word(12)·· '***This will cause Invalid parameter
counter·········· VAR··· Nib·'*** SX doesn't seem to like Nib's changed to bytes
type············· VAR··· Nib
IR_message······· VAR··· Byte
'
[noparse][[/noparse] Initialization ]
DEBUG CLS··································· ' BOE reset clears display.
'
[noparse][[/noparse] Main Routine ]
DO
· DO········································ ' Wait for IR detector output
· LOOP UNTIL IR_Detect = 0·················· ' to go low.
· GOSUB Display_Heading
· GOSUB Find_and_Display_Start_Pulse
· GOSUB Process_IR_Pulses
· GOSUB Display_IR_Pulse_Values
· GOSUB Convert_to_Binary_Number_Display
LOOP
'
[noparse][[/noparse] Subroutine - Display Heading in Debug Terminal ]
Display_Heading:
· DEBUG HOME·················· ·' *** I can·display stuff like this on my hc4led
· DEBUG "IR Remote Messages ", CR, CR
· DEBUG "Pulse Duration Value", CR
· DEBUG "
", CR
· RETURN
'
[noparse][[/noparse] Subroutine - Find and Display Start Pulse ]
' Packets are delivered around 20 times/second while a given button on the
' remote is pressed and held.· This program extracts a start pulse from
' an earlier packet.· The Process IR Pulses subroutine pickes up the rest
' of the pulse values a few packets later.· In remote controlled
' the duration of the start pulse can simply be discarded.
Find_and_Display_Start_Pulse:
· FOR counter = 0 TO 15
··· PULSIN IR_detect,active_low,IR_pulse(0)
··· IF IR_pulse(0) > 900 THEN
····· DEBUG "Start"
····· DEBUG " = ", DEC5 IR_pulse(0) * 2, " us "
····· DEBUG " Start Bit", CR
····· EXIT·································· ' Exit FOR...NEXT after start
··· ENDIF··································· ' pulse is detected.
· NEXT
· RETURN
'
[noparse][[/noparse] Subroutine - Process IR Pulses ]
Process_IR_Pulses:
· DO
··· PULSIN IR_detect,active_high,IR_pulse(0)
· LOOP UNTIL (IR_pulse(0) > 1400) AND (IR_pulse(0) <> 0)
· ' The BASIC Stamp 2p and 2SX modules are fast enough to load these
· ' values using a FOR...NEXT loop, but all other modules should load the
· ' pulse values as a sequence of PULSIN Commands.
· PULSIN IR_detect,active_low,IR_pulse(0)'*** Here is where I'm Stuck
· PULSIN IR_detect,active_low,IR_pulse(1)
· PULSIN IR_detect,active_low,IR_pulse(2)
· PULSIN IR_detect,active_low,IR_pulse(3)
· PULSIN IR_detect,active_low,IR_pulse(4)
· PULSIN IR_detect,active_low,IR_pulse(5)
· PULSIN IR_detect,active_low,IR_pulse(6)
· PULSIN IR_detect,active_low,IR_pulse(7)
· PULSIN IR_detect,active_low,IR_pulse(8)
· PULSIN IR_detect,active_low,IR_pulse(9)
· PULSIN IR_detect,active_low,IR_pulse(10)
· PULSIN IR_detect,active_low,IR_pulse(11)
· RETURN
'
[noparse][[/noparse] Subroutine - Display IR Pulse Values ]
Display_IR_Pulse_Values:
· FOR counter = 0 TO 10
··· DEBUG " ", DEC2 counter
··· DEBUG " = ", DEC5 IR_pulse(counter) * 2, " us "
··· IF IR_pulse(counter) > 450 THEN
····· DEBUG " Binary-1", CR
··· ELSE
····· DEBUG " Binary-0", CR
··· ENDIF
· NEXT
· RETURN
'
[noparse][[/noparse] Subroutine - Convert to Binary Number Display ]
Convert_to_Binary_Number_Display: ' Subroutine
· FOR counter = 0 TO 10
··· IF (IR_pulse(counter) < 450) THEN
····· IR_message.LOWBIT(counter) = 0
··· ELSE
····· IR_message.LOWBIT(counter) = 1
··· ENDIF
· NEXT
· DEBUG CR,CR,"Binary Value: ", BIN8 IR_message, CR
· DEBUG "Decimal Value: ", DEC3 IR_message, CR
· DEBUG "Without bit-7: "
· DEBUG " ",DEC3 IR_message & %01111111,CR
· RETURN
Comments
Ray
This is one of thoses cases where "what's best for the basic stamp is not best of the SX".
The stamp must use this method because of the delay it takes to load and decode the next line of the program would cause it to miss the next pulse.
The SX is easily capable of making a measurement in a loop then decide if the bit is a startbit, one bit, or zero bit act accordingly.
Actually the "best" way to read IR is with an interrupt.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheap 4-digit LED display with driver IC·www.hc4led.com
Low power SD Data Logger www.sddatalogger.com
"You're braver than you believe, stronger than you seem, and smarter than you think" Christopher Robin to Pooh
·
I'll go back to the drawing board. I tried a search the other day but I guess I didn't use the right keywords. The IR remote did bring a lot of info that I will sift through tonight. Hopefully, my super duper learning remote will not be so remote soon.
Thanks again,
Jim W.
·· The we have provided an enhanced search engine which is detailed at the following link.· I hope this helps.
http://forums.parallax.com/showthread.php?p=562370
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
There should be some way to point IS at the database used by the forum... I think...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
---
James Newton, Host of SXList.com
james at sxlist,com 1-619-652-0593 fax:1-208-279-8767
SX FAQ / Code / Tutorials / Documentation:
http://www.sxlist.com Pick faster!