Exiting a Pause?

Dermotx

Member
Hi. I would appreciate a little help with some code please.
I am using an 08M2 to control a water heater. I turn on the water heater by making pinC.4 high by simply momentarily pushing a push to make switch which is connected to 5V and pinC.4. This then makes pinC.0 go high which operates a relay to turn on the heater and it stays on for the time specified in the code below (60 x 60 seconds to give one hour).
The code below accomplishes this.

My problem is that I want to be able to turn off the heater by again momentarily pressing the switch to make pinC.4 high, if I change my mind about heating the water, during the period when the loop is performing.
I basically need to be able to exit the loop and get out of the PAUSE, which lasts for 60 seconds, and have the pinC.0 go low until the next high pinC.4. Once the 60 second pause starts I can’t get out of it.
I have looked at the EXIT command and INTERRUPT command but can’t get it to do what I want. I have searched the forum but can't get exactly what I need. Maybe it's not possible to get out of a pause once it's started!

The other solution I suppose is to reset by turning off the power supply to the 08M2 for a few moments,letting the capacitor discharge, but this entails using another switch.
The other option is to shorten the pause to say 6 seconds, interrupt the loop and increase the number of loops. If I then held the switch for more than 6 seconds I'd be finished the pause and could exit the loop. If possible I'd like to avoid this.
Thanks if you can help.

Regards
Dermot

'08M2

Start:

If pinC.4=1 then high C.0
endif
if pinC.4=0 then State1
for b1=1 to 60
pause 60000
next b1
low C.0
goto start

State1:

low C.0
goto Start
 
Last edited:

hippy

Technical Support
Staff member
Change your 60 x 60000ms FOR-NEXT loop to 36000 x 100ms, and move the button check inside the loop. Note you need to use a word variable to hold the loop value ...

Code:
PauseForAnHour:
  For w0 = 1 To 36000
    If pinC.4 = 0 Then Done
    Pause 100
  Next
Done:
You will probably have to reduce the 36000 value slightly if you want an accurate 1 hour delay.

Edit: That will give an hour delay whilst the button is held, immediately exit when released. It needs a bit more tweaking to handle when the button has been released then pressed again ...

Code:
Start:
  Low C.0
  Do : Loop Until pinC.4 = 0
  Do : Loop Until pinC.4 = 1
  High C.1

  w0 = 36000

ButtonStillHeld:
  Do
    If pinC.4 = 0 Then ButtonReleased
    Pause 100
    w0 = w0 - 1
  Loop Until w0 = 0
  Goto Start

ButtonReleased:
  Do
    If pinC.4 = 1 Then Start
    Pause 100
    w0 = w0 - 1
  Loop Until w0 = 0
  Goto Start
 
Last edited:

Hemi345

Senior Member
Your code is relying on you to hold the button long enough to get past the check for pinC.4=0. Probably fine since the PICAXE executes the instructions pretty fast, but I wouldn't rely on that. You could use an interrupt.

Code:
#picaxe 08m2
symbol onCounter = w0
symbol heaterStatus = b10
setint %00010000, %00010000	'arm interrupt on pin C.4

Start:
	if HeaterStatus = 1 then
		high C.0	'turn on heater
		for onCounter=1 to 6000
			pause 600
			if heaterStatus = 0 then exit
		next onCounter	
	endif
	low C.0		'turn off heater
	goto start

interrupt:
	if pinC.4 = 1 then interrupt 'debounce switch
	if heaterStatus = 1 then 
		heaterStatus = 0		'heater off
	else
		heaterStatus = 1		'heater on
	endif
	setint %00010000, %00010000 'rearm interrupt
	return
 

PieM

Senior Member
Its better to use the timer.

Code:
#picaxe 08M2

start:
do loop while pinc.4 = 0
do loop while pinC.4 = 1
	time=0
do while time <3600 
if pinC.4 = 1 then exit
	high c.0
loop

low C.0
do loop while pinC.4 = 1
goto start
or:

Code:
#picaxe 08M2

start:
do loop while pinc.4 = 0
do loop while pinC.4 = 1
	time=0
	high c.0
do while time <3600 and pinC.4 = 0
loop

low C.0
do loop while pinC.4 = 1
goto start
 
Last edited:

Hemi345

Senior Member
PieM, that is a really elegant solution. It took me a while to get my head around it, but simulating it in PE shows how simple and compact it is. Good job.

Just playing around with checking the state of the output pin and PieM's suggestion of using 'Time', I also came up with this interrupt-based example:

Code:
#picaxe 08m2
setint %00010000, %00010000	'arm interrupt on pin C.4

Start:
	if OutpinC.0 = 1 then	'heater is ON, start checking time
		time = 0			'reset time
		do while time < 3600
			if OutpinC.0 = 0 then exit 'heater is OFF, exit
		loop	
		low C.0				'time is up, turn heater OFF
	endif
	goto start

interrupt:
	if pinC.4 = 1 then interrupt 'debounce switch
	if OutpinC.0 = 0 then 		'check the state of pin C.0, if currently low...
		high C.0		'turn heater ON
	else				'if currently high...
		low C.0			'turn heater OFF
	endif
	setint %00010000, %00010000 'rearm interrupt
	return
 

fernando_g

Senior Member
There is a portion of the code that I don't understand:

time=0
high c.0
do while time <3600 and pinC.4 = 0
loop

I looked up at manual 2, and there is no "time" instruction per se. And it does not appear to be a symbol, either.
Am I missing something?
 
Top