问题描述
全部!
我为我的狗制作了一个自动、自动填充的水碗,它与水位控制器完美配合,现在我想使用 ESP8266 添加一些监控功能。比如检测是否有水在流动,或者是否发生了溢出。
观察液位传感器和溢流传感器没问题,但我一直在检测水流。
我有一个霍尔效应流量传感器,当水流过它时会发送 1/0 脉冲流,我可以使用 GPIO 引脚上的中断很好地检测到它。我的问题是我无法可靠地检测脉冲何时停止。
到目前为止,我的解决方案是对脉冲进行计数并将值写入计数器,然后将测试计数器设置为等于它。我的想法是,只要水在流动,计数就会继续变化,一旦水停止,计数就会保持不变。这一切都是通过一个定时器发生的,该定时器在 GPIO 中断被触发时启动。
它主要按预期工作,但是当计时器触发时,它会检查两个计数器值,并且有一段时间它们是相同的,因此它表明流已经停止了一个周期,然后又重新开始流.这是我正在努力克服的行为。
天啊,我知道这很长,对不起!
我的代码如下:
flow_sense_pin = 1
flow_counter = 0
test_counter = 0
flow = false
flow_timer = tmr.create()
flow_timer:register(4000,tmr.ALARM_AUTO,function() test_flow() end)
gpio.mode(flow_sense_pin,gpio.INT)
function flow_pin_cb(level)
gpio.trig(flow_sense_pin,level == gpio.HIGH and "down" or "up")
flow_counter = flow_counter + 1
test_counter = flow_counter
if flow == true then else print("Flow Detected") end
flow = true
flow_timer:start()
end
function test_flow()
if test_counter == flow_counter then flow = false end
if flow == false then flow_timer:stop() print("Flow Stopped") end
end
gpio.trig(flow_sense_pin,"down",flow_pin_cb)
终端的输出是这样的:
我确定我忽略了一些明显的东西,但我已经坚持了几个小时,但我什么也没有得到。如果我所有的漫无边际都有意义,并且您有编码建议/解决方案,我很想听听。
谢谢!
解决方法
我仍然需要做一些脉宽测试,但效果很好,新代码更简单!谢谢!
新代码要简单得多:
flow_sense_pin = 1
timeout = 1000
flow_timer = tmr.create()
flowing = false
gpio.mode(flow_sense_pin,gpio.INT)
function flow_pin_cb(level)
gpio.trig(flow_sense_pin,level == gpio.HIGH and "down" or "up")
if flowing == false then print("Flow Detected") flowing = true end
flow_timer:alarm(timeout,tmr.ALARM_SINGLE,flow_stop)
end
function flow_stop()
print("Flow Stopped")
flowing = false
end
gpio.trig(flow_sense_pin,"down",flow_pin_cb)
,
我会采用这种更简单且可能更强大的方法:
-
像您一样创建
flow_timer
对象 -
在流量脉冲(
flow_pin_cb
)触发的函数中,调用flow_timer:alarm(timeout,flow_stop)
其中
timeout
是比连续流脉冲之间的最大时间稍长一点的时间,flow_stop
是一个函数,当流停止时调用该函数 -
使用一个标志,我们称之为
flowing
在
flow_pin_cb
中,如果flowing
为假,print("Flow Detected")
并设置flowing
为真在
flow_stop
、print("Flow Stopped")
中并设置flowing
false