14M2 locks up when set point exceeded

npomeroy

Member
This is a simple proportional controller that works well switch a 2A 12V dc load via a FET. All seems well if I just let it run from cold. But if I add extra warmth to the sensor so it exceeds the 150 set point it switches off (as it is supposed to) but does not switch on again when it cools. The debug information also stops. The controller still fails to re-start even when not connected to the USB cable.

Code:
symbol setpoint = b0
symbol sensor = C.4		;thermistor
symbol temp = b1			;8 bit analoge of temperature
symbol longloop = b3
symbol loopperiod = b9
symbol recent = b4		;temperature stored recently
symbol earlier = b11		;not used
symbol lowdiff = b2		;units temp is below setpoint
symbol gain = b7			;additition added to duty period over time
symbol shortloop = b5		;loop that creates duty cycle
symbol heat = B.1			; FET turns on load
symbol duty = b8			; duty period withing cycle

Main:
	Let setpoint = 150
	let loopperiod = 50
	readadc sensor, temp
	let gain = 0
	let recent = temp

testemp:
		if temp > setpoint then		 	 
		 	goto oneoff				;if too hot switch off
			endif
	if temp <= recent then				; if cooled since last store increase gain
		let gain = gain +1
			endif
	if gain = 0 then					; if gain already zero avoid subtracting from it
		goto skip
			endif
	if temp = setpoint then				;if reached setpoint then reduce gain
		let gain = gain - 1
			endif	
skip:	
	for longloop = 1 to loopperiod		;approx 50 second loop stores current temp at end
	readadc sensor, temp					
debug
		if temp > setpoint then		 	 
		 	goto oneoff				;if too hot switch off
			endif
	
	let lowdiff = setpoint - temp			;find differential				
	 						
	let duty = lowdiff * 3 + gain			;duty period proportional to differential plus gain
	
	
	for shortloop = 1 to 30				;approx 1-second loop for duty cycle
	
		if shortloop > duty then gosub oneoffa	;if iterations exceed duty switch heat off
	
		if shortloop <= duty then gosub oneon	;if iterations <= duty switch heat on
						
	debug
		pause 10
	
	next shortloop
			if longloop = loopperiod then	; at last iteration oflong loop store current temperature
				let recent = temp
						endif
	next longloop
		goto testemp				;go to start and measure temperature	
	

	
oneoff:
	low heat
	wait 2
	goto testemp

oneoffa:
	low heat

		return

oneon:
	high heat
		return
=====================
please excuse the lack of formatting
Regards
Nelson
 
Last edited by a moderator:

Technical

Technical Support
Staff member
When you goto oneoff: then goto back to testtemp: where do you actually read the temperature again from the sensor to change variable 'temp'?
 

npomeroy

Member
When you goto oneoff: then goto back to testtemp: where do you actually read the temperature again from the sensor to change variable 'temp'?
Good point
It's just before "debug" but should be first line after "testemp"
I have changed it to see if makes a difference - Do you think it might have been stuck in a loop?
Nelson

PS thanks for formatting it. I'll learn how to do that but was in a hurry.
 
Top