PaulS:
How many requests is that code going to send to the slave?
We still don't know what the slave is. We still don't have proof that the request is getting to the slave. We still don't have proof that the slave does not reply.
Usually, one allows more than a few nanoseconds for the request to get to the slave, for the slave to handle the request, for the slave to generate a response, and for the response to get back to the slave.
Try this:
if (requestNewData == true)
{
Serial.println("Sending request to slave...");
Wire.requestFrom(I2C_ADDR, 1);
while(Wire.available() == 0)
{
// wait for a reply
}
char data = Wire.read();
Serial.println(data);
requestNewData = false;
}
It will still generate a single request, and block until the slave responds.
But, if the slave DOES respond, with data that you expect/seems reasonable, then we can show you how to use the boolean to control sending a request to slave and, asynchronously, getting response, WITHOUT blocking.
One step at a time, though.
Unforunately your example didn't worked too, but here is my Slave code and it's working if the Master has a Serial loop, so I'm getting the results with it.
I tought that the Master has also a listener ISR availability, no?
Here is my actual Slave code, and it's piece of cake:
#include "Wire.h"
#include "Keypad.h"
#define INT_DEBUG
#define I2C_ADDR 0x1A
char pKey;
const byte nRows = 4;
const byte nCols = 4;
char kMap[nRows][nCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rPins[nRows] = {9,8,7,6};
byte cPins[nCols]= {5,4,3,2};
Keypad myKeyPad = Keypad(makeKeymap(kMap), rPins, cPins, nRows, nCols);
void setup()
{
#ifdef INT_DEBUG
Serial.begin(9600);
Serial.println(F("I2C Matrix Keypad here..."));
#endif
Wire.begin(I2C_ADDR);
Wire.onRequest(requestEvent);
}
void loop()
{
pKey = myKeyPad.getKey();
if (pKey != NO_KEY)
{
#ifdef INT_DEBUG
Serial.println(pKey);
#endif
}
delay(100);
}
void requestEvent()
{
if (pKey != NO_KEY)
{
Wire.write(pKey);
}
memset((void*)pKey, 0, sizeof(pKey));
}
btw...my hardware confiration is: Arduino UNO as a Master and ATmega8 Slave as a Kaypad repetitor.
So, what I wish to achieve from all of it, when I press any key on the Slave, the Master need to receive it.