And here is my test code that i am using for the moment to experiment
Code for the master Arduino (sending and receiving data)
// Wire Master Reader sender
// based in the arduino ide examble with some additions by me
// caslor 03/05/2020
#include <Wire.h>
// Define Slave I2C Address
#define SLAVE_ADDR 8
// Define Slave answer size
#define ANSWERSIZE 6
const byte button = 4; // pin to read button
char t[10] = {};
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
pinMode(button, INPUT_PULLUP);
}
void loop() {
if (digitalRead(button) == LOW ) {
Command2Slave() ;
}
else if (digitalRead(button) == HIGH ) {
Serial.println("Stand By mode");
}
delay(500);
}
void Command2Slave() {
Wire.beginTransmission(SLAVE_ADDR);
Wire.write('U');
Wire.endTransmission();
delay(500);
ReceiveSlave() ;
}
void ReceiveSlave() {
Wire.requestFrom(SLAVE_ADDR, ANSWERSIZE);
int i = 0; //counter for each bite as it arrives
while (Wire.available()) {
t[i] = Wire.read(); // every character that arrives it put in order in the empty array "t"
i = i + 1;
}
Serial.println(t); //shows the data in the array t
delay(500);
}
/*
void ReceiveSlave() {
Wire.requestFrom(SLAVE_ADDR, ANSWERSIZE);
while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(500);
}
*/
Code for the Slave Arduino (sending and receiving data)
// Wire Slave Reader sender
// based in the arduino ide examble with some additions by me
// caslor 03/05/2020
#include <Wire.h>
#define SLAVE_ADDR 8
#define ANSWERSIZE 6
const byte potentiometer1 = A0;
const byte potentiometer2 = A2;
const byte potentiometer3 = A3;
const byte testbutton = 4;
int Watervalue1;
int Watervalue2;
int Watervalue3;
char *myStrings1[] = {"Full", "Half", "RES", "EMPTY" };
int j = 0;
char *myStrings2[] = {"100", "90", "80", "70", "60", "50", "40", "30", "20",
"10", "5", "0"
};
int k = 0;
char *myStrings3[] = {"LOW", "0", "0.5", "1", "1.5", "2", "2.5", "3", "3.5", "4", "4.5", "5"};
int m = 0;
char *myStrings4[2];
void setup() {
pinMode(testbutton, INPUT_PULLUP);
Wire.begin(SLAVE_ADDR);
// Function to run when data requested from master
Wire.onRequest(requestEvent);
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600);
}
void loop() {
// Serial.println(Windvalue);
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
// as expected by master
Wire.write(myStrings1[j]);
// Wire.write(myStrings2[k]);
// Wire.write(myStrings3[m]);
// Wire.write(myStrings4[0]);
}
void receiveEvent(int howMany)
{
if (Wire.available())
{
char c = Wire.read();
if (c == 'U')
{
SensorsRead1() ;
SensorsRead2() ;
SensorsRead3() ;
ButtonState() ;
}
}
}
void SensorsRead1() {
Watervalue1 = analogRead(potentiometer1);
Watervalue1 = map(Watervalue1, 0, 1023, 0, 255);
if (Watervalue1 <= 1) {
j = 1;
}
else if (Watervalue1 >= 10 && Watervalue1 < 100 ) {
j = 2;
}
else if (Watervalue1 >= 100 && Watervalue1 < 200 ) {
j = 3;
}
}
void SensorsRead2() {
Watervalue2 = analogRead(potentiometer2);
Watervalue2 = map(Watervalue2, 0, 1023, 0, 255);
if (Watervalue2 <= 1) {
k = 1;
}
else if (Watervalue2 >= 10 && Watervalue2 < 100 ) {
k = 2;
}
else if (Watervalue2 >= 100 && Watervalue2 < 200 ) {
k = 3;
}
}
void SensorsRead3() {
Watervalue3 = analogRead(potentiometer3);
Watervalue3 = map(Watervalue3, 0, 1023, 0, 255);
if (Watervalue3 <= 1) {
m = 1;
}
else if (Watervalue3 >= 10 && Watervalue3 < 100 ) {
m = 2;
}
else if (Watervalue3 >= 100 && Watervalue3 < 200 ) {
m = 3;
}
}
void ButtonState() {
if (digitalRead(testbutton) == LOW) {
myStrings4[0] = " Low " ;
}
else if (digitalRead(testbutton) == HIGH) {
myStrings4[0] = " High" ;
}
}