Hi, I'm having issues trying to use the wire library to send an analog reading from the slave to the master.
Master Code:
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop()
{
Wire.requestFrom(2, 2); // request 6 bytes from slave device #2
while(Wire.available()) // slave may send less than requested
{
int c = Wire.read(); // receive a byte as character
Serial.println(c); // print the character
}
Serial.println(" ");
delay(500);
}
Slave Code:
#include <Wire.h>
int i;
void setup()
{
Serial.begin(9600);
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
}
void loop()
{
i=analogRead(0);
Serial.println(i);
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
Wire.write(i); // respond with message of 6 bytes
// as expected by master
}
Can anyone help me get the analog reading from the slave to display on the serial port of the master? Thank you.