'master program for maximite 'This is the final version for where master program controls the time the 'slave is on. 'This program operates a remote RF switch in either toggle or timed mode 'For toggle operation press "0" to "9" to select the slave then "2" on remote 'to turn on. "0" actually refers to slave 10. 'To turn off, press "0" to "9" to select slave then "3" 'green led 11 is lit when master is ready to receive input 'Additionally, led 1 thru led 10 will light to show when a slave has been selected. 'when toggled on the corresponding led will stay lit. When toggled 'off, the led will be off 'For timed mode press "0" to "9" on remote to select which slave. Then "1" to 'enter timed mode. Now enter the time in the form hhmmss. Leds 1 to 6 will 'light in succession as the time is entered. When the last digit has been 'entered the time will be sent to the corresponding slave and the master will 'be revert to being avaialable for more timing instructions 'Any digit 0 - 9 can be entered so a timing period of over 99 hours is possible. 'To cancel a timed operation select "0" to "9" to selct the desired slave 'then enter "4" Dim tim(2),id$(10),a(40) Sound 200000 For i = 1 To 10: Read id$(i):Next i Open "com2:1200" As #2 'open com2 serial port For i = 1 To 10:SetPin i,8:Next i:SetPin 14,8 'set for digital output loop0: 'IR input routine to get station# Pin(14)=1 GoSub irdecode 'get sony keypress for slave # Pin(14) = 0 'turn off ready led station = t If station > 10 Then GoTo loop0 'check valid station # Pin(station) = 1 'light up correct led for station Pin(14) = 1 'turn on ready led for next input loop2: GoSub irdecode 'get remote control function input Pin(14) = 0 function = t If function > 4 Then GoTo loop2 'check valid function # On function GoTo timed,turnon,turnoff,reset 'goto correct function routine GoTo loop0 timed: Pin(station) = 0:Pin(14) = 1 a$ = Input$(255,#2):a$ = "" 'clear input buffer Print#2, 85;id$(station);Chr$(2); 'send station id; 2 = timed Pin(14) = 0:Pause 500 a$ = Input$(255,#2):GoSub decode 'wait for ack (= "1") If (value$ <> Str$(station)) Or (code$ <> id$(station)) Then GoTo error1'not valid For i = 0 To 2 'this section gets time input via remote control tim$ = "" For j = 1 To 2 Pin(14) = 1 loop4: GoSub irdecode Pin(2*i+j) = 1 If t > 9 Then t = 0 'treat 10 & above as zero tim$ = tim$ + Str$(t) 'store digit Next j tim(i) = Val(tim$) 'get number of sec,min or hr Next i For i = 1 To 10: Pin(i) = 0:Next i 'this section sends the time signal a$ = Input$(255,#2):a$ = "" 'clear #2 input buffer Print#2,85;id$(station);Chr$(tim(0));Chr$(tim(1));Chr$(tim(2)); 'send to slave 1 Pause 500 'wait for response a$ = Input$(255,#2):GoSub decode 'wait for ack (= "1")" If (value$ <> Str$(station)) Or (CODE$ <> id$(station)) Then GoTo error1'not valid errror GoTo loop0 decode: 'subroutine to decode a$ code$ = Mid$(a$,2,2):value$ = Mid$(a$,4,1):Return error1: 'blink light on corresponding station LED if transmission problem For b0 = 1 To 5 Pin(station) = 1:Pause 100:Pin(station) = 0:Pause 100 Next b0 GoTo loop0 turnon: Pin(station) = 1 a$ = Input$(255,#2):a$ = "" Print#2, 85;id$(station);Chr$(1); Pause 500 a$ = Input$(255,#2):GoSub decode 'wait for ack (= "1") If (value$ <> Str$(station)) Or (code$ <> id$(station)) Then GoTo error1'not valid error GoTo loop0 turnoff: Pin(station) = 0 a$ = Input$(255,#2):a$ = "" Print#2, 85;id$(station);Chr$(0); 'send turn off signal Pause 500 a$ = Input$(255,#2):GoSub decode 'wait for ack (= "1") If (value$ <> Str$(station)) Or (code$ <> id$(station)) Then GoTo error1'not valid error GoTo loop0 reset: Pin(station) = 1 For k = 1 To 3 a$ = Input$(255,#2):a$ = "" For j = 1 To 3 Print#2, 85;id$(station);Str$(station); Next j Pause 500 a$ = Input$(255,#2):GoSub decode 'wait for ack (= "1") If (value$ <> Str$(station)) Or (code$ <> id$(station)) Then Next k Else Pin(station) = 0 GoTo loop0 EndIf GoTo error1 'this program decodes Sony & compatible IR remote controls irdecode: SetPin 15,7,update 'interrupt to detect -ve edge from remote ii = 0 flag = 0 loop1: 'wait in this loop for 1st -ve IR pulse from remote If ii > 40 Then 'detect when enough received SetPin 15,0 'turn off interrupt GoTo process 'process results EndIf If flag = 0 Then SetPin 12,5 'no pulses yet. Keep resetting counter GoTo loop1 update: a(ii) = Pin(12) 'record No of pulses ii = ii + 1 'increment counter flag = 1 'set flag to stop initialising counter IReturn process: For ii = 40 To 1 Step -1 'calculated pulse intervals a(ii) = a(ii) - a(ii-1) If a(ii) < 297 Then a(ii) = 0 'check if a zero If a(ii) > 296 And a(ii) < 476 Then a(ii) = 1 'check if a 1 Next ii For ii = 1 To 40 If a(ii) > 1000 Then GoTo exit1 'find first gap between codes Next ii 'no valid code received - no gap was found. Try again GoTo irdecode exit1: 'gap has been found t = 0 'initialise total ("1" is coded as zero;"2" as a 1 etc) ii = ii + 2 'jump header to firstdata bit (LSB) For jj = 0 To 6 'calculate value t = t + a(ii+jj)*2^jj Next jj If t > 127 Then GoTo irdecode 'not a valid Sony code t = t + 1 Pause 200 Return Data "A1","A2","A3","A4","A5","A6","A7","A8","A9","A0"