Where has the time gone indeed?
I built a temperature controlled fan for the A/V cabinet - using an 08M - in September 2006. Other than replacing the fan multiple times, it's still running just fine (I have a new-in-the-box fan waiting for the next failure ;-). When the temperature at the hottest point in the cabinet rises to 86F/30C, the fan turns on and runs until the temperature drops to 82.4F/28C. The LED in the translucent box (small plastic container that some hardware came in) is on while the DS18B20 is being read and then flashes twice if the temperature is above the lower limit (could be rising or falling).
The PICAXE chips are reliable little slaves if you stay within their limits - the fan is controlled by a 2N4401 transistor that's driven by a pin on the PICAXE through a 4K7 resistor.
My code was adapted from an example published by Peter H Anderson. It shows how to avoid working with negative numbers when measuring temperature with a DS18B20.
Code:
' AV-Fan.bas
' control A/V cabinet cooling fan based on temperature reported by DS18B20
'
' Adapted from DS18B20_08M_3.Bas - PICAXE-08M
'
' Illustrates an interface with the DS18B20 in monitoring temperature. An alarm
' is associated with the temperature.
'
' If the temperature measured on the DS18B20 associated with IN1 is abve HighTrip, a relay
' on Out4 (term 3) is operated. It is released when the temperature falls below LowTrip
'
' Note that HighTrip and LowTrip are specified as the temperature plus 60 degrees C to avoid
' working with negative numbers.
'
' Note that a 4.7K pullup to +5 VDC is required on the DQ lead. +5 VDC is required on the
' V+ terminal of the DS18B20.
'
' 08M DS18B20
'
' IO1 (term 7) ------------------ DQ (term 2)
'
' 2N4401 ---(FAN)-- +
' |/
' OUT4 (term 3) ------- 4K7 ----->|\
' -- GRD
'
' Uses 68 of 2048 bytes (08M2) - or 65 of 256 bytes.(08M)
'
' original DS18B20 code copyright, Peter H Anderson, Baltimore, MD, Sept, '04
' AV-Fan changes copyright John E Carter 24 Sept 2006
symbol counter = b1
Symbol TempC_100 = W3
Symbol DevNum = W6
Symbol Temp_8 = B9
'need 100 as HighTrip and 98 as LowTrip for Traces 3210A solar controller external fan
Symbol HighTrip = 98 '(100.4F) ' 38 + 60 - fan on when temp hits 100F - adjust as necessary
Symbol LowTrip = 96 '(96.8F) ' 36 + 60 - fan off when 2 deg cooler
low 0 'set unused pin
high 4 'operation verification on power up (fan on)
high 2 '(status LED on)
pause 600
low 2
Low 4 ' turn off alarm
Top:
high 2 'indicate a read is in progress (power/activity light)
ReadTemp 1, Temp_8 ' read the high 8 bits - approx temp in deg C
low 2
' respond to the temperature
Temp_8 = Temp_8 + 60 ' to avoid negative numbers
If Temp_8 >= HighTrip Then OperateAlarm
if Temp_8 < LowTrip then continue
'indicate if above LowTrip but below HighTrip
for counter = 1 to 2
pause 200
high 2
pause 200
low 2
next counter
continue:
If Temp_8 < LowTrip Then ReleaseAlarm
' else
Goto SequenceDone
OperateAlarm:
High 4
Goto SequenceDone
ReleaseAlarm:
Low 4
Goto SequenceDone
SequenceDone:
Pause 1000 'was 20000
GoTo Top
If I were to rewrite it today, it would get more use of "symbol" so the "high 2" would be "high LED" and similar changes. After 17 years that's just window dressing because the code works reliably. I'm from a generation that learned to write code in languages where the only informative things in that code were the comments - if the original programmer even bothered to put in comments :-( I did enough fixing of other people's code to get in the habit of having enough notes in what I wrote that I would know what the code was supposed to be doing when/if I had to make changes.
How reliably? Using the calculator at
https://www.timeanddate.com/date/durationresult.html, as the day this was posted the PICAXE circuit has been running 17 years, 5 months, 21 days or 6383 days or 153,192 hours. Be nice if fans with an expected life (MTBF) of over 150,000 hours weren't priced so only the military can afford them :-(