2-outlet Alarm Receiver Picaxe Basic Program

Declarations and Data

#rem 2_Outlet__Receiver.bas This is the receiver in a low beige plastic case'

It contains a duplex receptacle and a speaker.

The audio amp has a thermistor on its heatsink, and there is a fan.

The red LED is power, the blue LED shows message received.

The receptacle nearest the power connector is RL1 and is for the ceiling light.

The other receptacle is for the bluetooth speaker.

The electronics unit has a 434 MHz receiver which receives 2400 Hz messages. 

The applicable messages start with an unlock code "14L1776", then a key code character.

This program accepts messages with the following key codes:

R(on),G(off)    RL1 controls the ceiling light. They originate from manual transmitters.

Q(open).B(closed)    These originate from the RF magnetic switch fastened to the side gatepost

            They play tunes and also operate RL1.

S(on),U(off)    These control RL2.They originate from manual transmitters

h            Sounds the siren.

John Saunders 1/18/2019, 4/14/2020 Changed RL2 codes

#endrem


#picaxe 14M2


'Input Ports

symbol Thermister   = B.4        'Voltage decreases with increasing audio amplifier heatsink temperature

symbol Rcvr_In      = B.5        'Idle low; from receiver digital output

symbol DTR          = C.3        'Not used, pin 4 of DA0S


'Output Ports

symbol LED           = B.1        'High turns the blue LED on

symbol RL1          = B.2        'High turns the outlet on

symbol RL2          = B.3        'High turns the outlet on

symbol Aud_Cont     = C.1        'Low enables audio output

symbol Aud_Tune     = C.2        'Square waves to the audio amp

symbol Fan          = C.4        'High turns the fan on

    

'Variables 

'Interrupt variables

symbol Temp         = b1

symbol Comma        = b2        'Will be CR for a command        

symbol Rcvr_Val     = b3        'Command character

symbol LenCode      = b4        'Will be LF for a command


'Specific variable

symbol Gate_Alarm   = bit0        'Gate open message was received

symbol Siren_Alarm  = bit1        'Siren message was received

symbol LED_Strobe   = bit2

symbol LED_Lit      = bit3

symbol Loop_Cnt     = b7        'Count of main loop

symbol Seconds        = b8

symbol Key_Code     = b9        'The key command of the last transmission

symbol Tune_Indx    = b10        'Index into the tune branch table; 0 - 9

symbol Temperature  = b11        'ADC output

symbol New_Msg      = b12        'Command of a new message


'General variables

symbol Char            = b14



'Constants

symbol TooHot         = 70        'Too hot to touch, Shuts off the audio amp

symbol HighTemp          = 110        'Hot to touch, turns on the fan

symbol LowTemp           = 121        'Warm, turns off the fan

symbol Gate_Open_tune    = 2

symbol Gate_Closed_tune  = 5

symbol Emergency_tune    = 4

symbol Init_Tune         = 1

symbol Loops         = 9        'Loops per second

symbol Gate_Count     = 24        'Seconds the ceiling light is on when the the gate open message is received

symbol Alarm_Count     = 70        'Seconds the ceiling light is on when the the siren message is received

Init and  Main

Init:

SETFREQ m8

DIRSB=%00001111

DIRSC=%00010110

HIGH Aud_Cont

LOW Aud_Tune

LOW RL1

LOW RL2

LOW Fan

LET Loop_Cnt  = 10

LET New_Msg = 11            'Invalid value

LET Tune_Indx = Init_Tune

LET Key_Code = "z"

LET Rcvr_Val = "z"

LET Gate_Alarm = 0

LET Siren_Alarm = 0

LET LED_Strobe = 1

SETINT %00000001,%00000001    'Interrupt when C.0 high


main:

PAUSE 196

READADC Thermister,Temperature    'Audio Amp heat sink

IF Temperature < HighTemp THEN 

    HIGH Fan

ENDIF

IF Temperature > LowTemp THEN 

    LOW Fan

ENDIF

INC Loop_Cnt

IF Loop_Cnt >= Loops THEN

    LET Loop_Cnt = 0

    IF LED_Lit = 1 THEN

        LOW LED

        LET LED_Lit = 0

        LET LED_Strobe = 0

    ENDIF

    IF LED_Strobe = 1 THEN

        HIGH LED

        LET LED_Lit = 1

    ENDIF

    IF Seconds > 0 THEN

        DEC Seconds

    ENDIF

    IF Tune_Indx > 0 THEN 

        GOSUB Make_Sound

    ENDIF

ENDIF

IF Gate_Alarm = 1 AND Seconds = 0 THEN

    LET Gate_Alarm = 0

    LOW RL1

ENDIF

