I am working on a project where I need to split incoming data from serial (time to be exact, so aa:bb:cc) and need to split it into aa then bb and cc, ideally into an array. I know I could use readStringUntil and make 3 separate variables, but I would prefer it is in arrays. Here is the code that supposed to do this. It works with predefined string (eg. 00:00:00), but with reading serial it says that the initializer fails to determine size of "input". Thank you in advance for helpful suggestions.
Here is the code:
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
Serial.begin(9600);
while(!Serial.available()){} //wait until Serial data received
String vst = Serial.readString();
char buf[vst.length()+1];
vst.toCharArray(buf, vst.length());
char input[] = vst;
char delic[] = ":";
char* sub = strtok(input, delic);
while(sub != NULL){
Serial.println(ptr);
sub = strtok(NULL, delic);
}
}
vst.toCharArray()
(which copies the data) just directly usevst.c_str()
which is a char array already. You don't needbuf