繁体   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