我正在编写一个非常简单的lua脚本,用于计算led闪光灯的次数
但是,当我尝试将前一个函数的返回值分配给另一个函数中的局部变量时,它总是给我错误。
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 counter()
local ledValue = readADC1()
--local interval -- interval between led on and off. If interval larger than 1 second, reset counter
--TODO add interval definition
local interval = os.clock()
while (true) do
if ((ledValue >= OnThreshHold) and (interval < 1000)) then -- if value exceed threshhold, mean it on
ledCounter = ledCounter + 1
elseif ((ledValue < OnThreshHold) and (os.clock() - interval > 1000)) then -- if led off for longer than 1 second
ledCounter = 0 -- reset counter to one and prepare for next flashing
else
ledCounter = ledCounter -- not sure if we need this. Doing this might cause bug later on
end
end
--return ledCounter
print (ledCounter,"\r\n")
end
-- end of counter()
如您所见,我正在尝试从ReadADC1函数为ledValue分配adc_voltage_value。我以为应该工作,但事实证明没有。它给了我这个错误:
> +LUA ERROR: LEDcounter.lua:29: attempt to call global 'readADC1' (a nil value)
>
> stack traceback:
>
> LEDcounter.lua:29: in main chunk
>
> [C]: ?
我已经使用黑盒调试并独立测试每个功能,ReadADC1给我一个不错的数值。但是当我测试counter()函数时,它给了我那个错误
欢迎提出任何建议或修正。我正在尝试学习
答案 0 :(得分:1)
仔细查看您的错误,可以很清楚地看到Lua很难找到该名称的函数(或其他任何变量)。如果您仔细观察,会发现对readADC1
的调用是无效的,因为没有这样的功能。这是因为您定义的函数被称为ReadADC1
。注意大写字母,并记住变量在Lua中是区分大小写的。