From 83a33cc20225116343286ea05b7d126f8f37cf16 Mon Sep 17 00:00:00 2001 From: KenwoodFox Date: Sat, 3 Jul 2021 23:57:17 -0400 Subject: [PATCH] Finalize better reading of full string over i2c and splitting --- .../arduino/crawler_slave/crawler_slave.ino | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/crawler_software/arduino/crawler_slave/crawler_slave.ino b/crawler_software/arduino/crawler_slave/crawler_slave.ino index 201cd47..dcfca5c 100644 --- a/crawler_software/arduino/crawler_slave/crawler_slave.ino +++ b/crawler_software/arduino/crawler_slave/crawler_slave.ino @@ -11,7 +11,11 @@ Servo windowWiperServo; // This is a variable capable of storing up to 4 items -char instruction[4] = ""; +char instruction[32] = ""; + +// Values for the id, and raw value of an instruction +char * id; +char * raw_value; void setup() { // Attach the wiper servo to pin 9 @@ -21,12 +25,12 @@ void setup() { Serial.begin(9600); // This is the address the pi will speak to us at - Wire.begin(0x8); + Wire.begin(0x4); // Call receiveEvent when data received Wire.onReceive(receiveEvent); - Serial.print("Finished Setup."); + Serial.println("Finished Setup."); } // Just loop to keep the running code alive, and wait for events to happen. @@ -36,10 +40,27 @@ void loop() { // This method runs when we receive a message void receiveEvent(int n) { - for (int i = 0; i < n; i++) //n is equal to the number of data bytes received - { + + for (int i = 0; i < n; i++) { instruction[i] = Wire.read(); + instruction[i + 1] = '\0'; //add null after ea. char + } + + //RPi first byte is cmd byte so shift everything to the left 1 pos so temp contains our string + for (int i = 0; i < n; ++i) + instruction[i] = instruction[i + 1]; + + id = strtok (instruction, "-"); + raw_value = strtok (NULL, "-"); + + int value = atoi(raw_value); + + Serial.println(id); + Serial.println(value); + + // Switch statements dont work on char * + if (strcmp(id,"WIPE") == 0) // if id, and WIPE cancel out + { + windowWiperServo.write(value); } - - Serial.print(instruction); }