1

How can I use delimiter/split serial input in a loop function? I want to use each word from the input and check a database, to check availability.

#include SoftwareSerial.h>
#define MAX_WORD_LENGTH 15

char messageArray[5][MAX_WORD_LENGTH + 1];
SoftwareSerial BT(10, 11);
String command = "";
char message[] = "";

void setup() {
  BT.begin(9600);
  Serial.begin(9600);
  while (BT.available()) {
    delay(10);
    char c = BT.read();
    command += c;
  }

  if (command.length() > 0) {
    Serial.println(command);
    char message[] = {command};
    for (int i = 0; i < 5; i++) {
      // you can 'reset' the array by writing a null to the first element of each
      messageArray[i][0] = '\0';
    }
    sscanf(message, "%s %s %s %s %s", messageArray[0], messageArray[1], messageArray[2], messageArray[3], messageArray[4]);
    for (int i = 0; i < 5; i++) {
      Serial.println(messageArray[i]);
      delay(1000);
    }
  }
}

void loop() {
  for (int i = 0; i < 5; i++) {
    // you can 'reset' the array by writing a null to the first element of each
    messageArray[i][0] = NULL;
  }
  command = "";
}
  • Exactly how would you like this to work? How does it work now? Any compiler warnings or errors? – Mikael Patel Apr 8 '17 at 8:23
  • You seem to have no clue about arrays on C. Maybe you should read some C array tutorials. – Majenko Apr 8 '17 at 9:17
  • Also your understanding of serial communications is flawed. – Majenko Apr 8 '17 at 9:18
  • You might like this new blog post of mine: hackingmajenkoblog.wordpress.com/2017/04/08/… – Majenko Apr 8 '17 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.