The following code uses 2 MCP3008 chips attached to Rpi and my home alarm panel. These 16 analog values fluctuate between 600 and 715 out of 1023 depending on PIRs etc.
The Int value prints out:
Chip A - 1023 Chip B - 242
Chip A - 1023 Chip B - 1023
Chip A - 0 Chip B - 68
Chip A - 1023 Chip B - 599
Chip A - 1023 Chip B - 1023
Chip A - 16 Chip B - 1023
Chip A - 1023 Chip B - 1023
Chip A - 1023 Chip B - 1023
I need:
Value 1 = chip 1 readvalue 1
Value 2 = chip 1 readvalue 2
etc...
I tried strings, lists, IO, wrappers but I only get the first value.
Could you please go into more detail about what you want? Currently, it is unclear. – Sildoreth 35 mins ago
MCP3008_A and MCP3008_B print out the analog values from the chip.I want to assign each of those values to a specific name, eg.Value1 = 1023, Value2 = 654 etc.. Ultimately I want to be able to write that ( if Value 2 >=653 ) and (Value10 <= 702) do something. hope this explains it a bit better – maurice1 19 mins ago
Maybe to be clearer, At the bottom of the code I have #V1 = input.readline() , #V2= etc. I need to know what I should put in thereto read the output from MCP3008_A – maurice1 1 min ago edit
Could you please add all this information to the original post? – Sildoreth 38 secs ago
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_MCP3008/mcp3008.py
# just some bitbang code for testing all 8 channels
import RPi.GPIO as GPIO, time, os
import time # import time for the sleep function
import os
import sys
import cStringIO
from cStringIO import StringIO
import io
DEBUG = 1
GPIO.setmode(GPIO.BCM)
# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
if ((adcnum > 16) 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
if __name__=='__main__':
try:
# change these as desired
SPICLK = 11 #MCP3008_13
SPIMISO = 9 #MCP3008_12
SPIMOSI = 10 #MCP3008_11
SPICS = 8 #MCP3008_10
SPICLK2 = 17 #MCP3008_13
SPIMISO2 = 27 #MCP3008_12
SPIMOSI2 = 22 #MCP3008_11
SPICS2 = 7 #MCP3008_10
# set up the SPI interface pins
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)
GPIO.setup(SPICLK2, GPIO.OUT)
GPIO.setup(SPIMISO2, GPIO.IN)
GPIO.setup(SPIMOSI2, GPIO.OUT)
GPIO.setup(SPICS2, GPIO.OUT)
while True:
#print "",
time.sleep(1)
for adcnum in range(8):
MCP3008_A = readadc(adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS)
MCP3008_B = readadc(adcnum, SPICLK2, SPIMOSI2, SPIMISO2, SPICS2)
print ' Chip A - ' "%4d" % MCP3008_A,
print ' Chip B - ' "%4d" % MCP3008_B,
print ''
time.sleep(0.3)
d1=str(MCP3008_A)
d2=str(MCP3008_B)
print "--------------------------------------"
#V1 = input.readline()
#V2 = input.readline(2)
#V3 = input.readline()
#v4 = input.read()
except KeyboardInterrupt:
pass
GPIO.cleanup()