如何在 Lua 脚本中像 arduino 一样拥有 currentMilis 和 previousMillis

我正在使用 simcom GPU 编写一个非常简单的 Lua 脚本。

它将计算 LED 闪烁次数并返回相应的计数器数字。到目前为止,我已经使用 os.clock() 来完成工作,但结果并不如我所预期。

function counter()
        local t1 = os.clock()
        while (ReadADC1() > 98) do

            local t2 = os.clock()
            local dt = t2 - t1
            t1 = t2
            local ledValue = ReadADC1()

            if ((ledValue >= OnThreshHold) and (dt < 1)) then -- 如果 LED 打开时间小于 1 秒钟
                ledCounter = ledCounter + 1
                --vmsleep(100)
            elseif ((ReadADC1() < OnThreshHold) and (dt > 2.3)) then-- 如果 LED 关闭超过 2 秒钟
                ledCounter = 0;
                --vmsleep(100)
            else
                ledCounter = ledCounter
            end

            --print (ledValue,"\r\n")
        end
        print(ledCounter,"\r\n")
        --return ledCounter
end

在这种情况下,如果我使用这个计数器()函数,它只在从 ReadADC() 输入值的时间很短然后停止时工作。如果 ADC 连续流过数据,则不起作用。

在 arduino 中,解决方案非常简单,因为可以使用 currentmillis 和 previousMillis 来确定闪烁之间的时间。那么在 Lua 中,我们有类似的东西吗?

点赞