Freezer alarm

davidwf

Senior Member
My 95 year old father keeps leaving his freezer door open so I thought it would make an ideal and relatively simple PIC project.....

I have got so far and it all works on the sim but I have one issue I am struggling with......under hi-temp alarm condition and IF the door is also open, the LED goes bonkers as it is being flashed by both the alarm and also the door open routines..... I can see why but not how to workaround it

How can I "bypass" the door open bit under hi-temp alarm conditions ?... please keep it simple - you can probably tell by my code I am no expert !

Thanks

#picaxe 08m2 ' Freezer alarm monitors door open via magnetic reed switch and temperature using DS18B20

SYMBOL LED = 0 'PIC pin 7 / output 0 to LED's
SYMBOL temp = 4 'PIC pin 3 / input 4 from DSB1820 temp sensor
SYMBOL piezo = 2 'PIC pin 5 / output 2 to piezo sounder
SYMBOL setpoint = 140 '140 = -12 degC set point : note that for a minus values actual value is 128+12 (so minus 12 = actual value of 140)
SYMBOL temperature = b1 'store DS18B20 current temp reading in variable b1

LOW piezo 'piezo off : starting point = all off
LOW LED 'LED off : starting point = all off

' NB pin 0 refers to output 0 i.e. C.0 / PIC pin 7 (LED's)- ALWAYS flashing if door open
' pin 1 refers to input 1 i.e. C.1 / PIC pin 6 (door open = 1, close = 0)
' pin 2 refers to output 2 i.e. C.2 / PIC pin 5 (Piezo)
' pin 3 refers to input 3 i.e. C.3 / PIC pin 4 (rec attn normally 1, operated = momentary 0)
' pin 4 refers to input 4 i.e. C.4 / PIC pin 3 (temp sensor)
' pin 5 refers to C.5 / PIC pin 2 (programming input)

main:

gosub CheckTemp

gosub CheckDoor

goto main

end


CheckTemp:

readtemp 4, b1 ' read temp from DSB1820 PIC pin 3 / input C.4 into variable b1

if b1=> setpoint then gosub SoundPiezo ' temp too high
if pin3 = 0 then goto delay10 ' if PIC pin 4 / C.3 = 0, rec. attn pressed go to 10 min delay
'sertxd (" ", #b1,cr,lf)

if b1=> setpoint then goto checktemp

return

CheckDoor:

if pin1 = 1 then gosub FlashLED ' PIC pin 6 / input C.1 door open switch (hi = door open)
return

delay10: ' 600 sec / 10 min delay

low piezo ' silence piezo
for w4 = 1 to 5 ' test = 5 (3 sec), actual value will be 1000 (10 mins) ' 1000 steps @ 600msec increases response time if temp drops below setpoint
if pin1 = 1 then ' PIC pin 6 / input C.1 door open switch (hi = door open)
gosub FlashLED

endif

if b1< setpoint then goto main ' temp has reset
pause 600 ' 1000 steps @ 600msec
next w4

return

FlashLED:

high LED ' flash LED all the time door is open and temp is below setpoint
pause 500
low LED

return

SoundPiezo

high piezo ' PIC pin 5 / output C.2
high LED ' PIC pin 7 / output C.0
pause 500
low piezo
low LED

return

end
 

Attachments

hippy

Ex-Staff (retired)
if pin3 = 0 then goto delay10

That is going to potentially cause some problems. You are jumping into code which is a subroutine with a RETURN at the end of it which will lead to a stack underflow. You can observe that by simulating the code.

Once you get a stack underflow program execution becomes unpredictable on a real chip.
 

davidwf

Senior Member
hmmmm....I have had various stages of stack overflow and underflow !

The input to pin3 is in fact a non locking pushbutton switch which takes the input to 0V that will only be momentarily operated providing a "receive attention" / acknowledge function to stop the audible alarm
and start a 10 minute delay

I will upload a cct diagram when I get home .....
 

BESQUEUT

Senior Member
Please, use
Code:
...[/ CODE] tags when you publish some code.

