0

I use bluetooth (works like serial monitor). Let me say i send a text to my hc-05 (same as sending to serial monitor) My current code is displaying text from serial monitor/HC-05 TO LCD

#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
String readString;
void setup() {
 Serial.begin(9600);
 lcd.begin(16, 2);
}
void loop() {
 while(Serial.available()){
 delay(50);
 char c=Serial.read();
 readString+=c;
 }
 if(readString.length()>0){
 Serial.println(readString);
 lcd.clear();
 lcd.print(readString);
 readString="";
 }
}
from serial monitor i type `a,b,c,d`

now my goal is to just get the a and display in lcd

or

in other words i want to get the value of a b c d in the text that is CSV to list or array and get value for index 1,2,3,4

OR

split string by delimiter

so how can i do it

details-ARUINO UNO

asked Apr 1, 2021 at 9:43

2 Answers 2

2

Use the strtok function.

From Geeks for geeks (https://www.geeksforgeeks.org/strtok-strtok_r-functions-c-examples/):

arguments:

  • str: The string which is to be split
  • delim: The character on the basis of which the split will be done Return value

The function performs one split and returns a pointer to the token split up. A null pointer is returned if the string cannot be split.

answered Apr 1, 2021 at 10:02
3
  • 2
    Note that to use strtok() you would first have to migrate away from using String, which is a good thing in any case. Commented Apr 1, 2021 at 10:05
  • but the example is for c++ can you provide the example for Arduino Commented Apr 2, 2021 at 3:50
  • @hifitech Even though the Arduino/C++ string can be used, it's not a good idea because of the very low SRAM the Arduino Uno has. I assume with a default function and plenty of examples to be found on internet of using strtok it would not be a big problem to find a solution. Commented Apr 2, 2021 at 7:29
1

i searched in stack exchange Arduino but i dint get answer so i posted but when i searched in stack over flow i got link this is my final code

String part01;
String getValue(String data, char separator, int index)
{
 int found = 0;
 int strIndex[] = {0, -1};
 int maxIndex = data.length()-1;
 for(int i=0; i<=maxIndex && found<=index; i++){
 if(data.charAt(i)==separator || i==maxIndex){
 found++;
 strIndex[0] = strIndex[1]+1;
 strIndex[1] = (i == maxIndex) ? i+1 : i;
 }
 }
 return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void setup()
{
 Serial.begin(9600);
}
void loop()
{
 String part01 = getValue("523;524;525",';',1);
 Serial.println(part01);
}
answered Apr 2, 2021 at 4:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.