' {$STAMP BS2} ' {$PBASIC 2.5} ' -----[ I/O Definitions ]------------------------------------------------- ' Inputs IRDet PIN 7 E_Mode PIN 0 ' Outputs Buzzer PIN 1 BankA PIN 8 BankB PIN 9 BankC PIN 10 HB_A PIN 5 HB_B PIN 6 ' -----[ Constants ]------------------------------------------------------- ' Pulse duration constants for SONY remote. ThresholdStart CON 1000 ' Message rest vs. data rest ThresholdPulse CON 500 ' Binary 1 vs. 0 for PULSIN ThresholdEdge CON 300 ' Binary 1 vs. 0 for RCTIME ' -----[ Variables ]------------------------------------------------------- ' SONY TV IR remote variables irPulse VAR Word ' Stores pulse widths remoteCode VAR Byte ' Stores remote code level_flag VAR Nib ' Stores the current brightness of the lamp mode_flag VAR Nib ' Stores the current mode of the lamp idx VAR Byte ' -----[ Initigalization ]-------------------------------------------------- DIRL = %01100010 ' Set pin directions DIRH = %00000111 level_flag = 3 ' Preset level flag to mid intensity mode_flag = 1 ' Set mode to off GOSUB Set_Brightness ' -----[ Main Routine ]---------------------------------------------------- ' Replace this button testing DO...LOOP with your own code. DO ' Main DO...LOOP GOSUB Get_Ir_Remote_Code ' Call remote code subroutine GOSUB Check_E_Mode SELECT remoteCode CASE 21 IF mode_flag = 1 THEN mode_flag = 0 level_flag = 0 GOSUB Set_Brightness ELSE mode_flag = 1 level_flag = 3 GOSUB Set_Brightness ENDIF CASE 18 IF mode_flag = 1 THEN IF level_flag < 5 THEN level_flag = level_flag + 1 GOSUB Set_Brightness ENDIF ENDIF CASE 19 IF mode_flag = 1 THEN IF level_flag > 0 THEN level_flag = level_flag - 1 GOSUB Set_Brightness ENDIF ENDIF ENDSELECT PAUSE 180 LOOP ' Repeat main DO...LOOP ' -----[ Subroutine - Set_Brightness ]------------------------------------- Set_Brightness: SELECT level_flag CASE 0 BankA = 0 BankB = 0 BankC = 0 HB_A = 0 HB_B = 0 CASE 1 BankA = 1 BankB = 0 BankC = 0 HB_A = 0 HB_B = 0 CASE 2 BankA = 1 BankB = 1 BankC = 0 HB_A = 0 HB_B = 0 CASE 3 BankA = 1 BankB = 1 BankC = 1 HB_A = 0 HB_B = 0 CASE 4 BankA = 0 BankB = 0 BankC = 0 HB_A = 1 HB_B = 0 CASE 5 BankA = 0 BankB = 0 BankC = 0 HB_A = 1 HB_B = 1 ENDSELECT RETURN ' -----[ Subroutine - Get_Ir_Remote_Code ]--------------------------------- ' SONY TV IR remote subroutine loads the remote code into the ' remoteCode variable. Get_Ir_Remote_Code: remoteCode = 0 'DO ' Wait for end of resting state. RCTIME IrDet, 1, irPulse 'LOOP UNTIL irPulse > ThresholdStart IF irPulse > ThresholdStart THEN PULSIN IrDet, 0, irPulse ' Get data pulses. IF irPulse > ThresholdPulse THEN remoteCode.BIT0 = 1 RCTIME IrDet, 0, irPulse IF irPulse > ThresholdEdge THEN remoteCode.BIT1 = 1 RCTIME IrDet, 0, irPulse IF irPulse > ThresholdEdge THEN remoteCode.BIT2 = 1 RCTIME IrDet, 0, irPulse IF irPulse > ThresholdEdge THEN remoteCode.BIT3 = 1 RCTIME IrDet, 0, irPulse IF irPulse > ThresholdEdge THEN remoteCode.BIT4 = 1 RCTIME IrDet, 0, irPulse IF irPulse > ThresholdEdge THEN remoteCode.BIT5 = 1 RCTIME IrDet, 0, irPulse IF irPulse > ThresholdEdge THEN remoteCode.BIT6 = 1 ENDIF RETURN ' -----[ Subroutine - Check_E_Mode ]--------------------------------- Check_E_Mode: DO WHILE E_Mode GOSUB MORSE_S GOSUB MORSE_O GOSUB MORSE_S PAUSE 1000 LOOP RETURN ' -----[ Subroutine - Morse Codes ]--------------------------------- MORSE_S: FOR idx = 0 TO 2 HIGH HB_A HIGH HB_B PAUSE 100 LOW HB_A LOW HB_B PAUSE 150 NEXT RETURN MORSE_O: HIGH Buzzer FOR idx = 0 TO 2 HIGH HB_A HIGH HB_B PAUSE 300 LOW HB_A LOW HB_B PAUSE 150 NEXT LOW Buzzer RETURN