我正在尝试读取5个微动开关,并计算出多少个处于活动状态以及哪个处于活动状态。在这方面,我成功了。但是,我还想知道一扇门被打开时被带走了多少(doorstatus == 0
表示它已打开)。除了使用while doorstatus == 1 do
之外,还有其他方法可以检测门状态的变化吗?例如:一个if then循环,它检测到门的状态从0变为1
我现在的代码(不起作用):
if global.doorstatus == 1 then -- door closed
checkMicroswitches() -- checks the microswitches
calculateFree() -- calculates the amount and place of free microswitches
global.numberFreeStart = global.numberFree
global.rfidTakenCheck = 0
end
msg.payload = global.numberFreeStart
msg.topic = "numberFreeStart"
sendTo(4, msg)
while (global.doorstatus == 0) do -- door open, rfidTakenCheck = total taken while door was open
checkMicroswitches()
calculateFree()
global.numberFreeEnd = global.numberFree
global.rfidTakenCheck = global.numberFreeStart - global.numberFreeEnd
msg.payload = {}
msg.payload = global.rfidTakenCheck
msg.topic = "rfidTakenCheck"
sendTo(3, msg)
if global.doorstatus == 1 then
break
end
end
程序由于while循环而卡住,所以我想更改它。我不知道如何。
答案 0 :(得分:0)
您可以将代码更改为以下内容:
local function doorClosed() -- door closed
checkMicroswitches() -- checks the microswitches
calculateFree() -- calculates the amount and place of free microswitches
global.numberFreeStart = global.numberFree
global.rfidTakenCheck = 0
end
local function doorOpen() -- door open, rfidTakenCheck = total taken while door was open
checkMicroswitches()
calculateFree()
global.numberFreeEnd = global.numberFree
global.rfidTakenCheck = global.numberFreeStart - global.numberFreeEnd
msg.payload = {}
msg.payload = global.rfidTakenCheck
msg.topic = "rfidTakenCheck"
sendTo(3, msg)
end
msg.payload = global.numberFreeStart
msg.topic = "numberFreeStart"
sendTo(4, msg)
while(true) do
global.doorstatus == updateDoorStatus() -- did not see an update to the status in the code you posted.
if global.doorstatus == 0 then
doorOpen()
else
doorClosed()
end
end
我假设您正在寻找无限循环的循环。如果不是这种情况,则可以删除while循环并仅使用其中的代码。