我正在尝试编写一个程序,该程序在门打开(doorstatus = 0
)时读出5个微动开关。它还应该能够通过计算关闭框时设置的微动开关与打开时所设置的微动开关的数量来计算用户所占的位置(rfidTakenCheck
)。但是,当我部署程序时,我的CloudGate(计算机单元)卡住了,这使我认为正在发生无限循环。
我尝试换出另一个CloudGate,但这没有帮助,所以这不是硬件问题。我也尝试在while循环中添加一个break
命令,但这没有帮助。
函数checkMicroswitches()
从5个gpio引脚获取其信息。
if not msg then
-- called on startup
return
end
function checkMicroswitches() --checks microswitches + calculates numberFree & placesFree
if msg.topic == "Microswitch1" then
global.microswitch1 = msg.payload
end
if msg.topic == "Microswitch2" then
global.microswitch2 = msg.payload*10
end
if msg.topic == "Microswitch3" then
global.microswitch3 = msg.payload*100
end
if msg.topic == "Microswitch4" then
global.microswitch4 = msg.payload*1000
end
if msg.topic == "Microswitch5" then
global.microswitch5 = msg.payload*10000
end
global.placesFree = global.microswitch1 + global.microswitch2 + global.microswitch3 + global.microswitch4 + global.microswitch5
function sumdigits(str)
local total = 0
for digit in string.gmatch(str, "%d") do
total = total + digit
end
return total
end
global.numberFree = sumdigits(tostring(global.placesFree))
end -- ends function checkMicroswitches
-- initialising
checkMicroswitches()
msg.payload = {}
msg.payload = global.numberFree
msg.topic = "numberFree"
sendTo(1, msg)
msg.payload = {}
msg.payload = global.placesFree
msg.topic = "placesFree"
sendTo(2, msg)
global.numberFreeStart = 0
global.numberFreeEnd = 0
while (global.doorstatus == 1) do -- door closed
checkMicroswitches()
global.numberFreeStart = global.numberFree
global.rfidTakenCheck = 0
end
while (global.doorstatus == 0) do -- door open
global.numberFreeEnd = global.numberFree
global.rfidTakenCheck = global.numberFreeStart - global.numberFreeEnd
end
msg.payload = {}
msg.payload = global.rfidTakenCheck
msg.topic = "rfidTakenCheck"
sendTo(3, msg)
global.numberFreeEnd = 0
global.numberFreeStart = 0
global.rfidTakenCheck = 0
如果我删除了while循环,则numberFree
和placesFree
变量的确会被计数并发送,因此我认为这是引起问题的while循环。我希望他们通过比较微动开关的值来计算开门时用户所占的位置。