Thanks for the quick reply..
Please find the code below.
Please revert for any information required.
/*
Wireless IO using MCP23017, Arduino Nano and Xbee PRO S2B
Written by Vijay L Rajandekar, 04 June 2018
Setup 1 - Arduino Nano1 + MCP23017 ( 2nos)+ Xbee PRO S2B1
MCP23017 Output -pins 15~17 to GND, I2C bus address is 0x20
MCP23017 Input - pin 15 to VCC, pins 16~17 to GND, I2C bus address is 0x21
XBEE S2B1- Co-ordinator
Setup 2 - Arduino Nano 2 + MCP23017 ( 2nos)+ Xbee PRO S2B2
MCP23017 Output -pins 15~17 to GND, I2C bus address is 0x20
MCP23017 Input - pin 15 to VCC, pins 16~17 to GND, I2C bus address is 0x21
XBEE S2B2- Router
*/
#include "Wire.h"
byte inputs=0;//Input status fron MCP 23017 Port A
byte inputs1=0;//Input status fron MCP 23017 Port B
byte count =0 ;// Status read from Serial port sent from XBEE , Port A
byte count1=0;//Status read from Serial port sent from XBEE , Port B
void setup()
{
Serial.begin(9600);
Wire.begin(); // wake up I2C bus
Wire.beginTransmission(0x20);// Declare first MCP 23017 as Output
Wire.write(0x00); // IODIRA register
Wire.write(0x00); // set all of port A to outputs
Wire.endTransmission();
Wire.beginTransmission(0x20);
Wire.write(0x01); // IODIRB register
Wire.write(0x00); // set all of port B to outputs
Wire.endTransmission();
count=0;// After Power ON, reset all outputs
count1=0;//After Power ON, reset all outputs
{
Wire.beginTransmission(0x20); //Read a byte from XBEE and then output through port A
Wire.write(0x12); // GPIOA
Wire.write(count); // port A
Wire.endTransmission();
//delay(50); // for debounce
}
{
Wire.beginTransmission(0x20); //Read a byte from XBEE and then output through port B
Wire.write(0x13); // GPIOB
Wire.write(count1); // port B
Wire.endTransmission();
delay(50); // for debounce
}
}
// Read and write loop Main program
void loop()
{
if (Serial.available()>0) // Check if serial data avaialble ?. Read byte 1
{
count= Serial.read(); // If yes, read it.
//Serial.print (count, HEX);// This is temporary command to check whether the byte is read via serial port.
delay(50);
goto A;// Check for next read of serial data
}
else
{
count=0;// Reset the outputs
Wire.beginTransmission(0x20); //Read a byte from second zigbee and then output through port A
Wire.write(0x12); // GPIOA
Wire.write(count); // port A, switch off the outputs
Wire.endTransmission();
}
A:// Read next serial byte
if(Serial.available()>0)// Check if serial data avaialble ? Read byte 2
{
count1 = Serial.read();// If yes, read it.
// Serial.print (count1, HEX);// This is temporary command to check whether the byte is read via serial port.
delay(50);
goto Y; // Goto output program.
}
else
{
count1=0;// Reset the outputs
Wire.beginTransmission(0x20); //Read a byte from second zigbee and then output through port B
Wire.write(0x13); // GPIOB
Wire.write(count1); // port B,switch off the outputs
Wire.endTransmission();
delay(50); // for debounce
}
delay(50);
goto Z;// Goto read input status and send it on serial port via XBEE
Y:// Switch on the outputs based on the read status
{
Wire.beginTransmission(0x20); //Read a byte from XBEE and then output through port A
Wire.write(0x12); // GPIOA
Wire.write(count); // port A
Wire.endTransmission();
}
{
Wire.beginTransmission(0x20); //Read a byte from XBEE and then output through port B
Wire.write(0x13); // GPIOB
Wire.write(count1); // port B
Wire.endTransmission();
}
Z://Read input status and send it on serial port via XBEE
{
Wire.begin(); // wake up I2C bus
Wire.beginTransmission(0x21);
Wire.write(0x13); // set MCP23017 memory pointer to GPIOB address
Wire.endTransmission();
Wire.requestFrom(0x21, 1); // request one byte of data from MCP20317
inputs=Wire.read(); // store the incoming byte into "inputs"
delay(50); // for debounce
}
{
Wire.begin(); // wake up I2C bus
Wire.beginTransmission(0x21);
Wire.write(0x12); // set MCP23017 memory pointer to GPIOA address
Wire.endTransmission();
Wire.requestFrom(0x21, 1); // request one byte of data from MCP20317
inputs1=Wire.read(); // store the incoming byte into "inputs"
Serial.print (inputs, HEX);Serial.print (inputs1, HEX);// Transmit the Input bytes on serial port via XBEE
delay(10); // for debounce
}
}
============
Please note that both Arduino nano set 1 and set 2 codes are same as above.
Observations - The Arduino Nano set 1, MCP23017 outputs are not switched on as per the 2 HEX bytes sent from Arduino Nano set 2