Hello All,
I have been working on a project for a while now where I have 3 ESP32's connected to each other via I2C (1 Master, 2 Slaves)
I am having issues with the slave boards not receiving the full communication (Only receiving 2 digits instead of 3)
I believe it has to do with the Wire.onreceive event getting interrupted by the code getting executed on the other core, however, as I just stated, the actual code itself is being run on the other core so I dont understand why it would be interrupting the onreceive
Instead of receiving "060" from the master, the slaves will rarely receive "06" or "0" (Which will then lead to an invalid data length message)
Here is a rough diagram of how everything is connected:

Here are some examples of the code:
Master:
void inputHandler(void* parameter) {
while (!globals.shutdown) {
if (digitalRead(PIN1) == 0) {
if (digitalRead(PIN2) == 1) {
globals.right.desired = 15;
globals.left.desired = 1;
} else if (digitalRead(PIN3) == 1) {
globals.right.desired = 1;
globals.left.desired = 15;
} else {
globals.right.desired = 1;
globals.left.desired = 1;
}
} else if (digitalRead(PIN1) == 1) {
if (digitalRead(PIN2) == 1) {
globals.right.desired = 25;
globals.left.desired = 2;
} else if (digitalRead(PIN3) == 1) {
globals.right.desired = 2;
globals.left.desired = 25;
} else {
globals.right.desired = 2;
globals.left.desired = 2;
}
}
String txr = "";
String txl = "";
if (globals.web.style < 10) {
txr += "0";
txl += "0";
}
txr += String(globals.web.style);
txl += String(globals.web.style);
switch (globals.right.desired) {
case 1:
txr += "0";
break;
case 15:
txr += "1";
break;
case 2:
txr += "2";
break;
case 25:
txr += "3";
break;
}
switch (globals.left.desired) {
case 1:
txl += "0";
break;
case 15:
txl += "1";
break;
case 2:
txl += "2";
break;
case 25:
txl += "3";
break;
}
// Perform data validity check
if (!isValidTransmittedData(txr) || !isValidTransmittedData(txl)) {
Serial.println("Invalid TX Data");
} else {
// Right (Slave 1)
Wire.beginTransmission(0x0A);
Wire.write(txr.c_str());
Wire.endTransmission();
// Left (Slave 2)
Wire.beginTransmission(0x1A);
Wire.write(txl.c_str());
Wire.endTransmission();
}
delay(10);
}
}
Slave ReceiveEvent:
void receiveEvent(int byteCount) {
String receivedData = ""; // Variable to store the received data
while (Wire.available()) {
char c = Wire.read(); // Read the received data as a character
receivedData += c; // Concatenate the character to the receivedData string
}
// Validate the received data
if (receivedData.length() != 3) {
Serial.println("Invalid data length");
return;
}
for (int i = 0; i < receivedData.length(); i++) {
if (!isdigit(receivedData[i])) {
Serial.println("Invalid data format");
return;
}
}
// Extracting individual numbers
int digit0 = receivedData[0] - '0';
int digit1 = receivedData[1] - '0';
int digit2 = receivedData[2] - '0';
int combinedDigits = digit0 * 10 + digit1;
// Processing the extracted numbers
globals.style = combinedDigits;
switch (digit2) {
case 0:
globals.desired = 10;
break;
case 1:
globals.desired = 15;
break;
case 2:
globals.desired = 20;
break;
case 3:
globals.desired = 25;
break;
default:
Serial.println("Invalid value for digit2");
return;
}
if ((globals.desired != globals.running && globals.running < 3) || globals.style != globals.activestyle) {
globals.halt = true;
}
//Serial.println(receivedData);
delay(5);
}
Slave Running Function:
void anims::style6::mode10() {
//bool isFirstInstance = true;
while (!globals.shutdown) {
fadeInOut(8, 0, true, 30);
if (checkWithDelay(1)) { return; }
fadeInOut(7, 8, false, 20);
if (checkWithDelay(1)) { return; }
fadeInOut(6, 7, false, 15);
if (checkWithDelay(1)) { return; }
fadeInOut(5, 6, false, 15);
if (checkWithDelay(1)) { return; }
fadeInOut(4, 5, false, 15);
if (checkWithDelay(1)) { return; }
fadeInOut(3, 4, false, 15);
if (checkWithDelay(1)) { return; }
fadeInOut(2, 3, false, 10);
if (checkWithDelay(1)) { return; }
fadeInOut(1, 2, false, 10);
if (checkWithDelay(1)) { return; }
fadeInOut(0, 1, false, 10);
if (checkWithDelay(1)) { return; }
fadeOut(0, 60);
if (checkWithDelay(190)) {
return;
}
//isFirstInstance = false;
}
}
void fadeInOut(int ring, int prev, bool isFirstInstance, int speed) {
// Number of steps for fading
int numSteps = speed; // Adjust the number of steps for faster fading
// Fade in and fade out
for (int i = 0; i < numSteps; i++)
{
for (const int & value: it.LEDS_BOTH(ring, ring, ring, ring, 1)) {
int brightness = map(i, 0, numSteps - 1, 0, 255 - (31.9 * ring));
globals.leds[value] = CRGB(brightness, brightness, brightness);
}
// Fade out the previous ring, excluding the first instance
if (!isFirstInstance)
{
for (const int & value: it.LEDS_BOTH(prev, prev, prev, prev, 1)) {
int brightness = map(numSteps - i - 1, 0, numSteps - 1, 0, 255 - (31.9 * ring));
globals.leds[value] = CRGB(brightness, brightness, brightness);
}
}
FastLED.show();
}
}
int checkWithDelay(int time) {
for (int i = 0; i <= time; i++) {
delay(5);
if (globals.halt) {
globals.halt = false;
return 1;
}
}
return 0;
}
I know I have kinda just dumped this, I just cannot figure out why I am getting interruptions to the receive event running on the other core.
For reference, I have a different "Running Function" which (almost) never gives me data corruption, So it seems to be an issue with this function.
Any help would be greatly appreciated
Cheers
