CanSat-India-Updated / Altitude_GCS_Slave
Altitude_GCS_Slave
Raw
#include <Wire.h>
#define ALT_ADDR 10
String packet;
String parts[21]; // Declare parts array outside of the loop

void setup() {
  Wire.begin(ALT_ADDR);
  Serial.begin(9600);
}

void loop() {
  packet = "";
  while (Wire.available()) {
    char c = Wire.read(); // Read each character from the buffer
    packet += c;
    if (c == '>') {
      int i = 0;
      char *pch = strtok(packet.c_str(), ",");
      while (pch != NULL && i < 21) { // Ensure i is within bounds
        parts[i++] = pch;
        pch = strtok(NULL, ",");
      }
      // Print altitude to Serial Monitor
      if (i > 3) { // Check if altitude index exists
        Serial.println(parts[3]);
      } else {
        Serial.println("Altitude not found.");
      }
    }
  }
}