2

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 = "";
}
4
  • Exactly how would you like this to work? How does it work now? Any compiler warnings or errors? Commented Apr 8, 2017 at 8:23
  • You seem to have no clue about arrays on C. Maybe you should read some C array tutorials. Commented Apr 8, 2017 at 9:17
  • Also your understanding of serial communications is flawed. Commented Apr 8, 2017 at 9:18
  • You might like this new blog post of mine: hackingmajenkoblog.wordpress.com/2017/04/08/… Commented Apr 8, 2017 at 15:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.