IF Siren_Alarm = 1 AND Seconds = 0 THEN

    LET Siren_Alarm = 0

    LOW RL1

ENDIF

IF New_Msg <= 6 THEN                    'This avoids an interrupt changing the key code elsewhere

    LET Key_code = New_Msg

    LET New_Msg = 11

ENDIF

SELECT Key_Code

    CASE 0                    '  "R"

        HIGH RL1

    CASE 1                    '  "G"

        LOW RL1

    CASE 4                     ' "Q"

        HIGH RL1

        LET Tune_indx = Gate_Open_Tune

        LET Seconds = Gate_Count

        LET Gate_Alarm = 1

    CASE 5                    ' "B"

        LOW RL1

        LET Tune_indx = Gate_Closed_Tune

    CASE 2                    ' "S"

        HIGH RL2

    CASE 3                    '  "U"

        LOW RL2

    CASE 6                    '  "h"

        HIGH RL1

        LET Seconds = Alarm_Count

        LET Tune_indx = Emergency_tune

        LET Siren_Alarm = 1

ENDSELECT

LET Key_Code = 11

GOTO main

Sub-programs

Make_Sound:

IF Tune_Indx > 0 AND Temperature > TooHot THEN

    LOW Aud_cont

    PAUSE 1000        'Time for the audio amp to stabilize

ENDIF

SETINT OFF        'Won't play all of the tune if interrupted

BRANCH Tune_Indx,(NoMore,Acquarious,Chime,Tone4,Siren,ShortTune,Tone2,Tone3,BaBa,Okay)

Tone3:

TUNE Aud_tune,6,($40,$40,$40,$40,$40,$40,$77,$77,$75,$77,$72,$72,$74,$70,$70,$40,$40,$40,$40,$40,$40,$77,$77,$75,$77,$72,$72,$74,$70,$70)

GOTO NoMore

Chime:

TUNE Aud_tune,2,($04,$07,$05,$AB) 

GOTO NoMore

ShortTune:

TUNE Aud_tune,5,($40,$05,$49,$05,$40,$2BC7,$45,$04,$47,$04,$64,$29,$C5,$40,$05,$49,$05,$40,$2B,$C7,$45,$04) 

GOTO NoMore

Tone2:

TUNE Aud_tune,5,($40,$02,$44,$85,$40,$42,$44,$45,$AC,$42,$44,$46,$47,$AC,2,$44,$46,$47,$2C,$42,$44,$46,$47,$2C,$00,$44,$85) 

GOTO NoMore

Acquarious:

TUNE Aud_tune,2,($34,$36,$B7,$39,$37,$76,$34,$32,$B4,$32,$74,$36,$F6,$34,$74,$32,$72,$B4)'Aquarious

GOTO NoMore

Tone4:

TUNE Aud_tune,8,($30,$75,$75,$35,$77,$79,$79,$39,$79,$77,$79,$3A,$34,$37,$35,$7C,$40,$40,$79,$02,$40,$40,$7A,$3A,$7A,$7A,$77,$00,$7A,$7A,$79,$39,$7C,$70,$75,$75,$35,$77,$79,$79,$39,$79,$77,$79,$3A,$34,$37,$35,$3C) 'boom

GOTO NoMore

BaBa:

TUNE Aud_tune,2,($30,$30,$37,$37,$79,$7B,$40,$79,$37,$3C,$35,$35,$34,$34,$32,$32,$30) 'Ba Ba

GOTO NoMore

Siren:

SOUND Aud_Tune,(86,300,95,300,86,300,95,300,86,300,95,300,86,300,95,300,86,300,95,300,86,300,95,300,86,300,95,300) 

GOTO NoMore

Okay:

SOUND Aud_Tune,(25,30)

NoMore:

HIGH Aud_Cont

LET Tune_Indx = 0

SETINT %00000001,%00000001    'Interrupt when Rcvr_Int high

RETURN



Interrupt:                'Interrupt response time = 4 ms

LET Rcvr_Val = "z"

LET temp = 0        

DO WHILE PinC.0 = 1

     INC temp        

    PAUSE 1

LOOP                'There is a 10 ms gap before the code to allow the serin to execute

IF temp < 8 OR temp > 21 THEN End_Interrupt

SERIN [40,End_Interrupt],Rcvr_In,N2400_8,("14L1776"),Rcvr_Val,comma,lenCode

IF Rcvr_Val = "z" THEN End_Interrupt

IF Comma = 13 AND LenCode = 10 THEN

    LOOKDOWN Rcvr_Val,("R","G","S","U","Q","B","h"),New_Msg        'Ignore unwanted codes

    LET LED_Strobe = 1

ENDIF

PAUSE 5

End_Interrupt:

SETINT %00000001,%00000001    'Interrupt when Rcvr_Int high

RETURN