Go Down

Topic: Solved:time lag as encoders control the mouse via Arduino serial communication (Read 3644 times) previous topic - next topic

Robin2

Glad to hear it is working.

I wonder should you use both IF and WHILE like this

Code: [Select]
if(Serial1.available()>5) {
  while(Serial1.available > 0) {


It does nothing if there are five or fewer characters, but then it reads all that there is.


You might want to look at the examples in serial input basics which read from the serial buffer in the background until all the data has been received.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

33chen

sure. I am looking at the link now and it is very comprehensive. Great work, Robin2.

33chen

Robin2,

Again the link is quite informative. a question to your exemplary code in Serialinput link. How should I operate the serial monitor to see the parsed message of "This is a test, 1234, 45.3". When I upload the code and then open the serial monitor, there is no any text coming out. Should I type This is a test, 1234, 45.3, and then hit Send.
Thanks a lot.
Code: [Select]
// simple parse demo
char receivedChars[] = "This is a test, 1234, 45.3" ;

char messageFromPC[32] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

char recvChar;
char endMarker = '>';
boolean newData = false;


void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");

parseData();
showParsedData();
}


void loop() {

}


void parseData() {

    // split the data into its parts
   
  char * strtokIndx; // this is used by strtok() as an index
 
  strtokIndx = strtok(receivedChars,",");      // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
 
  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  integerFromPC = atoi(strtokIndx);     // convert this part to an integer
 
  strtokIndx = strtok(NULL, ",");
  floatFromPC = atof(strtokIndx);     // convert this part to a float

}


void showParsedData() {
Serial.print("Message ");
Serial.println(messageFromPC);
Serial.print("Integer ");
Serial.println(integerFromPC);
Serial.print("Float ");
Serial.println(floatFromPC);
}

Robin2

You don't need to enter any data in the Serial Monitor. Try pressing the reset button on the Arduino (assuming it is NOT a Leonardo) while the Serial Monitor is open.

The code only runs once when the Arduino restarts.

The parse example simulates the situation where you had used one of the other examples to put data into receivedChars[]

Do let me know if you can't get it to work

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

33chen

I See. Thanks. Sorry, unfortunately, I am trying with my Leonardo board. It didn't work that way somehow.

Robin2

I See. Thanks. Sorry, unfortunately, I am trying with my Leonardo board. It didn't work that way somehow.
Change the code to this and it will print out every two seconds (I hope).

Code: [Select]
// simple parse demo
char receivedChars[] = "This is a test, 1234, 45.3" ;

char messageFromPC[32] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

char recvChar;
char endMarker = '>';
boolean newData = false;


void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");


}


void loop() {

        parseData();
showParsedData();
        delay(2000);

}


void parseData() {

    // split the data into its parts
    
  char * strtokIndx; // this is used by strtok() as an index
  
  strtokIndx = strtok(receivedChars,",");      // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
  
  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  integerFromPC = atoi(strtokIndx);     // convert this part to an integer
  
  strtokIndx = strtok(NULL, ",");
  floatFromPC = atof(strtokIndx);     // convert this part to a float

}


void showParsedData() {
Serial.print("Message ");
Serial.println(messageFromPC);
Serial.print("Integer ");
Serial.println(integerFromPC);
Serial.print("Float ");
Serial.println(floatFromPC);
}


Hopefully also another example of the convenience of using functions.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

33chen

It works. I can see the output from Serial monitor. Just curious: my results look like this. Is there something else I should change as well? Thanks.
Code: [Select]

Message This is a test
Integer 0
Float 0.00
Message This is a test
Integer 0
Float 0.00
Message This is a test
Integer 0
Float 0.00

Robin2

Thanks for pointing that out. I learn new stuff every day. It happens that the strtok() function alters the array receivedChars[] by replacing the commas with \0.

That means that it only works properly the first time. And, because I was just testing it in setup() I had not been aware of that.

On your Leonardo you may not have seen the first printout.

This version should work repeatedly.

Code: [Select]
// simple parse demo
char receivedChars[] = "This is a test, 1234, 45.3" ;
char tempChars[32];        // temporary array for use by strtok() function

char messageFromPC[32] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

char recvChar;
char endMarker = '>';
boolean newData = false;
byte dLen = 0;


void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}


void loop() {

        // this is necessary because strtok() alters the array
        //   in this case replacing commas with \0
    strcpy(tempChars, receivedChars);
    parseData();
    showParsedData();
    delay(2000);
}

    
void parseData() {

    // split the data into its parts
    
  char * strtokIndx; // this is used by strtok() as an index

  strtokIndx = strtok(tempChars,",");      // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
  
  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  integerFromPC = atoi(strtokIndx);     // convert this part to an integer

  strtokIndx = strtok(NULL, ",");
  floatFromPC = atof(strtokIndx);     // convert this part to a float

}


void showParsedData() {
    Serial.print("Message ");
    Serial.println(messageFromPC);
    Serial.print("Integer ");
    Serial.println(integerFromPC);
    Serial.print("Float ");
    Serial.println(floatFromPC);
}

I will add a note to my serial input basics Thread.

Thanks again.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

33chen


Go Up