-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLVD.bas
More file actions
298 lines (245 loc) · 5.2 KB
/
LVD.bas
File metadata and controls
298 lines (245 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#define V0
#define V1
; #define V2
; #define DEBUGGING
; uncomment the above to enable light serial output,
; and/or full debugging data
; constants and variable assignment
; ADC values at 10 bits appear to be roughly V * 48
; 12.4V = 595 -> 95
; 13.0V = 625 -> 125
; determined experimentally from bandit: 117 + 156
SYMBOL def_low_water = 100
SYMBOL def_high_water = 170
EEPROM 0, (def_low_water, def_high_water)
EEPROM 10, (0, 0) ; clear the location for new values
SYMBOL low_water = b12
SYMBOL high_water = b13
SYMBOL tmp_low= b14
SYMBOL tmp_high = b15
SYMBOL low_timer = 6 ; cycles
SYMBOL high_timer = 12 ; cycles
SYMBOL pause_interval = 250 ; ms
SYMBOL flash_slow = 3
SYMBOL flash_med = 2
SYMBOL flash_fast = 1
SYMBOL flash_rate = b4
SYMBOL Vin = b0
SYMBOL avg = b5
SYMBOL low_ctr = b2
SYMBOL high_ctr = b11
SYMBOL last_state = b3
SYMBOL button_var = b7
SYMBOL button_delay = b8
SYMBOL button_flashes = b16
SYMBOL button_flash_rate = b17
SYMBOL ADC = C.1
SYMBOL LED = C.2
SYMBOL PUSHBUTTON = C.3
SYMBOL RELAY = C.4
; task 0: the flasher
start0:
high LED
let w12 = flash_rate * 125
pause w12
low LED
if flash_rate = flash_slow then
sleep 1
else
pause w12
endif
goto start0
; task 1: the button interface
start1:
button_init:
pullup on
let button_var = 0
let button_delay = 0
#ifdef V2
sertxd("reset", cr, lf)
#endif
; resume the flasher & controller (if it was suspended)
restart 0
restart 3
; config state 0 -- waiting for input
cfg_0:
button PUSHBUTTON, 0, 255, 1, button_var, 1, cfg_start
pause 10
goto cfg_0
cfg_reset:
let button_flashes = 20
let button_flash_rate = 25
gosub flash
#ifdef V2
sertxd("reset requested", cr, lf)
#endif
write 10, def_low_water
write 11, def_high_water
pause 1000
goto button_init
cfg_start:
; stop the flasher
suspend 0
low LED
; stop the main control task & fire up accesories
suspend 3
high RELAY
let button_flashes = 1
let button_flash_rate = 50
gosub flash
pause 1000
gosub get_adc
let tmp_low= Vin
#ifdef V1
sertxd("low water: ", #tmp_low, cr, lf)
#endif
let button_flashes = 1
let button_flash_rate = 50
gosub flash
pause 1000
; check for reset condition ?
let button_var = 0
button PUSHBUTTON, 0, 100, 100, button_var, 1, cfg_reset
#ifdef V1
sertxd("wait for press...", cr, lf)
#endif
let button_flashes = 1
let button_flash_rate = 50
gosub flash
low RELAY
; wait up to 60s for cfg_high press
let button_delay = 0 ; actually a delay counter
let button_var = 0
cfg_low_loop:
button PUSHBUTTON, 0, 255, 1, button_var, 1, cfg_high
pause 300
; if it's been lil (~ 60s) while since the first press, bail out
inc button_delay
if button_delay > 200 then
goto button_init
endif
goto cfg_low_loop
cfg_high:
let button_flashes = 1
let button_flash_rate = 50
gosub flash
low RELAY
pause 1000
gosub get_adc
let tmp_high = Vin
#ifdef V1
sertxd("high water: ", #tmp_high, cr, lf)
#endif
if tmp_high > tmp_low then
let button_flashes = 3
let button_flash_rate = 50
gosub flash
write 10, tmp_low
write 11, tmp_high
let low_water = tmp_low
let high_water = tmp_high
#ifdef V1
sertxd("saving", cr, lf)
#endif
else
let button_flashes = 4
let button_flash_rate = 250
gosub flash
endif
pause 5000
goto button_init
goto start1
; task 3: the main loop
start3:
; initialization
gosub init
let low_ctr = 0
let high_ctr = 0
let flash_rate = flash_fast
; states: init, testing, high/on, middle (low < Vin < high)
; default state
init_state:
low RELAY
gosub get_adc
#ifdef V1
sertxd("Vin: ", #Vin, " avg: ", #avg, cr, lf)
#endif
#ifdef DEBUGGING
debug
#endif
; FALTHROUGH to testing_state
; Vin < high
testing_state:
gosub get_adc
if Vin >= high_water then
let flash_rate = flash_med
let low_ctr = 0
if high_ctr > high_timer then
goto high_state
endif
inc high_ctr
elseif Vin <= low_water then
let flash_rate = flash_fast
let high_ctr = 0
if low_ctr > low_timer then
low RELAY
else
inc low_ctr
endif
else
let flash_rate = flash_med
endif
pause pause_interval
goto testing_state
; Vin >= high
high_state:
high RELAY
let flash_rate = flash_slow
gosub get_adc
if Vin < high_water then
goto testing_state
endif
pause pause_interval
goto high_state
end
init:
read 10, low_water
read 11, high_water
if low_water = 0 or high_water = 0 then
read 0, low_water
read 1, high_water
else
#ifdef V0
sertxd("Read from flash: low =", #low_water, ", high=", #high_water, cr, lf)
#endif
endif
return
get_adc:
readadc10 ADC, w0
#ifdef V2
calibadc10 w11
sertxd("ADC10: ", #w0, ", 1.024V = ", #w11, cr, lf)
#endif
if w0 > 750 then
let Vin = 250
elseif w0 < 500 then
let Vin = 0
else
let Vin = w0 - 500
endif
#ifdef V1
sertxd("Vin: ", #Vin, cr, lf)
#endif
return
flash:
for w12 = 1 to button_flashes
high LED
pause button_flash_rate
pause button_flash_rate
low LED
if w12 < button_flashes then
pause button_flash_rate
pause button_flash_rate
endif
next w12
return