[U]What to do :[/U]
1) When door is open and temp is below setpoint : flash LED all the time
2) When door is open and temp is above setpoint : flash LED all the time and sound piezo 10 mn (except if pin3 is pressed)
3) When door is closed and temp is above setpoint : [COLOR="#FF0000"]dont know ?[/COLOR]
4) When door is closed and temp is below setpoint : stop flash and piezo

1) and 2) ==> When door is open flash LED all the time, no matter what is temp.

Is all that OK ?
 
Last edited:

BESQUEUT

Senior Member
Collaborative multitasking proposal

... please keep it simple
Complex Post Title, but simple code :
Code:
[color=Navy]#picaxe [/color][color=Black]08m2                  [/color][color=Green]' Freezer alarm monitors door open via magnetic reed switch and temperature using DS18B20 
                                                            [/color]
[color=Blue]SYMBOL [/color][color=Black]LED   [/color][color=DarkCyan]= [/color][color=Blue]C.0
SYMBOL [/color][color=Black]Door  [/color][color=DarkCyan]= [/color][color=Purple]pinC.1 [/color]
[color=Blue]SYMBOL [/color][color=Black]Piezo [/color][color=DarkCyan]= [/color][color=Blue]C.2
SYMBOL [/color][color=Black]Attn  [/color][color=DarkCyan]= [/color][color=Purple]pinC.3[/color]
[color=Blue]SYMBOL [/color][color=Black]Temp  [/color][color=DarkCyan]= [/color][color=Purple]pinC.4[/color]

[color=Blue]SYMBOL [/color][color=Black]Under[/color][color=DarkCyan]=[/color][color=Navy]0[/color]
[color=Blue]SYMBOL [/color][color=Black]Over[/color][color=DarkCyan]=[/color][color=Navy]1[/color]
[color=Blue]SYMBOL [/color][color=Black]Closed[/color][color=DarkCyan]=[/color][color=Navy]0[/color]
[color=Blue]SYMBOL [/color][color=Black]Opened[/color][color=DarkCyan]=[/color][color=Navy]1[/color]

[color=Blue]SYMBOL [/color][color=Black]TempPrevious[/color][color=DarkCyan]=[/color][color=Purple]bit0[/color]
[color=Blue]SYMBOL [/color][color=Black]StopPiezo[/color][color=DarkCyan]=[/color][color=Purple]w1[/color]


[color=Blue]do
      if [/color][color=Black]Door [/color][color=DarkCyan]= [/color][color=Black]Opened [/color][color=Blue]then
            high [/color][color=Black]LED
      [/color][color=Blue]else
            low [/color][color=Black]LED
      [/color][color=Blue]endif
      
      if [/color][color=Black]Temp[/color][color=DarkCyan]<>[/color][color=Black]TempPrevious [/color][color=Blue]then
            [/color][color=Black]TempPrevious[/color][color=DarkCyan]=[/color][color=Black]Temp
            [/color][color=Blue]if [/color][color=Black]Temp [/color][color=DarkCyan]= [/color][color=Black]under [/color][color=Blue]then
                  low [/color][color=Black]Piezo
            [/color][color=Blue]else 
                  high [/color][color=Black]Piezo
                  StopPiezo[/color][color=DarkCyan]=[/color][color=Purple]Time[/color][color=DarkCyan]+[/color][color=Navy]3 [/color][color=Green]' For debug only, to be adjusted
            [/color][color=Blue]endif
      endif

      if [/color][color=Purple]Time[/color][color=DarkCyan]=[/color][color=Black]StopPiezo [/color][color=DarkCyan]or [/color][color=Purple]PinC.3[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=Blue]then
            low [/color][color=Black]Piezo
      [/color][color=Blue]endif

      sertxd ([/color][color=Black]#[/color][color=Purple]time[/color][color=Black],[/color][color=Red]" "[/color][color=Blue])      [/color][color=Green]' for simulation only[/color]
[color=Blue]loop[/color]
NB : No GOTO command, nor PAUSE command...
 
Last edited:

davidwf

Senior Member
Please, use
Code:
...[/ CODE] tags when you publish some code.

[U]What to do :[/U]
1) When door is open and temp is below setpoint : flash LED all the time
2) When door is open and temp is above setpoint : flash LED all the time and sound piezo 10 mn (except if pin3 is pressed)
3) When door is closed and temp is above setpoint : [COLOR="#FF0000"]dont know ?[/COLOR]
4) When door is closed and temp is below setpoint : stop flash and piezo

