0

Novice when it comes to python and I need help creating a basic window that prints and updates the variables I have changing based on the readings the RPi collects. The window will display ON or OFF based on whether the voltages are >1.0 or <1.0. So far the variable 'values' just prints in the terminal. My code so far:

    import time
import spidev
import os
import sys
from windowex import *
# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008

# Software SPI configuration:
CLK  = 12
MISO = 17
MOSI = 27
CS   = 22
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

x0 = "OFF"
x1 = "OFF"
x2 = "OFF"
x3 = "OFF"
x4 = "OFF"
x5 = "OFF"
x6 = "OFF"
x7 = "OFF"

y0 = "ON"
y1 = "ON"
y2 = "ON"
y3 = "ON"
y4 = "ON"
y5 = "ON"
y6 = "ON"
y7 = "ON"

# Main program loop.
while True:
    # 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). rounds to nearest 2 decimals
        values[i] = round((mcp.read_adc(i)*(3.3/1023)),2)

#conditional statements for ON and OFF renaming        
    if values[0] < 1.0:
         values[0] = x0   
    else:
         values[0] = y0

    if values[1] < 1.0:
         values[1] = x1     
    else:
         values[1] = y1

    if values[2] < 1.0:
         values[2] = x2      
    else:
         values[2] = y2

    if values[3] < 1.0:
         values[3] = x3      
    else:
         values[3] = y3

    if values[4] < 1.0:
         values[4] = x4       
    else:
         values[4] = y4

    if values[5] < 1.0:
         values[5] = x5     
    else:
         values[5] = y5

    if values[6] < 1.0:
         values[6] = x6       
    else:
         values[6] = y6

    if values[7] < 1.0:
         values[7] = x7
    else:
         values[7] = y7  


    print values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7]

    time.sleep(0.5)
  • why do you set the values of the voltage only to change it to a string? Whats the difference between `y0` and `y1` ? – corn3lius Jul 06 '17 at 14:03
  • I honestly named y0 and y1 etc for the sake of using a seperate one for each value. and I set the values of the voltages so i can check them if need be. For a final result however, ON and OFF is all that is needed. – jasamples Jul 06 '17 at 14:10

1 Answers1

0
while True:
    # Read all the ADC channel values in a list.
    values = [0]*8
    output = ""
    for i in range(8):
        # The read_adc function will get the value of the specified channel (0-7). rounds to nearest 2 decimals
        values[i] = round((mcp.read_adc(i)*(3.3/1023)),2)
        output += "{}-{} ".format(i, "OFF" if values[i] < 1.0 else "ON" )
    print output
    time.sleep(0.5)


>> "0-ON 1-OFF 2-ON 3-OFF ..."

Why change the value to a string later just print if it is the range you want "ON/OFF". All the x0,x1,... are the same string, why do the need to be defined at all ?

corn3lius
  • 4,857
  • 2
  • 31
  • 36
  • I was leaving them as separate variables so i could comment the if statements out if i wanted to know the true value the Pi was reading. Your way simplifies my code but i still need help getting these into a window that would dynamically update. Thanks for helping already – jasamples Jul 06 '17 at 16:50