I am using the arduino uno with an expansion I/O shield using a chip(MCP23017). Everything appears to work properly except when testing for a zero the if statement doesn't work. The following is my code with the resulting IDE serial printed results attached. I have tried changing the variables to different data types which did not change the results. The expansion shield recognizes the input change as the printed result shows but testing the result doesn't work. The if statement never recognizes zero. What am I doing wrong?
#include <Wire.h>
const byte mcp_address = 0x20; // I2C Address of MCP23017 Chip
const byte GPIOA = 0x12; // Register Address of Port A
const byte GPIOB = 0x13; // Register Address of Port B
const byte IODIRA = 0x00; // IODIRA register
const byte IODIRB = 0x01; // IODIRB register
const byte PinDA[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01}; //Pin DA0,1,2,3,4,5,6,7
const byte PinDB[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; //Pin DB0,1,2,3,4,5,6,7
byte inputs;
void setup() {
pinMode(7, OUTPUT);
pinMode(6, INPUT_PULLUP);
//Send settings to MCP device
Wire.begin(); // join i2c bus (address optional for master)
Wire.beginTransmission(mcp_address);
Wire.write((byte)IODIRB); // IODIRB register
Wire.write((byte)0x3F); // set GPIOB-0-5 to inputs
Wire.endTransmission();
Wire.beginTransmission(mcp_address);
Wire.write((byte)0x0D); // Pull up Register (GPPUA)
Wire.write((byte)0x3F); // set 0-5 bits as inputs
Wire.endTransmission();
}
void loop() {
Serial.begin(9600);
int result = Read_IO(PinDB[1]);
Serial.print("result ");
Serial.println(result);
if (result == 1) {
Serial.println("result=1");
digitalWrite(7, HIGH);
delay(500);
}
else {
Serial.println("result=0");
digitalWrite(7, LOW);
delay(500);
}
}
byte Read_IO(byte x) {//Read the input pin (x) and return value
Wire.beginTransmission(mcp_address);
Wire.write((byte)GPIOB); // set MCP23017 memory pointer to GPIOB address
Wire.endTransmission();
Wire.requestFrom(mcp_address, 1); // request one byte of data from MCP20317
byte inputs = Wire.read(); // store the incoming byte into "inputs"
Serial.print("read results inputs ");
Serial.println(inputs, BIN);
Serial.print("inputs & x ");
Serial.println(inputs & x, BIN);
delay(500);
if (inputs & x ==0) {
return 1;
}
else {
return 0;
}
}
IO Test Printout.txt (261 Bytes)