store the data received from a sensor in a python list

Asked

Viewed 140 times

2

How can I save 100 values received from a sensor in a python list, to later calculate their average?

This is the code I have so far:

#biblioteca para chamar ficheiros de outra extensão
from subprocess import check_output
import time

tam = 100

def criaVetor(valor):
    vec = []
    for i in range(tam): # vamos fazer isto tam (N) vezes
    vec.append((valor)) 
return vec

def Average(lst):
    return sum(lst)/len(lst)



while True:
    # chama o ficheiro em linguagem C em modo diferencial
    sensor_data= check_output(['./raspandmax','-d'], shell=False).decode()
    output_value = (sensor_data)# mostra o valor analógico lido no ADC
    adc_value = float(output_value)# converte o valor analógico de string para float
    #print(adc_value)# mostra o valor da conversão         
    print(criaVetor(adc_values)
    time.sleep(1)

when I run the code it always adds the same value read and not the following values as I have when running the code

[0.8554112934311049, 0.8554112934311049, 0.8554112934311049, 0.8554112934311049, 0.8554112934311049, 0.8554112934311049, 0.8554112934311049, 0.8554112934311049, 0.8554112934311049, 0.8554112934311049]

the idea is to read the values as follows, for example:

valor 1 = 0.25
valor 2 = 0.26
valor 3 = 0.3
valor4 = ...
valor5 = ...
etc..

and insert the values as follows : [0.25, 0.26, 0.3, ..., ...] and so on

I got this solution to be always reading the vector vec and go making the average of the total vector, it may not be the best solution but for the purpose it shows what I want perfectly.

  from subprocess import check_output
        import time

        tam = 1
        vec = []
        def criaVetor(valor):

            for i in range(tam): # vamos fazer isto tam (N) vezes
            vec.append((valor)) 
        return vec

        def Average(lst):
            return sum(lst)/len(lst)



        while True:
            # chama o ficheiro em linguagem C em modo diferencial
            sensor_data= check_output(['./raspandmax','-d'], shell=False).decode()
            output_value = (sensor_data)# mostra o valor analógico lido no ADC
            adc_value = float(output_value)# converte o valor analógico de string para float
            #print(adc_value)# mostra o valor da conversão         

            time.sleep(1)
            print("{0:.4f}".format(Average(criaVetor(adc_values))) 

but now I have a doubt that is to create a cycle while to always be reading the file raspandmax and give value, and later just wanted inside the while True that showed me the analog value received

  • Make a repeat loop that reads the sensor and stores in the list. To add an element to a list you can use the method list.append. Want to try? Anything you can [Dit] the question and ask what you have already done and what was the problem found.

  • Display the code, showing at least the data received from the sensor

  • I already have the question edited with the code

  • Friend, first try to put vec = [], out of the function just to see if something changes, otherwise, try to increase the number of measures beyond 3000~4000, because I believe that before the value changes, it has already filled all the positions of the vector.

1 answer

0

Hello, all right?

The problem of repetition is due to function criaVetor. In it you add to the list vec the same value tam times. Try adding list values inside while.

Take a look at this suggestion:

#biblioteca para chamar ficheiros de outra extensão
from subprocess import check_output
import time

def Average(lst):
    return sum(lst)/len(lst)

tam = 100
count = 0
vec = []
while count<tam:
    # chama o ficheiro em linguagem C em modo diferencial
    sensor_data= check_output(['./raspandmax','-d'], shell=False).decode()
    output_value = (sensor_data)# mostra o valor analógico lido no ADC
    adc_value = float(output_value)# converte o valor analógico de string para float
    #print(adc_value)# mostra o valor da conversão

    vec.append(adc_values)
    time.sleep(1)
    count += 1

print(vec)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.