-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow.lua
39 lines (34 loc) · 943 Bytes
/
flow.lua
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
pin = 1
count = 0
volume_in_ml = 0
on_volume_callback = nil
red_pin = 2
green_pin = 3
gpio.mode(pin, gpio.INT, gpio.PULLUP)
function on_volume(volume, callback)
print("start pouring....")
count = 0
volume_in_ml = tonumber(volume)
on_volume_callback = callback
volume_in_ticks = (volume * 440) / 1000
gpio.trig(pin, 'down', onChange)
gpio.write(red_pin, gpio.LOW)
gpio.write(green_pin, gpio.HIGH)
end
function onChange ()
count = count + 1
print('flow' .. count)
print('starting to pour: ' .. count)
if (count > volume_in_ticks) then
print('stop at ' .. count)
gpio.write(red_pin, gpio.HIGH)
gpio.write(green_pin, gpio.LOW)
on_volume_callback()
print('finished pouring: ' .. count)
gpio.trig(pin, 'none')
end
end
gpio.mode(red_pin, gpio.OUTPUT)
gpio.write(red_pin, gpio.HIGH)
gpio.mode(green_pin, gpio.OUTPUT)
gpio.write(green_pin, gpio.LOW)