text: getting values from variable and storing it to array
  Posted: Mon Mar 19, 2018 6:28 pm
  hello!
  i want to take mean of the first 10 values from the result of 'voltage' variable (mentioned in code below) . can anyone help me how to do it?
  i want to take mean of the first 10 values from the result of 'voltage' variable (mentioned in code below) . can anyone help me how to do it?
  Code: Select all
  from time import sleep import RPi.GPIO as GPIO import spidev import numpy as np GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(11,GPIO.OUT) spi= spidev.SpiDev() spi.open(0,0) spi.max_speed_hz= 250000 def poll_sensor(channel): assert 0 <= channel <= 1 if channel: cbyte=0b11000000 else: cbyte=0b10000000 r=spi.xfer2([1,cbyte,0]) return ((r[1]&31)<<6) + ( r[2]>>2) try: while True: channel=0 channeldata=poll_sensor(channel) voltage = round(((channeldata*5.0)/1024),0) print ('voltage : {}'.format(voltage)) print ('data : {}\n'.format(channeldata) ) my_volt=list(map(float,voltage) print ( my_volt[0:10]) print(np.mean(my_sal[0:10])) if channeldata<=500: GPIO.output(11,True) sleep(2) GPIO.output(11, False) sleep(2) else: print ('liquid is ok') sleep(2) finally: spi.close() print"\n all cleaned up" GPIO.cleanup() [code/] 'voltage' variable is in the while loop and 'my_volt' list will take first 10 values from the variable and will show the mean value. can anyone help me in this code that where i am wrong?