How to store output values produced in a while loop

This is the place for queries that don't fit in any of the other categories.

How to store output values produced in a while loop

Postby Deltafee » Wed May 21, 2014 5:18 am

Hello, So I have this code:
Code: Select all
#!/usr/bin/env python
import time
import os
import RPi.GPIO as GPIO
import eeml

GPIO.setmode(GPIO.BCM)
DEBUG = 1
LOGGER = 1

# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
        if ((adcnum > 7) or (adcnum < 0)):
                return -1
        GPIO.output(cspin, True)

        GPIO.output(clockpin, False)  # start clock low
        GPIO.output(cspin, False)     # bring CS low

        commandout = adcnum
        commandout |= 0x18  # start bit + single-ended bit
        commandout <<= 3    # we only need to send 5 bits here
        for i in range(5):
                if (commandout & 0x80):
                        GPIO.output(mosipin, True)
                else:
                        GPIO.output(mosipin, False)
                commandout <<= 1
                GPIO.output(clockpin, True)
                GPIO.output(clockpin, False)

        adcout = 0
        # read in one empty bit, one null bit and 10 ADC bits
        for i in range(12):
                GPIO.output(clockpin, True)
                GPIO.output(clockpin, False)
                adcout <<= 1
                if (GPIO.input(misopin)):
                        adcout |= 0x1

        GPIO.output(cspin, True)

        adcout /= 2       # first bit is 'null' so drop it
        return adcout

# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 18
SPIMISO = 23
SPIMOSI = 24
SPICS1 = 25
SPICS2 = 8

# set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS1, GPIO.OUT)
GPIO.setup(SPICS2, GPIO.OUT)

   def read_values_mV(): ## Read the analog pin and convert to milivolts
    millivolts = [0] * 16 # init list with values zero
      for i in range(8):
          millivolts[i] = readadc(i, SPICLK, SPIMOSI, SPIMISO, SPICS1) * ( 3300.0 / 1024.0)
           millivolts[i + 8] = readadc(i, SPICLK, SPIMOSI, SPIMISO, SPICS2) * ( 3300.0 / 1024.0)
         return millivolts
x = 0
while x < 5:
        if DEBUG:
                print read_values_mV()      
                print"\n"
                x += 1
                time.sleep(1)  # hang out and do nothing for 1 seconds


so to sum up what it does, is it produces values from a analog to digital converter chip with 16 channels and the while loop spits out a voltage value every second. I need help adding each column of the 5 lists together, so it would look like:
1st: [A1, B1, C1, D1]
2nd: [A2, B2, C2, D2]
3rd: [A3, B3, C3, D3]
4th: [A4, B4, C4, D4]
5th: [A5, B5, C5, D5]

[A1+A2+A3+A4+A5, B1+B2+B3+B4+B5, ..., D1+D2+D3+D4+D5]
'
and then multiply all of it by (1/5), I have tried using zip and np; I really need to get this working.
Deltafee
 
Posts: 13
Joined: Mon Mar 31, 2014 12:04 am

Re: How to store output values produced in a while loop

Postby micseydel » Wed May 21, 2014 6:21 am

Can you rephrase your question with less than 10 lines of code and no imports?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1491
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: How to store output values produced in a while loop

Postby Deltafee » Wed May 21, 2014 9:53 pm

sorry :)

Code: Select all
x = 0
while x < 5:
        if DEBUG:
                print read_values_mV()      
                print"\n"
                x += 1
                time.sleep(5)  # hang out and do nothing for 5 seconds


where: print read values is a analog to digital converter chip and is programed to create a list with 16 elements. What I want is for the five list columns created by while loop to be averaged.

so the code produces five lists all with 16 elements like this:

1st list:
    A1, B1, .., P1
2nd list:
    A2, B2, ..., P2
3rd list:
    A3, B3, ..., P3
4th list:
    A4, B4, ..., P4
5th list:
    A5, B5, ..., P5

what I want to do is have the code take the average of the 5 lists' elements; so in the example I want to do this:

    (1/5)(A1+A2+A3+A4+A5), (1/5)(B1+B2+B3+B4+B5), ..., (1/5)(P1+P2+P3+P4+P5)

so how do I do that?
Deltafee
 
Posts: 13
Joined: Mon Mar 31, 2014 12:04 am

Re: How to store output values produced in a while loop

Postby micseydel » Thu May 22, 2014 1:51 am

In the future, the code you post for a question like this should be runnable and as close to what you ultimately want as you can get. It also shouldn't involve any user input, unless absolutely necessary, or sleep() calls or DEBUG checks or anything of the sort. It should be as absolutely simple as possible. It should be complex enough though that there's some clear connection between what you're describing and what's in the code; I have no idea what A, B or P are.

Here is an example
Code: Select all
>>> def arbitrary_function(x):
   return x**2

>>>
>>> seen_values = []
>>> for n in xrange(10):
   result = arbitrary_function(n)
   seen_values.append(result)

   
>>> print seen_values
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>>
>>> def average(lst):
   return 1.0 * sum(lst) / len(lst)

>>> average(seen_values)
28.5

If I haven't answered your question, I'd ask kindly that you try to refine based on the new feedback I provided at the beginning of this post.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1491
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Marbelous, W3C [Linkcheck] and 3 guests