In TWI Bus (aka I2C Bus) Protocol --
1. Master sends executive command (s) to the Slave by executing Wire.write() instruction. The Salve is interrupted, and from the ISR it goes to Wire.onReceive() subroutine (SUR). In the SUR, the received command is interpreted and necessary actions are taken, after that, the control goes back to the loop() function.
2. Master sends data read command (s) to the Slave by executing Wire.requestFrom() instruction. The Slave is interrupted, and from the ISR it goes to Wire.onRequest() subroutine. In the SUR, the Slave finishes sending data to the Master; after that, the control goes back to loop() function.
3. For the Master, to receive flag status information/data from the Wire.onReceive() SUR of the Slave, it (Master) has to be configured as Slave as well (my understanding).
4. Just after sending the Wire.onRequest() command to the Slave, the Master will start polling the FIFO buffer to grasp the data (slider position) coming from the Wire.onRequest() SUB of the Slave.
Hope that the above information will become helpful to debug the program of the OP.
BTW: -1 is required to be defined in terms of a numerical value. What is about -- to assign a Control Character to it like ACK(06)?
UNO-1: Master/Slave Codes
#include <Wire.h>
volatile bool flag3 = false; //this flag synchronizes the timing of the events between UNO-1 and UNO-2
void setup()
{
Serial.begin(9600);
Wire.begin(0x50);
Wire.onReceive(receiveEvent);
Wire.beginTransmission(0x051);
Wire.write(0x01); //Command for Motor1
Wire.endTransmission();
while(flag3 == false)
;
Wire.requestFrom(0x51, 1); //slider position
while (Wire.available() ==0)
;
byte x = Wire.read();
Serial.print("Slider value received: ");
Serial.println(x, HEX); //show slider position
}
void loop()
{
}
void receiveEvent()
{
sei();
if(Wire.read() == 0x06)
{
Serial.println("Motor-1 Moved Command ACK Received from Slave!");
flag3 = true;
}
}
UNO-2: Master/Slave Codes
#include<Wire.h>
void setup()
{
Wire.begin(0x51); //address as a slave
Serial.begin(9600);
pinMode(13, OUTPUT);
analogReference(DEFAULT);
Wire.onReceive(receiveEvent);
Wire.onRequest(sendEvent);
}
void loop()
{
}
void receiveEvent(int howmany)
{
sei();
if(Wire.read() == 0x01) //Command to Move motor-1
{
Serial.println("Motor-1 Move Command Received");
//--SEND ack (0X06)--
Wire.beginTransmission(0x50);
Wire.write(0x06); //ACK Send
// Wire.endTransmission();
//---Motor-1 move command
digitalWrite(13, HIGH);
delay(5000); //5-sec delay to move slider
digitalWrite(13, LOW);
Serial.println("Slider Moved!");
Wire.endTransmission();
}
}
void sendEvent(int howmany)
{
Serial.println("Request Received to send Slider Position");
Wire.write(analogRead(A0)); // slider position is sent from Ch-0 of ADC (3.3V = 0XA6)
Serial.println("Slider-1 position is sent to Master");
}
Operating procedures:
1. Upload UNO-1 codes.
2. Bring in UNO-1 Serial Monitor
3. Upload UNO-2 codes.
4. Bring in UNO-2 Serial Monitor
5. Press and Hold down Reset Switch of UNO-1
6. Activate the Reset Switch of UNO-2
7. Release the Reset Swich of UNO-1
8. Let us observe that the following dialog message has appeared on the Serial Monitors of UNO-1 (Top SM) and UNO-2 (Bottom SM).