1) and 2) ==> When door is open flash LED all the time, no matter what is temp.

Is all that OK ?[/QUOTE]

Thanks BESQUEUT....almost.......   

2) flash LED and sound piezo .... continue flashing LED but stop piezo for 10 minutes if pin3 momentary taken to 0V
3) sound piezo and flash LED
 

davidwf

Senior Member
How do you change a variable with an IF command&#8230;...say for example I want to set the variable "ledon" to value =1 if pin1 =1....

CheckDoor:

if pin1 = 1 then ledon=1 ' PIC pin 6 / input C.1 door open switch (hi = door open)
return

doesn&#8217;t work &#61516;
 

AllyCat

Senior Member
Hi,

That structure is an (implied) GOTO (so the =1 is a syntax error).

Code:
if pin1 = 1 then 
    ledon=1                  ' PIC [B]LEG[/B] 6 / input C.1 door open switch (hi = door open)
endif
return
You probably need pulldown resistors on the two FET gates (but could avoid the two 100k pullups with a PULLUP command).

Cheers, Alan.
 

davidwf

Senior Member
I don't think the FET's need pull down as the o/p will either be hi or lo....I can sort the "mechanical" bits out later, the coding is my weakspot !

so how can I "load" a variable with the condition on the pin ?

ledon=pin1 seems to work .....
 

davidwf

Senior Member
Complex Post Title, but simple code :
Code:
[color=Navy]#picaxe [/color][color=Black]08m2                  [/color][color=Green]' Freezer alarm monitors door open via magnetic reed switch and temperature using DS18B20 
                                                            [/color]
[color=Blue]SYMBOL [/color][color=Black]LED   [/color][color=DarkCyan]= [/color][color=Blue]C.0
SYMBOL [/color][color=Black]Door  [/color][color=DarkCyan]= [/color][color=Purple]pinC.1 [/color]
[color=Blue]SYMBOL [/color][color=Black]Piezo [/color][color=DarkCyan]= [/color][color=Blue]C.2
SYMBOL [/color][color=Black]Attn  [/color][color=DarkCyan]= [/color][color=Purple]pinC.3[/color]
[color=Blue]SYMBOL [/color][color=Black]Temp  [/color][color=DarkCyan]= [/color][color=Purple]pinC.4[/color]

[color=Blue]SYMBOL [/color][color=Black]Under[/color][color=DarkCyan]=[/color][color=Navy]0[/color]
[color=Blue]SYMBOL [/color][color=Black]Over[/color][color=DarkCyan]=[/color][color=Navy]1[/color]
[color=Blue]SYMBOL [/color][color=Black]Closed[/color][color=DarkCyan]=[/color][color=Navy]0[/color]
[color=Blue]SYMBOL [/color][color=Black]Opened[/color][color=DarkCyan]=[/color][color=Navy]1[/color]

[color=Blue]SYMBOL [/color][color=Black]TempPrevious[/color][color=DarkCyan]=[/color][color=Purple]bit0[/color]
[color=Blue]SYMBOL [/color][color=Black]StopPiezo[/color][color=DarkCyan]=[/color][color=Purple]w1[/color]


