簡體   English   中英

求和此 python 從連接到 Raspberry Pi 的模數轉換器產生的值的總和?

[英]Sum the total of the values this python produces from an analog to digital converter connected to a Raspberry Pi?

我正在學校開展一個初學者項目,該項目使用 Raspberry Pi 3、10 位 MCP3008 模數轉換器 (ADC) 和 5v 太陽能電池來讀取太陽能電池從光中吸收的能量。 我已經使用了模數轉換器附帶的代碼並對其進行了一些修改,但我想對程序運行完成時顯示的數字求和並輸出到 excel。 它設置為每秒顯示一個值,因此我想在最后對所有值求和。 我該怎么做? 生成 ADC 值的代碼部分是由帶有 ADC 的 Adafruit 網站提供給我的,所以我仍在努力理解它。 運行 Python 2.7。 這是我的代碼:

# Simple example of reading the MCP3008 analog input channels and printing
# them all out for solar test

import time
import datetime
# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008


# Software SPI configuration:
CLK  = 18
MISO = 23
#MISO = Master Input Slave Output
MOSI = 24
#MOSI = Master Output Slave Input
CS   = 25
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

# Hardware SPI configuration:
# SPI_PORT   = 0
# SPI_DEVICE = 0
# mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
runTime = input("How many minutes should the program run?: ")

t_end = time.time() + runTime * 60

print('Reading MCP3008 values, press Ctrl+C to quit...')
# Print nice channel column headers.
file = open('solartest.xls','a')
st = time.time()
print (datetime.datetime.fromtimestamp(st).strftime('%Y-%m-%d %H:%M:%S'))
file.write(datetime.datetime.fromtimestamp(st).strftime('%Y-%m-%d %H:%M:%S') + "\n")
print('| {0:>4} |'.format(*range(8)))
#file.write('{0:>4}'.format(*range(8)) + "\t")
print('-' * 8)
# Main program loop.
#while True:
while time.time() < t_end:
    # Read all the ADC channel values in a list.
    values = [0]*8
    for i in range(8):
        # The read_adc function will get the value of the specified channel (0-7).
        values[i] = mcp.read_adc(i)
    # Print the ADC values.
    print('| {0:>4} |'.format(*values))
    file.write('{0:>4}'.format(*values) + '\n')
    # Pause for a second.
    time.sleep(1)
#end = datetime.datetime.fromtimestamp().strftime('%Y-%m-%d %H:%M:%S')
ed = time.time()
print (datetime.datetime.fromtimestamp(ed).strftime('%Y-%m-%d %H:%M:%S'))
file.write(datetime.datetime.fromtimestamp(ed).strftime('%Y-%m-%d %H:%M:%S'))
file.close()

如果可能,我想計算每個 ADC 值的電壓,並在另一列中寫入 excel。 計算電壓的公式為 V = (5/1024) * ADC 值。 ADC 值是程序運行時每秒打印的數字。 我試過安裝 openpyxl 但它不起作用,因為我的 python 版本不是 3.6 或更高版本,我無法更新它,因為它是學校設備。

#while True:
allValues = []
while time.time() < t_end:
    # Read all the ADC channel values in a list.
    values = [0]*8
    for i in range(8):
        # The read_adc function will get the value of the specified channel (0-7).
        values[i] = mcp.read_adc(i)
    allValues.append(values)
    # Print the ADC values.
    print('| {0:>4} |'.format(*values))
    file.write('{0:>4}'.format(*values) + '\n')
    # Pause for a second.
    time.sleep(1)

print(sum(sum(v) for v in allValues))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM