Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在lua中设置事件之间的时间间隔_Lua - Fatal编程技术网

如何在lua中设置事件之间的时间间隔

如何在lua中设置事件之间的时间间隔,lua,Lua,我正在用lua编写一个简单的脚本。如果LED亮起,计数器将增加1。如果led熄灭超过1秒,则会重置计数器 那么,我们如何准确地在lua中为这样的事件计时呢 这就是我迄今为止一直在用多种方法测试的东西 function ReadADC1() local adc_voltage_value = 0 adc_voltage_value = tonumber(adc.readadc()) * 2 -- 0.10 --get dec number out of this -- need t

我正在用lua编写一个简单的脚本。如果LED亮起,计数器将增加1。如果led熄灭超过1秒,则会重置计数器

那么,我们如何准确地在lua中为这样的事件计时呢

这就是我迄今为止一直在用多种方法测试的东西

function ReadADC1()
    local adc_voltage_value = 0
    adc_voltage_value = tonumber(adc.readadc()) * 2 -- 0.10 --get dec number out of this -- need to know where package adc come from
    --convert to voltage
    adc_voltage_value = adc_voltage_value *0.000537109375 --get V
    adc_voltage_value = math.floor(adc_voltage_value *1000 +0.5) --since number is base off resolution

    --print (adc_voltage_value)
    return adc_voltage_value

end
-- end of readADC1() TESTED

function interval()
local counter1 =0 
ledValue = ReadADC1()
if (ledValue >= OnThreshHold) then
    ledStatus = 1
    t1=os.time()
else
    ledStatus = 0
    t2 = os.time()
end

--counter1 =0
for i=1,20 do
if (ledStatus == 1) then -- if led is off for more than 1 second, reset counter = 0
counter1 = counter1 + 1
elseif ((ledStatus ==0 and ((os.difftime(os.time(),t2)/100000) > 1000))) then -- increment counter when led is on
counter1 = 0

end
end
print (counter1)
结束 我确信interval的逻辑是错误的,因为os.time返回一个巨大的数字(我假设它是以秒为单位的,而不是以秒为单位的)

欢迎提出任何建议或解决方案。在此之前,我尝试了vmstarttimer和vmstoptimmer,但不确定它是如何工作的

编辑:

函数计数器()
本地LED值=ReadADC1()
本地t1=os.clock()
尽管如此
本地t2=操作系统时钟()
局部dt=t2-t1
t1=t2
如果((led值1))然后——如果led关闭超过1秒
ledCounter=0;
其他的
LED计数器=LED计数器
终止
打印(LED计数器)
终止
终止

最终,它将是return ledCounter而不是print counter,因为我将把counter的值插入另一个函数,该函数将打印一条与计数器数量对应的消息。您可以使用
os.clock
,它以秒为单位返回程序启动后的运行时间

返回程序使用的CPU时间(以秒为单位)的近似值。

这个函数可以这样使用

local t1 = os.clock() -- begin
local t2 = os.clock() -- end
local dt = t2 - t1    -- calulate delta time

-- or looped

local t1 = os.clock() -- begin
while true do
    local t2 = os.clock() -- end
    local dt = t2 - t1    -- calulate delta time
    t1 = t2               -- reset t1

    -- use dt ...
end

-- or wait for time elapsed
-- runs until 1 second passed    

local t1 = os.clock()
while (os.clock() - t1) < 1 do
    -- do stuff while dt is smaller than 1

    -- could even reset timer (t1) to current to 
    -- repeat waiting
    -- t1 = os.clock() | ...
end

-- logic for your example

function counter()
    local time = os.clock()
    local lastLedOn = false
    local counter = 0

    while true do
        if os.clock() - time > 1.0 then
            break
        end

        if getLedValue() == on then -- replace
            time = os.clock()

            if not lastLedOn then
                lastLedOn = true
                counter = counter + 1

                -- print(counter) | or here if you want to print repeatedly
            end
        end
    end

    print(counter)
end -- was unable to test it
local t1=os.clock()--开始
本地t2=操作系统时钟()--结束
局部dt=t2-t1——计算增量时间
--或循环
本地t1=os.clock()--开始
尽管如此
本地t2=操作系统时钟()--结束
局部dt=t2-t1——计算增量时间
t1=t2——复位t1
--使用dt。。。
终止
--或者等待时间过去
--运行到1秒结束
本地t1=os.clock()
而(os.clock()-t1)<1 do
--当dt小于1时执行操作
--甚至可以将计时器(t1)重置为当前值
--重复等待
--t1=os.clock()|。。。
终止
--你的例子的逻辑
函数计数器()
本地时间=操作系统时钟()
本地lastLedOn=false
本地计数器=0
尽管如此
如果os.clock()-time>1.0,则
打破
终止
如果getLedValue()==打开,则--替换
time=os.clock()
如果不是lastLedOn那么
拉斯特尔顿=真
计数器=计数器+1
--打印(计数器)|如果要重复打印,请在此处打印
终止
终止
终止
打印(计数器)
结束--无法测试它

这是否只测量程序在开始和结束之间执行的增量时间,而不是LED打开和关闭之间的时间(使用脉冲边缘),您可以指定开始和结束。在我的例子中,t1是测量的开始,t2是测量的结束。dt是t1和t2之间的时间。在循环示例中,dt是循环每次迭代之间的时间(以秒为单位)。您能看看我编辑的代码并告诉我是否理解错误吗?我的计数器只返回0。感谢you@JackVu我想你理解第一部分错了。为您的问题提供了完整的逻辑示例。
local t1 = os.clock() -- begin
local t2 = os.clock() -- end
local dt = t2 - t1    -- calulate delta time

-- or looped

local t1 = os.clock() -- begin
while true do
    local t2 = os.clock() -- end
    local dt = t2 - t1    -- calulate delta time
    t1 = t2               -- reset t1

    -- use dt ...
end

-- or wait for time elapsed
-- runs until 1 second passed    

local t1 = os.clock()
while (os.clock() - t1) < 1 do
    -- do stuff while dt is smaller than 1

    -- could even reset timer (t1) to current to 
    -- repeat waiting
    -- t1 = os.clock() | ...
end

-- logic for your example

function counter()
    local time = os.clock()
    local lastLedOn = false
    local counter = 0

    while true do
        if os.clock() - time > 1.0 then
            break
        end

        if getLedValue() == on then -- replace
            time = os.clock()

            if not lastLedOn then
                lastLedOn = true
                counter = counter + 1

                -- print(counter) | or here if you want to print repeatedly
            end
        end
    end

    print(counter)
end -- was unable to test it