i have a problem with adafruit_mcp230xx python libraries from github.
My code is
Code: Select all
try:
from Adafruit_MCP230xx import Adafruit_MCP230XX
import sys, time
"""
The MCP23017 has 16 pins - A0 thru A7 + B0 thru B7. A0 is called 0 in the library, and A7 is called 7, then B0 continues from there as is called 8 and finally B7 is pin 15
"""
# Base variable settings
OUTPUT = 0
INPUT = 1
DISABLE = 0
ENABLE = 1
sensor = 8 # GPB0
door = 9 # GPB1
door_led = 11 # GPB3
mcp = Adafruit_MCP230XX( busnum = 1, address = 0x20, num_gpios = 16 )
# GPIO MCP230xx OUTPUT settings
mcp.config( door_led, OUTPUT)
# GPIO MCP230xx Input settings with Pull-UP resistor
mcp.pullup( sensor, 1 )
mcp.pullup( door, 1 )
while ( True ):
# Pir Sensor
if not mcp.input( sensor ):
print "Sensor:" + str( mcp.input( sensor ) )
# Magnetic Door switch
if not mcp.input( door ):
print "Door:" + str( mcp.input( door ) )
mcp.output( door_led, ENABLE )
time.sleep(0.3)
except KeyboardInterrupt:
sys.exit(127)
Code: Select all
Traceback (most recent call last):
File "switch.py", line 31, in <module>
if not mcp.input( sensor ):
File "/root/Adafruit_MCP230xx.py", line 118, in input
assert self.direction & (1 << pin) != 0, "Pin %s not set to input" % pin
AssertionError: Pin 8 not set to input
Why? Where is my error?
It seems to loose the input settings. I'm confused.
Following another code correctly runned
Code: Select all
# -*- coding: iso-8859-15 -*-
try:
from Adafruit_MCP230xx import Adafruit_MCP230XX
import re, sys, time
"""
The MCP23017 has 16 pins - A0 thru A7 + B0 thru B7. A0 is called 0 in the library, and A7 is called 7, then B0 continues from there as is called 8 and finally B7 is pin 15
mapping
pin1 = col2 = gpio7 => col[1] = 7
pin3 = col1 = gpio5 => col[0] = 5
pin5 = col3 = gpio3 => col[2] = 3
pin2 = raw1 = gpio6 => raw[0] = 6
pin4 = raw4 = gpio4 => raw[3] = 4
pin6 = raw3 = gpio2 => raw[2] = 2
pin7 = raw2 = gpio1 => raw[1] = 1
"""
# Base variable settings
OUTPUT = 0
INPUT = 1
code = ""
# keypad array settings
raw = [ 6, 1, 2, 4 ]
col = [ 5, 7, 3 ]
keypad = [ [ '1', '2', '3' ] , [ '4', '5', '6' ], [ '7', '8', '9' ], [ '*', '0', '#' ] ]
mcp = Adafruit_MCP230XX( busnum = 1, address = 0x20, num_gpios = 16 )
# GPIO MCP230xx OUTPUT settings
for gp_out in col:
mcp.config( gp_out, OUTPUT)
# GPIO MCP230xx Input settings with Pull-UP resistor
for gp_in in raw:
mcp.pullup( gp_in, 1 )
while ( True ):
# Base column settings
for gp_out in col:
mcp.output( gp_out, 1) #
# Column scans
for col_sel in col:
# set column to 0 value
mcp.output(col_sel, 0)
# Raw scans
for raw_sel in raw:
if mcp.input(raw_sel) == 0: # Key pressed
value = keypad [ raw.index(raw_sel)][ col.index(col_sel) ] # get key value
code += value # building code
#print "|" + code + "| - " + str(raw.index(raw_sel)) + ":" + str(col.index(col_sel))
if re.search( '^(\d|#)', code ): # If not start with '*' reset code variable: code must start with '*'
code = ""
if re.search( '(\*(\d+)#)', code ): # Ok code between '*' and '#'
print re.match( '.*(\*(\d+)#)', code ).group(2)
code = ""
while mcp.input(raw_sel) == 0: # wait until key released
time.sleep(0.1)
time.sleep(0.1)
except KeyboardInterrupt:
sys.exit(127)
Regards,
Stefano