You could found one way to do it from here and get some ideas to write your requestCallback()-function with digitalRead.. Especially this part might interest you:
//For slave
#include
const byte AnalogueInputPin = 0;
const byte SlaveDeviceId = 1;
void setup()
{
// Start I²C bus as a slave
Wire.begin(SlaveDeviceId);
// Set the callback to call when data is requested.
Wire.onRequest(requestCallback);
}
void loop()
{
}
void requestCallback()
{
// Contrived example - transmit a value from an analogue pin.
int input = analogRead(AnalogueInputPin);
// To send multiple bytes from the slave,
// you have to fill your own buffer and send it all at once.
uint8_t buffer[2];
buffer[0] = input >> 8;
buffer[1] = input & 0xff;
Wire.write(buffer, 2);
}
Master just have to keep sendind requests to the slave..
Pirate_Duino:
Maybe a simple question, but I am not having any luck researching by myself...
I want an Arduino Master to get the digital on or off condition of a pin, lets say 13 for this example.
I have succeeded in getting the 2 to communicate on I2c, but I cannot find how to monitor the high or low state of each pin on the slave.
Thanks
in the Slaves onRequestEvent(),
void onRequestEvent(){
uint8_t pins[3];
pins[0] = PIND; // digital 0..7 Pin0,1 are RX,TX
pins[1] = PINB; // port B pins, 8,9,10,11,12,13 ,
pins[1] = pins[1] & B00111111; // the two high bits are actually connected to the crystal
pins[2] = PINC;// Analog Pins 0..5
pins[2] = pins[2] & B00111111; // Bit 7,6 are not visible on the 328p, also A5, A4 are the I2C pins
Wire.write(&pins,3);
}
You will have to have the Master issue a Wire.requestFrom(slaveid,3);
And you will receive three bytes with all of the Slave Arduino Pin States.