1
喜女士和先生們,如何平均一個變量的最後十個實例,並顯示
,請原諒我,如果我輸入下面的代碼錯了,因爲這是我第一次在這裏發佈。我在這裏有一個python腳本,每隔十分之一秒輪詢一個電容器,目前正在使用一個光敏電阻來確定外部亮度。
唯一的問題是數值通常會偏差+/- 5左右。我想實現一行代碼,它每秒鐘平均最後10次民意調查並打印出來。我不知道從哪裏開始,任何幫助將不勝感激!
#!/usr/local/bin/python
import RPi.GPIO as GPIO
import time
import I2C_LCD_driver
GPIO.setmode(GPIO.BOARD)
mylcd = I2C_LCD_driver.lcd()
#define the pin that goes to the circuit
pin_to_circuit = 40
def rc_time (pin_to_circuit):
count = 0
#Output on the pin for
GPIO.setup(pin_to_circuit, GPIO.OUT)
GPIO.output(pin_to_circuit, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(pin_to_circuit, GPIO.IN)
#Count until the pin goes high
while (GPIO.input(pin_to_circuit) == GPIO.LOW):
count += 1
return count
#Catch when script is interrupted, cleanup correctly
try:
# Main loop
while True:
print "Current date & time " + time.strftime("%c")
print rc_time(pin_to_circuit)
a = rc_time(pin_to_circuit)
mylcd.lcd_display_string("->%s" %a)
mylcd.lcd_display_string("%s" %time.strftime("%m/%d/%Y %H:%M"), 2)
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
文件 「lightres1.py」,行43 除了一個KeyboardInterrupt: ^ 語法錯誤:無效的語法 –
感謝您的幫助,上面的錯誤是我在嘗試執行腳本時收到的內容 –
我現在還沒有在我面前有一個pi,所以無法直接測試,但如果您複製並粘貼了我的代碼,則可能是縮進。我編輯了我的帖子來修復縮進。 – BHawk