0

问候 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(蜂鸣器,高/低)”文本的任何混淆。如果有人能告诉我如何修复此代码,那就太好了。
:)

4

3 回答 3

3

我冒昧地重构了你的代码。

我认为 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 )
于 2020-05-17T13:36:33.670 回答
2

更改(output + output2 + output3 + output4/4)(output + output2 + output3 + output4)/4

您的代码首先将最后一个变量 (ouotput4) 除以四,然后将它们加在一起。

(编辑:忘记列表没有平均功能。抱歉。您可以使用sum()某个列表,然后将其除以len()该列表)

于 2020-05-17T13:33:50.803 回答
1

这可能能够修复您的代码并稍微整理一下,而不是再次编写相同的东西您可以使用 for 循环使工作更轻松

for step in range(30):
    TEST_SOIL()
    op_avg = (output1 + output2 + output3 + output4) / 4.0
    op = op + op_avg
    sleep(1)
op = op/30

希望能帮助到你

于 2020-05-17T13:55:56.447 回答