[color=Blue]do
      if [/color][color=Black]Door [/color][color=DarkCyan]= [/color][color=Black]Opened [/color][color=Blue]then
            high [/color][color=Black]LED
      [/color][color=Blue]else
            low [/color][color=Black]LED
      [/color][color=Blue]endif
      
      if [/color][color=Black]Temp[/color][color=DarkCyan]<>[/color][color=Black]TempPrevious [/color][color=Blue]then
            [/color][color=Black]TempPrevious[/color][color=DarkCyan]=[/color][color=Black]Temp
            [/color][color=Blue]if [/color][color=Black]Temp [/color][color=DarkCyan]= [/color][color=Black]under [/color][color=Blue]then
                  low [/color][color=Black]Piezo
            [/color][color=Blue]else 
                  high [/color][color=Black]Piezo
                  StopPiezo[/color][color=DarkCyan]=[/color][color=Purple]Time[/color][color=DarkCyan]+[/color][color=Navy]3 [/color][color=Green]' For debug only, to be adjusted
            [/color][color=Blue]endif
      endif

      if [/color][color=Purple]Time[/color][color=DarkCyan]=[/color][color=Black]StopPiezo [/color][color=DarkCyan]or [/color][color=Purple]PinC.3[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=Blue]then
            low [/color][color=Black]Piezo
      [/color][color=Blue]endif

      sertxd ([/color][color=Black]#[/color][color=Purple]time[/color][color=Black],[/color][color=Red]" "[/color][color=Blue])      [/color][color=Green]' for simulation only[/color]
[color=Blue]loop[/color]
NB : No GOTO command, nor PAUSE command...



This seems to work ...

Code:
#picaxe 08m2                  ' Freezer alarm monitors door open via magnetic reed switch and temperature using DS18B20 
                                                            
 SYMBOL LED = 0               'PIC pin 7 / output 0 to LED's
SYMBOL temp = 4              'PIC pin 3 / input 4 from DSB1820 temp sensor      
SYMBOL piezo = 2             'PIC pin 5 / output 2 to piezo sounder
SYMBOL setpoint = 140        '140 = -12 degC set point : note that for a minus values actual value is 128+12 (so minus 12 = actual value of 140)
SYMBOL ledon = b2
SYMBOL temperature = b1      'store DS18B20 current temp reading in variable b1

 LOW piezo                    'piezo off  : starting point = all off
LOW LED                      'LED off    : starting point = all off
LEDon=0                      'reset value

 ' NB pin 0 refers to output 0 i.e. C.0 / PIC pin 7 (LED's)- ALWAYS flashing if door open
'    pin 1 refers to  input 1 i.e. C.1 / PIC pin 6 (door open = 1, close = 0)
'    pin 2 refers to output 2 i.e. C.2 / PIC pin 5 (Piezo)
'    pin 3 refers to  input 3 i.e. C.3 / PIC pin 4 (rec attn normally 1, operated = momentary 0)
'    pin 4 refers to  input 4 i.e. C.4 / PIC pin 3 (temp sensor)
'    pin 5 refers to               C.5 / PIC pin 2 (programming input)


main:

gosub CheckTemp
gosub CheckDoor
gosub FlashLED 
goto main

end

CheckTemp:

readtemp 4, temperature                               ' read temp from DSB1820 PIC pin 3 / input C.4 into variable b1
if temperature=> setpoint then gosub SoundPiezo       ' temp too high

if pin3 = 0 then goto delay10             ' if PIC pin 4 / C.3 = 0, rec. attn pressed go to 10 min delay
      
'sertxd (" ", #b1,cr,lf) 

return

CheckDoor:

LEDon=pin1 'read value of PIC pin 6 / input C.1 door open switch (hi = door open) into variable "LEDon"

return

delay10:                                        ' 600 sec / 10 min delay

low piezo                                       ' silence piezo

      for w4 = 1 to 5         ' test = 5 (3 sec), actual will be 1000 (10 mins)   ' 1000 steps @ 600msec increases response time if temp drops below setpoint

LEDon=pin1                                'read value of PIC pin 6 / input C.1 door open switch (hi = door open) into variable "LEDon"
gosub FlashLED
      
      if temperature< setpoint then goto main   ' temp has reset
      pause 600                                 ' 1000 steps @ 600msec 
      next w4
return      

FlashLED:
If LEDon=0 then goto NoFlash                    ' if PIC pin 6 / input C.1 door open switch = lo then door is closed, stop flashing LED
      high LED                                  ' flash LED all the time door is open and temp is below setpoint
      pause 500
      low LED
      pause 500
