M
M
mIka012021-04-16 16:33:20
Arduino
mIka01, 2021-04-16 16:33:20

How to divide Char into an array by sign in Arduino?

Hello, I have a question with Arduino.
How to transfer several int variables over the Serial port. For example [1 45 67 94 23 543].
As I understand it, only a Char string can be transferred via Serial. How can I convert it to an int array with a width of 6, if the numbers of the transmitted string are separated by spaces.
PS As I understand it, there is no Split in the Arduino IDE.

Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Zaitsev, 2021-04-16
@mIka01

Collect from char`s string:

char inChar = (char)Serial.read();
inputString += inChar;

When input is complete, such as when passing an end-of-line character or a line break
if (inChar != '\n' or inChar != '\r'){

Finding the position of a space in a string:
int i = inputString.indexOf(" ");

If the position is not 0, then a gap is found, take the substring before and after the gap:

if (i > 0) {
String command = inputString.substring(0, i);//The part of the string about spaces
String paramString = inputString.substring(i + 1, inputString.length());//This is the part of the string after the space

In my example, the second part of the string is int, I translate it into a number of type int:
unsigned long param = paramString.toInt();

You need to int the first part and continue to divide the second until the victory

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question