From 6fbb53b6bd18cf1e805d7cae0a8e0b10ce9f3866 Mon Sep 17 00:00:00 2001 From: KenwoodFox Date: Sun, 4 Jul 2021 22:10:05 -0400 Subject: [PATCH] Fix a bug where arduino would crash on incorrectly sized i2c payload --- crawler_software/arduino/crawler_slave/crawler_slave.ino | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crawler_software/arduino/crawler_slave/crawler_slave.ino b/crawler_software/arduino/crawler_slave/crawler_slave.ino index 47abae0..dee8522 100644 --- a/crawler_software/arduino/crawler_slave/crawler_slave.ino +++ b/crawler_software/arduino/crawler_slave/crawler_slave.ino @@ -41,8 +41,7 @@ void loop() { // This method runs when we receive a message void receiveEvent(int n) { - Wire.read(); - digitalWrite(LED_BUILTIN, HIGH); + Wire.read(); // Remove smbus trash if (true) { // Dont do anything if this is not true id = Wire.read(); // ID of the servo/device to access val = Wire.read(); // Value to assign @@ -56,5 +55,9 @@ void receiveEvent(int n) { break; } } - digitalWrite(LED_BUILTIN, LOW); + + // Prevents a bug where if bytes are left in buffer, arduino crashes. + while (Wire.available()) { + Wire.read(); + } }