return

SoundPiezo:

      high piezo                                ' PIC pin 5 / output C.2
      high LED                                  ' PIC pin 7 / output C.0
      pause 500
      low piezo
      low LED
      


return

NoFlash:      
return


end
 

BESQUEUT

Senior Member
My #6 code was only simulated ; did you have a try ?
This seems to work ...
Seems is the good word...
if temperature< setpoint then goto main
NEVER GOTO outside a sub program. Always use the RETURN command.
==> you will encounter Stack overflow...

Code:
CheckDoor:
     LEDon=pin1 
	
delay10:                                        ' 600 sec / 10 min delay
      low piezo

      for w4 = 1 to 5         ' test = 5 (3 sec), actual will be 1000 (10 mins)  
		LEDon=pin1
		gosub FlashLED
      	        if temperature< setpoint then goto main   ' temp has reset
                pause 600 
      next w4
return
During ten minutes :
- temperature is not checked,
- pin3 is not checked

2) flash LED and sound piezo .... continue flashing LED but stop piezo for 10 minutes if pin3 momentary taken to 0V
3) sound piezo and flash LED
So :
1) When door is open flash LED all the time, no matter what is temp.
2) When temp is above setpoint :
2a) flash LED (even if door is closed)
2b) sound piezo 10 mn
2c) sound piezo 10 mn every X mn or stop piezo till new alarm ? (IE till Temp<Setpoint)
3) if pin3 is low :
3a) stop alarm immediately for 10 mn, but keep flashing LED
3b) sound piezo 10 mn every X mn or stop piezo till new alarm ? (IE till Temp<Setpoint)




Three proposals to make this working :
A) Linear no multitasking : you have to write an event loop for each possible state.

B) Collaborative multitasking : One event loop, but you have to use Time in place off pauses...

C) Pseudo-Multitasking using Picaxe startx: labels and SUSPEND/RESUME/RESTART command

For each case, please forgot the GOTO command...

Proposal for C) NOT TESTED
Code:
#picaxe 08m2
#simtask 0'all
                                                            
SYMBOL LED = C.0     
SYMBOL Piezo = C.2   
SYMBOL Setpoint = 140        '140 = -12 degC set point : note that for a minus values actual value is 128+12 
SYMBOL Temperature = b1

SYMBOL Door  = pinC.1 
SYMBOL DS18B20= 4

SYMBOL Closed=0
SYMBOL Opened=1

SYMBOL LEDstate=b2
SYMBOL PiezoState=b3

SYMBOL NoAlarm=0
SYMBOL AlarmOn=1
SYMBOL ShutOff=2

start0:
suspend 1	' Flash LED
suspend 2	' Sound Piezo
LEDstate=NoAlarm
PiezoState=NoAlarm
LOW Piezo 
LOW LED 


do		' EVENT LOOP
	readtemp DS18B20, Temperature 
		
	if Door=Opened or Temperature=> Setpoint then
		gosub StartLED
	else
		gosub StopLED
	endif

	if Temperature=> setpoint then
		gosub StartPiezo
	else
		gosub StopPiezo
	endif

	if pin3 = 0 then
		gosub ShutOffPiezo
	endif
loop
end

StartLED:
	if LEDstate=NoAlarm then
		LEDstate=AlarmOn
		restart 1
	endif
return

StopLED:
	if LEDstate=AlarmOn then
		LEDstate=NoAlarm
		suspend 1
		low LED
	endif
return



start1:
High LED
do
	pause 5 '500
	toggle LED
loop


start2:
High Piezo
do
	pause 6'60000
	toggle piezo
loop



StartPiezo:
	if PiezoState=NoAlarm then
		PiezoState=AlarmOn
		restart 2
	endif
return

ShutOffPiezo:
	if PiezoState=AlarmOn then
		PiezoState=ShutOff
		suspend 2
		low Piezo
	endif
return

StopPiezo:
	if PiezoState<>NoAlarm then
		PiezoState=NoAlarm
		suspend 2
		low Piezo
	endif
return
 
Last edited:
Top