How to select integers from data sentence

Hi all :slight_smile:

I have a little problem with integers. I am sending a data sentence from my phone to arduino via bluetooth. The sentence loks like this: STARTX:integerY:integerM:integerEND. So the problem is how to select those integers from that sentence?

How to use this forum

So the problem is how to select those integers from that sentence?

The solution depends on how you store that sentence. Try adding this code to yours:

what code?

what code?

Exactly. You have to post your code, first.

My code is very simple. Just arduino example for serial communication.

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(57600);
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte); 
  }
}

So, you don't have a sentence (string). You have one letter a time. It is very hard to extract useful information from one character.

When you actually start saving the data in a string, then you can think about parsing the string, and extracting useful information from the string.

So if I understand right, I have to put this line int inByte = Serial1.read(); into a string function? Something like that?

int inByte = Serial1.read();
String stringOne =  String('inByte');

So if I understand right, I have to put this line...into a string function?

No. You need to store the character returned by the Serial.read() method in the string. You do not put a line of code in a string.

Something like that?

No. That is 100% completely wrong in every possible way.

The first thing you need to learn is that string is NOT the same as String. A string is appropriate on the Arduino. A String is not.

Learn to use strings, and you'll never need, or want, to use Strings. Strings are for lazy people and zoomkat. Not you.

How exactly I can see that I am sending a full sentence or a seperate characters?

How exactly I can see that I am sending a full sentence or a seperate characters?

A sentence is a collection of characters that has a particular character as a terminator, such as a period, an exclamation point, or a question mark. There are delimiters that indicate where words end in the collection of characters, such as spaces, commas, etc.

What you need to do is read and store the data until the end of the sentence arrives. Only then do you try to use the data. Here is some code that expects one character to define the start of the packet (sentence) and another to indicate the end of the packet (sentence).

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

If you don't care about having a separate start of packet marker, remove that code, and the started flag.

Do the parsing in the if(started && ended) block.

mantazzz:
How exactly I can see that I am sending a full sentence or a seperate characters?

Some servo test code you can look at. It captures a String from the serial monitor and parses numeric data from the String, and converts the data into a number. The code shows two ways of doing the number conversion.

// zoomkat 11-22-10 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print to serial monitor to see results
      Serial.println(servo2);
      
      //int n1; //declare as number  
      //int n2;
      
      int n1 = servo1.toInt();
      int n2 = servo2.toInt();
      
      /*char carray1[6]; //magic needed to convert string to a number 
      servo1.toCharArray(carray1, sizeof(carray1));
      n1 = atoi(carray1); 
      
      char carray2[6];
      servo2.toCharArray(carray2, sizeof(carray2));
      n2 = atoi(carray2); */
      
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
    servo1="";
    servo2="";
  } 
}

Hi guys again.
I was searching and I found a serial.parseint() function, but in her's discription says that it parses integers but with exeption of minus sign. So could someone help me to find out how to use this function, but with no exeptions for minus sign?

I don't know what you were reading, but apparently it wasn't this:

which says:

parseInt() returns the first valid (long) integer number from the current position. Initial characters that are not integers (or the minus sign) are skipped. parseInt() is terminated by the first character that is not a digit.

There is an OR in that statement, that says that - signs ARE processed.

I found this example

const int NUMBER_OF_FIELDS = 3; // how many comma-separated fields we expect
int fieldIndex = 0;             // the current field being received
int values[NUMBER_OF_FIELDS];   // array holding values for all the fields


void setup()
{
  Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud
}

void loop()
{

  if( Serial.available()) {
    for(fieldIndex = 0; fieldIndex  < 3; fieldIndex ++)
    {
      values[fieldIndex] = Serial.parseInt(); // get a numeric value

    }
    Serial.print( fieldIndex);
    Serial.println(" fields received:");
    for(int i=0; i <  fieldIndex; i++)
    {
       Serial.println(values[i]);
    }
    fieldIndex = 0;  // ready to start over
  }
}

but this code didn't parse the float numbers, for example 3,5. And I don't know how to modify this code to parse the right numbers...

Gee, it does seem strange that parseInt() didn't parse the floats correctly. Not!

I found the solution !!!

const int NUMBER_OF_FIELDS = 3; // how many comma-separated fields we expect
int fieldIndex = 0;             // the current field being received
float values[NUMBER_OF_FIELDS];   // array holding values for all the fields


void setup()
{
  Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud
}

void loop()
{

  if( Serial.available()) {
    for(fieldIndex = 0; fieldIndex  < 3; fieldIndex ++)
    {
      values[fieldIndex] = Serial.parseFloat(); // get a numeric value

    }
    Serial.print( fieldIndex);
    Serial.println(" fields received:");
    for(int i=0; i <  fieldIndex; i++)
    {
       Serial.println(values[i]);
    }
    fieldIndex = 0;  // ready to start over
  }
}