如何在lua脚本中的led闪烁之间间隔时间

时间:2019-01-30 19:32:14

标签: lua arduino led

我正在学习编写lua脚本,并且试图编写一个简单的脚本。

本质上,我需要计算LED闪光的次数。如果两次闪烁之间的间隔大于一秒钟,我们将加总计数器并显示一条消息,该消息与led闪烁的次数相对应。

简单来说,LED闪烁一,二,三遍,然后停止1秒钟,我们将计数器打印为3,并显示消息“闪烁3次”。如果停止时间超过1秒,我们将重置计数器。

这是我到目前为止所拥有的

-------------------------------------------------------
-- Function ReadADC1
-- read raw value and transform to value of power of 10. 
-- This is not nessasrily if we know the raw value of threshhold
------------------------------------------------------- 
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,"\r\n")
    return adc_voltage_value
end
-- end of readADC1() TESTED

------------------------
-- function counter()
-- count how many time led flashing when knowing threshhold of voltage to be on
ledCounter = 0 --initialized
-----------------------
function counter()
        local t1 = os.clock()   
        while (ReadADC1()>48) do
            local t2 = os.clock()
            local dt = t2 - t1
            t1 = t2
            local ledValue = ReadADC1()

            if ( ((ReadADC1() >= OnThreshHold) and (dt<0.4)) or ((ReadADC1() < OnThreshHold) and (dt<0.1))) then-- if led is off for more than 1 second
                    ledCounter = ledCounter + 1
                    vmsleep(10)                     
            else 
                ledCounter = 0
            end

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

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

这项工作很不可靠,因为我用arduino模拟了led闪光灯。它仅适用于3次闪烁,但不适用于其他号码。

欢迎任何建议或修正

0 个答案:

没有答案