1

I'm trying to send sensor data from my Remote Control to my Bot via Bluetooth. It's 6 numbers that I would like to have read as integers on the other side. I'm printing them to Serial on the Remote side with the numbers separated by commas. I've also added a start marker "<" and an end marker ">".

The incoming data from the remote looks like this in the Bots Serial Monitor:

<1,1,1,1,1,512>
<1,1,1,1,1,512>
<1,1,1,1,1,513>
<1,1,1,1,1,512>
<1,1,1,1,1,512>
etc

Before adding in the markers I was trying to use Serial.parseInt() to do this, but I kpet getting out of order numbers (probably from no start marker?).

if (Serial1.available() > 0) {

 trig1 = Serial.parseInt();
 trig2 = Serial.parseInt();
 trig3 = Serial.parseInt();
 trig4 = Serial.parseInt();
 trig5 = Serial.parseInt();
 pot = Serial.parseInt();

if (Serial.read() == '\n') {
}
}

But now that I have the markers I don't know the best way to initialize the code. Anyone have any ideas on how to parse these 6 numbers into usable integers?

EDIT: I've wrote a code based on VE7JRO's comments.

uint16_t myArray[6] = {};
int counter = 0;


void setup(){

Serial.begin(9600);

}

void loop(){
  if (Serial.available() > 0) {
  char* pch = strtok(Serial.read,"<>,\r\n");
   }
while(pch != NULL){
  myArray[counter] = atoi(pch);
  pch = strtok(NULL, "<>,\r\n");
  counter += 1;
 }

 trig1 = myArray[0];
 trig2 = myArray[1];
 trig3 = myArray[2];
 trig4 = myArray[3];
 trig5 = myArray[4];
 pot   = myArray[5];

 Serial.print (trig1);
 Serial.print (", ");
 Serial.print (trig2);
 Serial.print (", ");
 Serial.print (trig3);
 Serial.print (", ");
 Serial.print (trig4);
 Serial.print (", ");
 Serial.print (trig5);
 Serial.print (", ");
 Serial.println(pot);  
 }  

I'm getting this error code: invalid use of non-static member function
for this line:

      char* pch = strtok(Serial.read,"<>,\r\n");

Other than that. I subbed out the test input that VE7JRO put in and inserted "Serial.read". Not sure if that is the correct way to do this. Any ideas?

I tried your second sketch. It kind of worked. The data that printed to the serial monitor looked like this:

1, 1, 1, 1, 1, 519 518, 1, 1, 1, 1, 519 1, 1, 1, 1, 19, 9 1, 1, 1, 1, 1, 519 1, 1, 1, 1, 1, 519 1, 1, 1, 1, 1, 518 1, 1, 519, 518, 1, 518 1, 1, 519, 518, 1, 518 1, 1, 518, 518, 1, 518 1, 1, 1, 1, 1, 8 1, 1, 1, 1, 1, 519 1, 1, 518, 1, 1, 1 8, 1, 518, 1, 1, 1 1, 1, 519, 1, 1, 1 1, 1, 1, 519, 1, 1 1, 1, 1, 519, 1, 1 1, 1, 1, 1, 1, 519 1, 1, 1, 1, 1, 518 1, 1, 1, 1, 519, 8 1, 1, 1, 1, 1, 518 1, 1, 1, 1, 1, 519 1, 1, 1, 1, 1, 518 1, 1, 1, 1, 1, 518 1, 1, 1, 1, 1, 520

it should have all been 1, 1, 1, 1, 1, 518(ish).

  • The parseInt has a timeout. That can slow down a sketch. You could fill a buffer with data (one byte each time) and when all the data is read then do a sscanf. For parseInt you have to skip the comma. The "<" and ">" markers is very good. Please give a minimal sketch that reads data from the serial monitor so we can test it. – Jot Apr 24 at 2:20
1

Here is a sketch that uses strtok() and atoi() to parse the data and store the results in an integer array.

char testInput[] = "<1,2,3,4,5,512>\r\n";
uint16_t myArray[6] = {};

void setup(){

  Serial.begin(9600);

  int counter = 0;
  char* pch = strtok(testInput,"<>,\r\n");

  while(pch != NULL){
    myArray[counter] = atoi(pch);
    pch = strtok(NULL, "<>,\r\n");
    counter += 1;
  }

  // Print out the contents of the integer array.
  for(int i = 0; i < counter; i++){
    Serial.println(myArray[i]);
  }

}

void loop(){}

EDIT

Based on your comments, I have another sketch for you to try.

// Sample input: <1,2,3,4,5,512>
char testInput[40];
uint16_t myArray[6] = {};
int counter = 0;

void setup(){
  Serial.begin(9600);
}

void loop(){

  if(Serial.available() > 0){

    Serial.readBytesUntil('\n', testInput, 40);

    char* pch = strtok(testInput,"<>,\r\n");

    while(pch != NULL){
      myArray[counter] = atoi(pch);
      pch = strtok(NULL, "<>,\r\n");
      counter += 1;
    }

    Serial.print (myArray[0]);
    Serial.print (", ");
    Serial.print (myArray[1]);
    Serial.print (", ");
    Serial.print (myArray[2]);
    Serial.print (", ");
    Serial.print (myArray[3]);
    Serial.print (", ");
    Serial.print (myArray[4]);
    Serial.print (", ");
    Serial.println(myArray[5]);

    counter = 0;
  }
}
  • This is very helpful! I think I understand most of it. Does this really all run in the setup? I don't want to print the array, but rather use each int as a variable in the rest of my sketch. Is there a function to use for that? Sorry I'm a bit of a beginner and just barely grasping this as a whole – PatchBOTS Apr 24 at 12:55
  • All the code is in setup() for testing purposes. The integer array contains the 6 integers and can be used as variables in the rest of your sketch. Every time you receive new data, it should be parsed using strtok(). – VE7JRO Apr 24 at 14:09
  • So would this give me the variables from the the array? trig1 = myArray[1]; trig2 = myArray[2]; trig3 = myArray[3]; trig4 = myArray[4]; trig5 = myArray[5]; pot = myArray[6]; I can't test right now otherwise I would just test myself. Excited to find the answer! – PatchBOTS Apr 24 at 14:28
  • Yes, you could do that, or just use the array directly e.g. if(myArray[0] > 1){ //do something}. – VE7JRO Apr 24 at 17:30
  • Thank you so much, this was very helpful! – PatchBOTS Apr 24 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.