這是我在Python中的校準碼。我有16個模擬傳感器連接到我的覆盆子pi 2, GPIO 7,11,13,15控制着一個16到1多路複用器,我將模擬信號送到一個MCP3002讓他們讀到我的樹莓。 我知道這個代碼的工作原理,因爲直到我校準代碼寫入的時候,一切都很順利。但僅僅因爲我有不同數量的來自每個傳感器的讀數,一個數字作爲一個閾值是不夠的。 所以我創建了2個整數數組和一個for循環來遍歷每個傳感器, 三次讀取並獲得平均價格並將其存儲到calibration_max數組中。然後打印一條消息,並在3秒鐘之前給你一個消息,併爲此傳感器做同樣的事情,以便它可以存儲從該傳感器獲得的最小值並將其存儲到calibration_min數組中,以便我可以將它用作參考我的門檻。語法錯誤在Python與數組和循環與如果ELIF?
但我每次去這個錯誤信息並編譯到shell。
有錯誤你的程序:爲什麼我收到此錯誤信息 取消縮進不匹配任何外部縮進級別
?
calibration_max=array.array('i',(0 for m in range(0,16)))
calibration_min=array.array('i',(0 for n in range(0,16)))
for m in range(0, 16):
x == m
if x == 0:
GPIO.output(7, 0)
GPIO.output(11, 0)
GPIO.output(13, 0)
GPIO.output(15, 0)
elif x == 1:
GPIO.output(7, 1)
GPIO.output(11, 0)
GPIO.output(13, 0)
GPIO.output(15, 0)
elif x == 2:
GPIO.output(7, 0)
GPIO.output(11, 1)
GPIO.output(13, 0)
GPIO.output(15, 0)
elif x == 3:
GPIO.output(7, 1)
GPIO.output(11, 1)
GPIO.output(13, 0)
GPIO.output(15, 0)
elif x == 4:
GPIO.output(7, 0)
GPIO.output(11, 0)
GPIO.output(13, 1)
GPIO.output(15, 0)
elif x == 5:
GPIO.output(7, 1)
GPIO.output(11, 0)
GPIO.output(13, 1)
GPIO.output(15, 0)
elif x == 6:
GPIO.output(7, 0)
GPIO.output(11, 1)
GPIO.output(13, 1)
GPIO.output(15, 0)
elif x == 7:
GPIO.output(7, 1)
GPIO.output(11, 1)
GPIO.output(13, 1)
GPIO.output(15, 0)
elif x == 8:
GPIO.output(7, 0)
GPIO.output(11, 0)
GPIO.output(13, 0)
GPIO.output(15, 1)
elif x == 9:
GPIO.output(7, 1)
GPIO.output(11, 0)
GPIO.output(13, 0)
GPIO.output(15, 1)
elif x == 10:
GPIO.output(7, 0)
GPIO.output(11, 1)
GPIO.output(13, 0)
GPIO.output(15, 1)
elif x == 11:
GPIO.output(7, 1)
GPIO.output(11, 1)
GPIO.output(13, 0)
GPIO.output(15, 1)
elif x == 12:
GPIO.output(7, 0)
GPIO.output(11, 0)
GPIO.output(13, 1)
GPIO.output(15, 1)
elif x == 13:
GPIO.output(7, 1)
GPIO.output(11, 0)
GPIO.output(13, 1)
GPIO.output(15, 1)
elif x == 14:
GPIO.output(7, 0)
GPIO.output(11, 1)
GPIO.output(13, 1)
GPIO.output(15, 1)
elif x == 15:
GPIO.output(7, 1)
GPIO.output(11, 1)
GPIO.output(13, 1)
GPIO.output(15, 1)
# average three readings to get a more stable one
channeldata_1 = read_mcp3002(0) # get CH0 input
sleep(0.001)
channeldata_2 = read_mcp3002(0) # get CH0 input
sleep(0.001)
channeldata_3 = read_mcp3002(0) # get CH0 input
channeldata = (channeldata_1+channeldata_2+channeldata_3)/3
calibration_max(m) == channeldata
print("place magnet over sensor No:"(n+1))
sleep(3)
# average three readings to get a more stable one
channeldata_1 = read_mcp3002(0) # get CH0 input
sleep(0.001)
channeldata_2 = read_mcp3002(0) # get CH0 input
sleep(0.001)
channeldata_3 = read_mcp3002(0) # get CH0 input
channeldata = (channeldata_1+channeldata_2+channeldata_3)/3
calibration_min(n) == channeldata
n=n+1
它的實際工作!謝謝 !用python我並不擅長。現在我收到關於數組聲明的錯誤消息。我如何在Python中聲明一個數組? –
這是我進入我的終端:文件「./Documents/Examples/spitest2.py」,行91,在 calibration_max = array.array('i',(0代表範圍內的m(0,16 ))) NameError:name'array'未定義 –
您正在probalby文件頂部缺少一個'import array'。這將導入包含python數組定義的模塊。 – Oliver