Infrared Remote Control Code
Hey guys. I'm a counselor at a summer computer camp, and we offer Robotics as a course. One of the projects in this course is to hook up some IR receivers and use a remote to control the computer. I don't know if Parallax makes the remote or if it's proprietary, so here's a picture of it:

Anyway, the way our camp gives us the code, the cars move in short bursts instead of continuously. I've been trying to optimize the code, but for the life of me I just cannot figure it out! Can anyone explain to me what in god's name is going on here?:

Anyway, the way our camp gives us the code, the cars move in short bursts instead of continuously. I've been trying to optimize the code, but for the life of me I just cannot figure it out! Can anyone explain to me what in god's name is going on here?:
'Version 030217
'{$STAMP BS2}
'{$PBASIC 2.5}
'
'
''''''''''''''''
' main routine '
''''''''''''''''
DEBUG "Reset",CR
DO WHILE 1 'loop forever
GOSUB getcode 'get remote signal code
DEBUG ?receive 'display which code was pressed
'and then perform an action
SELECT receive
CASE 770
'up button pressed
FOR x = 0 TO 25
PULSOUT 14, 750-150
PULSOUT 15, 750+150
PAUSE 20
NEXT
CASE 962
'down button pressed
FOR x = 0 TO 25
PULSOUT 14, 750+150
PULSOUT 15, 750-150
PAUSE 20
NEXT
CASE 834
'left button pressed
FOR x = 0 TO 25
PULSOUT 14, 750+150
PULSOUT 15, 750+150
PAUSE 20
NEXT
CASE 898
'right button pressed
FOR x = 0 TO 25
PULSOUT 14, 750-150
PULSOUT 15, 750-150
PAUSE 20
NEXT
CASE 850
'power button pressed
END
CASE 594
'mute button pressed
FREQOUT 1, 750, 1046 'C
FREQOUT 1, 750, 1567 'G
FREQOUT 1, 250, 1396 'F
FREQOUT 1, 250, 1318 'E
FREQOUT 1, 250, 1174 'D
FREQOUT 1, 700, 2092 'C
FREQOUT 1, 700, 1567 'G
FREQOUT 1, 250, 1396 'F
FREQOUT 1, 250, 1318 'E
FREQOUT 1, 250, 1174 'D
FREQOUT 1, 700, 2092 'C
FREQOUT 1, 700, 1567 'G
FREQOUT 1, 250, 1396 'F
FREQOUT 1, 250, 1318 'E
FREQOUT 1, 250, 1396 'F
FREQOUT 1, 700, 1174 'D
CASE 930
'enter button pressed
END
CASE 930
endcase:
END
ENDSELECT:
LOOP
''''''''''''''''''''''''''''''''''''''''''''
'subroutine for scanning for remote signal '
' do not change '
' '
' hook up IR receiver to pin 0 '
''''''''''''''''''''''''''''''''''''''''''''
getcode:
RECEIVE VAR Word
COPY VAR Word
IR_IN CON 0 'change this number
IR_Pin VAR IN0 'and this number
buffer VAR Word(4)
x VAR Byte
bitcount VAR Nib
beats VAR Nib
' spender VAR Bit 'max
copy = receive
' spender = 0
wait_for_clear:
IF IR_PIN = 0 THEN wait_for_clear
IF IR_PIN = 0 THEN wait_for_clear
IF IR_PIN = 0 THEN wait_for_clear
IF IR_PIN = 0 THEN wait_for_clear
IF IR_PIN = 0 THEN wait_for_clear
wait_for_signal:
IF IR_PIN = 1 THEN wait_for_signal
' why can't this be a part of the for loop?
' is it a timing problem? when I remove all of these
' and make them part of the for loop, it no longer reads all
' 64 bits, even though the foor loop goes from 1 to 63
' (i manually read the first one)
buffer.BIT0(0) = ~IR_PIN
buffer.BIT0(1) = ~IR_PIN
buffer.BIT0(2) = ~IR_PIN
buffer.BIT0(3) = ~IR_PIN
buffer.BIT0(4) = ~IR_PIN
buffer.BIT0(5) = ~IR_PIN
buffer.BIT0(6) = ~IR_PIN
buffer.BIT0(7) = ~IR_PIN
buffer.BIT0(8) = ~IR_PIN
buffer.BIT0(9) = ~IR_PIN
buffer.BIT0(10) = ~IR_PIN
buffer.BIT0(11) = ~IR_PIN
buffer.BIT0(12) = ~IR_PIN
buffer.BIT0(13) = ~IR_PIN
buffer.BIT0(14) = ~IR_PIN
buffer.BIT0(15) = ~IR_PIN
buffer.BIT0(16) = ~IR_PIN
buffer.BIT0(17) = ~IR_PIN
buffer.BIT0(18) = ~IR_PIN
buffer.BIT0(19) = ~IR_PIN
buffer.BIT0(20) = ~IR_PIN
buffer.BIT0(21) = ~IR_PIN
buffer.BIT0(22) = ~IR_PIN
buffer.BIT0(23) = ~IR_PIN
buffer.BIT0(24) = ~IR_PIN
buffer.BIT0(25) = ~IR_PIN
buffer.BIT0(26) = ~IR_PIN
buffer.BIT0(27) = ~IR_PIN
buffer.BIT0(28) = ~IR_PIN
buffer.BIT0(29) = ~IR_PIN
buffer.BIT0(30) = ~IR_PIN
buffer.BIT0(31) = ~IR_PIN
buffer.BIT0(32) = ~IR_PIN
buffer.BIT0(33) = ~IR_PIN
buffer.BIT0(34) = ~IR_PIN
buffer.BIT0(35) = ~IR_PIN
buffer.BIT0(36) = ~IR_PIN
buffer.BIT0(37) = ~IR_PIN
buffer.BIT0(38) = ~IR_PIN
buffer.BIT0(39) = ~IR_PIN
buffer.BIT0(40) = ~IR_PIN
buffer.BIT0(41) = ~IR_PIN
buffer.BIT0(42) = ~IR_PIN
buffer.BIT0(43) = ~IR_PIN
buffer.BIT0(44) = ~IR_PIN
buffer.BIT0(45) = ~IR_PIN
buffer.BIT0(46) = ~IR_PIN
buffer.BIT0(47) = ~IR_PIN
buffer.BIT0(48) = ~IR_PIN
buffer.BIT0(49) = ~IR_PIN
buffer.BIT0(50) = ~IR_PIN
buffer.BIT0(51) = ~IR_PIN
buffer.BIT0(52) = ~IR_PIN
buffer.BIT0(53) = ~IR_PIN
buffer.BIT0(54) = ~IR_PIN
buffer.BIT0(55) = ~IR_PIN
buffer.BIT0(56) = ~IR_PIN
buffer.BIT0(57) = ~IR_PIN
buffer.BIT0(58) = ~IR_PIN
buffer.BIT0(59) = ~IR_PIN
buffer.BIT0(60) = ~IR_PIN
buffer.BIT0(61) = ~IR_PIN
buffer.BIT0(62) = ~IR_PIN
buffer.BIT0(63) = ~IR_PIN
receive = 0
bitcount = 0
cutoff CON 3 'length of pulse to count as 1
FOR x = 1 TO 63
' why does this break when the following line and the buffer negations are removed?
' is this just to spend a clock cycle? I tried removing the tildes and spending the
' clock cycle a different way, and it didn't work
buffer.BIT0(x) = ~buffer.BIT0(x)
IF buffer.BIT0(x) = 1 THEN
IF buffer.BIT0(x-1) = 0 THEN
DEBUG "0 to 1, end of pulse.", CR
'0 to 1 transition (end of pulse)
beats = (beats - cutoff) >> 15
DEBUG "Shifted beats: ", DEC beats, " BIN: ", BIN beats,CR
receive = (receive << 1) | beats
DEBUG "Received: ", DEC receive, " BIN: ", BIN receive, CR
bitcount = bitcount + 1
DEBUG "Bitcount: ", DEC bitcount, " BIN: ", BIN bitcount, CR
ENDIF
ELSE
DEBUG "Low. Beats: ", DEC beats, CR
'pulse... count beats
IF buffer.BIT0(x-1) = 1 THEN
DEBUG "1 to 0, reset beats.", CR
beats = 0
ENDIF
beats = beats + 1 'add a beat
ENDIF
NEXT
' no idea why this is necessary
receive = ~(receive << (16 - bitcount & 15))
receive = receive >> 6
IF receive <> copy THEN getcode 'make sure code is received properly
RETURN

Comments
' {$STAMP BS2} ' {$PBASIC 2.5} time VAR Word DO PULSIN 0, 0, time IF time <> 0 THEN DEBUG DEC5 time, CR ENDIF LOOPIt just outputs the length of the received pulse. However, no matter what button is pressed on the remote, the SAME output seems to be generated. There's always something with in the range of 1200 and 1275, then something within the range of 300 and 400. For example, tapping left, up, right, down, generates this code:
(left tapped)
1258
450
1239
477
(up tapped)
1257
403
1230
451
(right tapped)
1258
450
1216
482
etc, etc.
There's no discernable pattern emerging based on the button pressed. What gives? It's as if that bitwise code I posted (given to us from HQ) works some kind of strange hocus-pocus on the numbers to create order from chaos.
confused...
Up- 220,460,700,900
Down-210,370,650,700
now you can see that both of these a relatively the same, there are two below and two above 500, but if you continue reading the bits sent in ull notice that there will be one where it is high then 500 and one lower then 500. Continue reading the PDF and see if you figure anything out.