Code change

Tony P

Member
I made a vivarium controller some months ago which is working as planned.

It controls the light on/off at set times and controls the heat lamp, increasing the temperature in the morning, holding a set temperature all day and decreasing at night.


With trial and error I found that checking the temperature every three minutes allowed the heat lamp enough time to heat up/cool down to a point where it needed to be either on or off. Anything less and the relays would sometimes flash on and off if the temperature reading was hovering around the trigger point.

The code below is the section that checks the time every three minutes. It works fine but I would like to know if and how it could be shortened?

Code:
	IF MINS=1  AND SECONDS <2 THEN GOTO TEMPTEST   
	IF MINS=4  AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=7  AND SECONDS <2 THEN GOTO TEMPTEST   
	IF MINS=16 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=19 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=22 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=25 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=34 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=37 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=40 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=49 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=52 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=55 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=64 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=67 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=70 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=73 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=82 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=85 AND SECONDS <2 THEN GOTO TEMPTEST    
	IF MINS=88 AND SECONDS <2 THEN GOTO TEMPTEST
 

AllyCat

Senior Member
Hi Tony,

Presumably your MINS is counting in Binary Coded Decimal.

The first line of the following code converts to "real" (decimal) minutes, the second line calculates the remainder when dividing by 3 and the final line is your 3-minute test:

Code:
MINSBYTE = MINS / 16 * 250 + MINS       ; Sutract 6 for each 10 minutes (i.e. $10 = 16  --> 10)
MINSBYTE = MINSBYTE // 3
IF MINSBYTE =1 AND SECONDS <2 THEN GOTO TEMPTEST
But with an M2 one can simply use the "time" variable, which increments automatically once each second:

Code:
DO
	IF time > 179 THEN
		time = 0
		GOTO TEMPTEST
	ENDIF
LOOP
Cheers, Alan.
 

Tony P

Member
But with an M2 one can simply use the "time" variable, which increments automatically once each second:

.
Thank you AllyCat. That is exactly what I needed.

I didn't know about the "time" variable. Not too clear in the manuals :)
 
Top