问候python程序员。我正在建立一个树莓派项目,该项目旨在在我的花园中使用湿度传感器。我设计的程序可以读取30秒钟的读数,然后将它们平均为一个数字。它使用四个传感器并在python3.5上运行,并且所有传感器都能正常工作,但是让python进行数学运算来平均输入时遇到了问题。我认为它正在添加变量,但似乎并没有将它们分开。这是代码:
TEST_SOIL()
outpuT = str(output)
print("Moisture:", outpuT)
GPIO.output(buzzer,GPIO.HIGH)
sleep(0.1) # Delay in seconds
GPIO.output(buzzer,GPIO.LOW)
sleep(0.1)
GPIO.output(buzzer,GPIO.HIGH)
sleep(0.1) # Delay in seconds
GPIO.output(buzzer,GPIO.LOW)
print("TESTING SOIL")
o1 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o2 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o3 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o4 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o5 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o6 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o7 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o8 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o9 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o10 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o11 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o12 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o13 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o14 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o15 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o16 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o17 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o18 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o19 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o20 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o21 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o22 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o23 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o24 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o25 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o26 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o27 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o28 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o29 = (output + output2 + output3 + output4/4)
sleep(1)
TEST_SOIL()
o30 = (output + output2 + output3 + output4/4)
sleep(1)
o = (o1 + o2 + o3 + o4 + o5 + o6 + o7 + o8 + o9 + o10 + o11 + o12 + o13 + o14 + o15 + o16 + o17 + o18 + o19 + o20 + o21 + o22 + o23 + o24 + o25 + o26 + o27 + o28 + o29 + o30)
o = (o/30)
[如果有人想知道“ TEST_SOIL()”是什么,那是我在程序前面创建的我自己的函数。它在这里定义:
def TEST_SOIL():
output = analogInput(0) # Reading from CH0
output = interp(output, [0, 1023], [100, 0])
output = int(output)
output = (output - 12.5)
output2 = analogInput(1) # Reading from CH0
output2 = interp(output2, [1, 1023], [100, 1])
output2 = int(output2)
output2 = (output2 - 12.5)
output3 = analogInput(2) # Reading from CH0
output3 = interp(output3, [2, 1023], [100, 2])
output3 = int(output3)
output3 = (output3 - 12.5)
output4 = analogInput(3) # Reading from CH0
output4 = interp(output4, [3, 1023], [100, 3])
output4 = int(output4)
output4 = (output4 - 12.5)
我需要提醒一下,此代码还控制了一个小型蜂鸣器,以消除与“ GPIO.output(buzzer,high / low)”文本的任何混淆。如果有人可以告诉我如何解决此代码,那将是很棒的。:)
我有点自由地重构了您的代码。
我认为这4种不同的模拟读数必须保持原样,并且不要碰它们,否则……
test_soil
现在返回读数的4元组。average_output()
将所有可迭代的数字求平均。beep()
发出蜂鸣声。main
]在循环中调用上述位,将值收集到一个数组中,然后对其求平均。def TEST_SOIL():
output = analogInput(0) # Reading from CH0
output = interp(output, [0, 1023], [100, 0])
output = int(output)
output = output - 12.5
output2 = analogInput(1) # Reading from CH0
output2 = interp(output2, [1, 1023], [100, 1])
output2 = int(output2)
output2 = output2 - 12.5
output3 = analogInput(2) # Reading from CH0
output3 = interp(output3, [2, 1023], [100, 2])
output3 = int(output3)
output3 = output3 - 12.5
output4 = analogInput(3) # Reading from CH0
output4 = interp(output4, [3, 1023], [100, 3])
output4 = int(output4)
output4 = output4 - 12.5
return (output, output2, output3, output4)
def average_output(o):
return sum(o) / len(o)
def beep():
GPIO.output(buzzer, GPIO.HIGH)
sleep(0.1) # Delay in seconds
GPIO.output(buzzer, GPIO.LOW)
def main():
ao = average_output(TEST_SOIL())
print("Initial Moisture:", ao)
beep()
beep()
print("TESTING SOIL")
values = []
for x in range(30):
values.append(average_output(TEST_SOIL()))
sleep(1)
final_average = average_output(values)
print("Final Moisture:", final_average )
将(output + output2 + output3 + output4/4)
更改为(output + output2 + output3 + output4)/4
您的代码首先将最后一个变量(ouotput4)除以四,然后将它们加在一起。
((编辑:忘记了列表没有平均功能。对不起。您可以将sum()
用于某些列表,然后将其除以该列表的len()
)] >>
这可能能够修复您的代码并对其进行整理您可以使用for循环来简化工作,而不必再次编写相同的内容