Simultaneous Read and Write 2 variable HEX bytes with Arduino Nano and XBee S2B

Hi all,

This is Vijay here ..

I am using two separate Arduino Nanos with 2 Xbee S2Bs for wireless send and receive 2 variable HEX bytes between the two.

Operation in brief:

Arduino Nano board 1:

Arduino Nano1 reads the status of the 2 input ports of MCP23017 and sends those 2 HEX bytes using serial interface to Xbee1.
Xbee1 is configured as Coordinator and it sends the HEX bytes to Arduino Nano board2 XBee2.
At the same time, Xbee1 reads the HEX bytes sent from Arduino Nano board 2 XBee2 and switches on the outputs on Arduino Nano board 1 as per HEX bytes received.

Arduino Nano board 2:

Arduino Nano2 reads the status of the 2 input ports of MCP23017 and sends those 2 HEX bytes using serial interface to Xbee2.
Xbee2 is configured as Router and it sends the HEX bytes to Arduino Nano board 1 XBee1.
At the same time, Xbee2 reads the HEX bytes sent from Arduino Nano board 1 XBee1 and switches on the outputs on Arduino Nano board 2 as per HEX bytes received.

The code is written and individual Arduino Nano board 1 can read the bytes sent from Arduino Nano board 2 and vice versa when programmed either read or write functions.

But when code with both Read and Write functions are combined and used for both boards, the read and write doesnot work and garbage data is output.

Think the code runs in loop searching for data receipt and probably both read and write causes the malfunction.

Am using

"if serial.available()>0" to receive the HEX bytes is causing the delays in read in both boards.

Can someone suggest suitably so that both read and write would take place reliably without any delay ?

Thanks

Vijay

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