Resolved. Reading & Writing datas with the two Serial port of the Due.

Hi,

For now, I send 5 datas at the same time with the serial port from Processing to Arduino Uno and I can read them very well, I can control 5 stepper motors very precisely. :)

Now, I have changed my setup. I would like to be able to read datas from Processing with the Programming Port Arduino Due (as I did with the Uno) and at the same time printing datas from Processing on the NativeUSBserial port.

But as soon as, I start my Arduino program my motors turn in a random way. When I start my Processing program my motors turn in a different random way. Whereas my fonction testStepMotors(); works perfectly. :o

I have changed nothing on my Arduino program, I have just added this, to read data from Processing

 SerialUSB.print ("A "); SerialUSB.println (w4);     
 SerialUSB.print ("B "); SerialUSB.println (w3);        
 SerialUSB.print ("C "); SerialUSB.println (w2);  
 SerialUSB.print ("D "); SerialUSB.println (w1);
 SerialUSB.print ("E "); SerialUSB.println (w0);

And it works, I can read the good datas from Processing on the Native USB serial port thanks to the serial monitor. :) I have tried to run my Arduino program with printing data on the Native USB serial port , but my motors turn in a random way.

Maybe it's a problem in my program with Serial.available >0 ???

Thank you

My code below

//***********  Variable de Processing
int w0, w1, w2, w3, w4; // vitesse à recevoir en nombre de pas /seconde

#include 

// Define a stepper and the pins it will use
#define NBMOTEURS 5

#define NBPASPARTOUR 200 // Number of step

// Define a stepper and the pins it will use
#define STEP 1//-->Mode pas complet

#define PINDIRECTION0  12
#define PINSPEED0  13

#define PINDIRECTION1  10
#define PINSPEED1  11

#define PINDIRECTION2  8
#define PINSPEED2  9

#define PINDIRECTION3  6
#define PINSPEED3  7

#define PINDIRECTION4  4
#define PINSPEED4  5

/*
const uint8_t PINDIRECTION[NBMOTEURS] =  {8, 12};
const uint8_t PINSPEED[NBMOTEURS] =  {9, 13};
*/
//int stepper[] = NBMOTEURS;
//AccelStepper stepper[i](STEP, PINSPEED0, PINDIRECTION0);  


AccelStepper stepper[ NBMOTEURS] = {
AccelStepper (STEP, PINSPEED0, PINDIRECTION0), 
AccelStepper (STEP, PINSPEED1, PINDIRECTION1), 
AccelStepper (STEP, PINSPEED2, PINDIRECTION2), 
AccelStepper (STEP, PINSPEED3, PINDIRECTION3), 
AccelStepper (STEP, PINSPEED4, PINDIRECTION4),  
};

#define ANALOG_IN A0

int analog_in;
int positionX;

void setup()
{  
     pinMode(LED_BUILTIN, OUTPUT);
    Serial.begin (115200);
    SerialUSB.begin(115200);

    
  for(uint8_t i = 0; i < NBMOTEURS; i++) {      
  stepper[i].setMaxSpeed(500);
  stepper[i].setAcceleration(150);
   }
}

void loop() { 
 //testStepMotors();
   receiveData(); // receive data from Processing
     for(uint8_t i = 0; i < NBMOTEURS; i++) {    

  stepper[i].setMaxSpeed(300);
  stepper[i].setAcceleration(150);
  stepper[i].setSpeed(250);
  

  stepper[4].moveTo(w4);
  stepper[3].moveTo(w3);
  stepper[2].moveTo(w2);
  stepper[1].moveTo(w1);
  stepper[0].moveTo(w0); // premiere donnée envoyée dansla chaine de String de Processing = virtualPositon4

  stepper[i].run(); // beaucoup plus rapide

      SerialUSB.print ("A "); SerialUSB.println (w4);     
      SerialUSB.print ("B "); SerialUSB.println (w3);        
      SerialUSB.print ("C "); SerialUSB.println (w2);  
      SerialUSB.print ("D "); SerialUSB.println (w1);
      SerialUSB.print ("E "); SerialUSB.println (w0);
    //  Serial.print ("F "); Serial.println (i-2);
  }
}
void receiveData() {

  if (Serial.available() > 0) { // Ne pas redeclarer w1

    w0 = Serial.parseInt(); // FONCTIONNE AVEC SPEED0 dans Processing et le reste commenté dans Arduino
    w1 = Serial.parseInt(); 
    w2 = Serial.parseInt();
    w3 = Serial.parseInt();
    w4 = Serial.parseInt(); // La dernière donné qui figure sur le serial de Processing arrive en premier sur Arduino

    Serial.read();
    }
  }
void testStepMotors() {
  

    for(uint8_t i = 0; i < NBMOTEURS; i++) {      
     stepper[i].setCurrentPosition(0);
SerialUSB.println (stepper[0].currentPosition());
  // Run the motor forward at 400 steps/second until the motor reaches 600 steps (3 revolutions):
  while (stepper[i].currentPosition() != 400)  {
     
    stepper[i].setMaxSpeed(300);
    stepper[i].setSpeed(200);
    stepper[i].setAcceleration(150);
    stepper[i].runSpeed();  
}
 //delay(1);
  // Reset the position to 0:
  stepper[i].setCurrentPosition(0);
  // Run the motor backwards at 600 steps/second until the motor reaches -200 steps (1 revolution):
  while (stepper[i].currentPosition() != -400)   {
    stepper[i].setSpeed(-200);
    stepper[i].setAcceleration(1);
    stepper[i].run();
  // stepper[i].runSpeedToPosition(); /NE FONCTIONNE PAS
  }
  }
}

This blog might help you out:

https://mpuprojectblog.wordpress.com/2013/07/19/arduino-dues-serialusb-and-processing/

It doesn't help because I want to receive datas from Processing with the Programming Port and with theses datas control motors with Arduino. I just want this for the moment. Do AccelStepper library works well with Arduino Due?

Does your loop() still print the correct values on SerialUSB?

On my opinion, your reading of the serial port in receiveData() is flawed. Serial communication is slow; you check if you have one or more characters to read. What happens if it is just one; you read 5 ints anyway.

I suggest that you read Robin's updated Serial Input Basics thread to get some ideas how to make it more reliable.

sterretje: Does your loop() still print the correct values on SerialUSB?

Hello sterretje,

Thank you for helping me.

Yes I can read Processing values with the serial monitor on NativeSerialUSB. To send datas to an other port, we must choose the native port in the Arduino software. That's all!

But it makes my simulation from Processing going slower. So, I have rise up the framerate to 600. But my simulation still have a lag so i will check Robien 's solutions. Nevertheless, I can read my datas almost without delay on Max/Msp.

I was asking to myself if i use a MacBook Pro 15'' quadricoeur, should it be more powerful than my MacBook Pro dualcoeur 13.3''. Should I have less lag? Both are from the same year (2015).

Because later, i will have to use an other software (Abelton live) controlled with Max for Live.

Thank for your attention.

You are aware that parseInt() is a blocking function; it might be the explanation why it's slow though it probably would also be with your original code with one port. parseInt() wil read till it either finds a non-digit character or till it times out.

Do you have an example of the data that you're sending?

Note: I have no experience with the Due.

bvking: I have changed nothing on my Arduino program, I have just added this, to read data from Processing

 SerialUSB.print ("A "); SerialUSB.println (w4);     
 SerialUSB.print ("B "); SerialUSB.println (w3);        
 SerialUSB.print ("C "); SerialUSB.println (w2);  
 SerialUSB.print ("D "); SerialUSB.println (w1);
 SerialUSB.print ("E "); SerialUSB.println (w0);

I am not familiar with the DUE but it seems to me that your code is sending data to Processing rather than reading data from Processing

...R

sterretje: You are aware that parseInt() is a blocking function; it might be the explanation why it's slow though it probably would also be with your original code with one port. parseInt() wil read till it either finds a non-digit character or till it times out.

Do you have an example of the data that you're sending?

Note: I have no experience with the Due.

I send this kind of data on the Native Serial Port.

22:23:58.872 -> D 2
22:23:58.872 -> E -8
22:23:59.877 -> A -3
22:23:59.877 -> B -1
22:23:59.877 -> C 0
22:23:59.877 -> D 1
22:23:59.877 -> E -12
22:24:00.865 -> A -4
22:24:00.865 -> B -2
22:24:00.865 -> C -1
22:24:00.865 -> D 0
22:24:00.865 -> E -16

I read this kind of data from Processing -217*-137*-57*22*-222* -217*-137*-57*22*-222*

Here there is no relation between datas A, B, C, D, E and the first, second...fifth data from Processing, but there is in real.

Robin2: I am not familiar with the DUE but it seems to me that your code is sending data to Processing rather than reading data from Processing

...R

The code you quoted send or print data on the NativeSerialUSBport of the Arduino Due. Actually I add the letter A with a space ("A ") to the data from Processing (w4). The same with other letter and values from Processing. SerialUSB.print ("A "); SerialUSB.println (w4);..... SerialUSB.print ("E "); SerialUSB.println (w0);

I'm reading value from the Programming Port of the Arduino Due (value from Processing) and I print them with a letter to the NativeSerialUSBport of the Arduino Due.

It works. But I think the way I read data from Processing is not good because it makes my simulation in Processing going slower.

I put the program of the exemple 5 of Serial Input Basics from Robin, but I can't see any value in the monitor of the Arduino IDE.

//***********  Variable de Processing
int w0, w1, w2, w3, w4; // vitesse à recevoir en nombre de pas /seconde

// Consignes transformées de Processing à Arduino.
int processingPosition, processingPosition1;
//float consigneA, consigneB, consigneC, consigneD, consigneE;


#include 

// Define a stepper and the pins it will use
#define NBMOTEURS 5

#define NBPASPARTOUR 200

// Define a stepper and the pins it will use
#define STEP 1//-->Mode pas complet

#define PINDIRECTION0  12
#define PINSPEED0  13

#define PINDIRECTION1  10
#define PINSPEED1  11

#define PINDIRECTION2  8
#define PINSPEED2  9

#define PINDIRECTION3  6
#define PINSPEED3  7

#define PINDIRECTION4  4
#define PINSPEED4  5

/*
const uint8_t PINDIRECTION[NBMOTEURS] =  {8, 12};
const uint8_t PINSPEED[NBMOTEURS] =  {9, 13};
*/
//int stepper[] = NBMOTEURS;
//AccelStepper stepper[i](STEP, PINSPEED0, PINDIRECTION0);  


AccelStepper stepper[ NBMOTEURS] = {
AccelStepper (STEP, PINSPEED0, PINDIRECTION0), 
AccelStepper (STEP, PINSPEED1, PINDIRECTION1), 
AccelStepper (STEP, PINSPEED2, PINDIRECTION2), 
AccelStepper (STEP, PINSPEED3, PINDIRECTION3), 
AccelStepper (STEP, PINSPEED4, PINDIRECTION4),  
};

#define ANALOG_IN A0

int analog_in;
int positionX;

// Example 5 - Receive with start- and end-markers combined with parsing

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

boolean newData = false;

//============



void setup()
{  
   //     pinMode(LED_BUILTIN, OUTPUT);
    Serial.begin (115200);
    SerialUSB.begin (9600); // it doesn't make any sens but i prefer

      SerialUSB.print ("A "); SerialUSB.println (-4);     
      SerialUSB.print ("B "); SerialUSB.println (-3);        
      SerialUSB.print ("C "); SerialUSB.println (-2);  
      SerialUSB.print ("D "); SerialUSB.println (-1);
      SerialUSB.print ("E "); SerialUSB.println (-10);

      Serial.print ("A "); Serial.println (4);     
      Serial.print ("B "); Serial.println (3);        
      Serial.print ("C "); Serial.println (2);  
      Serial.print ("D "); Serial.println (1);
      Serial.print ("E "); Serial.println (10);

    
  for(uint8_t i = 0; i < NBMOTEURS; i++) {      
  stepper[i].setMaxSpeed(500);
  stepper[i].setAcceleration(150);
   }
}

void loop() { 

   recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        newData = false;
    }
   
 // receiveData(); // receive data from Processing


 /*

     for(uint8_t i = 0; i < NBMOTEURS; i++) {    

  stepper[i].setMaxSpeed(300);
  stepper[i].setAcceleration(150);
  stepper[i].setSpeed(250);
  

  stepper[4].moveTo(w4);
  stepper[3].moveTo(w3);
  stepper[2].moveTo(w2);
  stepper[1].moveTo(w1);
  stepper[0].moveTo(w0); // premiere donnée envoyée dansla chaine de String de Processing = virtualPositon4

  stepper[i].setSpeed(100);
  stepper[i].setAcceleration(10);
  stepper[i].runSpeedToPosition();
 //  stepper[i].run(); // turn without stop

      SerialUSB.print ("A "); SerialUSB.println (w4);     
      SerialUSB.print ("B "); SerialUSB.println (w3);        
      SerialUSB.print ("C "); SerialUSB.println (w2);  
      SerialUSB.print ("D "); SerialUSB.println (w1);
      SerialUSB.print ("E "); SerialUSB.println (w0);
    //  Serial.print ("F "); Serial.println (i-2);
    

  } 

 */
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

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() {
    SerialUSB.print("Message ");
  SerialUSB.println(messageFromPC);
   SerialUSB.print("Integer ");
  SerialUSB.print(integerFromPC);
    SerialUSB.print("Float ");
    SerialUSB.println(floatFromPC);
}
void receiveData() {

  if (Serial.available() > 0) { // Ne pas redeclarer w1

    w0 = Serial.parseInt(); // FONCTIONNE AVEC SPEED0 dans Processing et le reste commenté dans Arduino
    w1 = Serial.parseInt(); 
    w2 = Serial.parseInt();
    w3 = Serial.parseInt();
    w4 = Serial.parseInt(); // La dernière donné qui figure sur le serial de Processing arrive en premier sur Arduino

    Serial.read();
    }
  }

}

bvking: The code you quoted send or print data on the NativeSerialUSBport of the Arduino Due.

That's what I thought. But in your Original Post you said "to read data from Processing" which had me confused.

...R

Robin2: That's what I thought. But in your Original Post you said "to read data from Processing" which had me confused.

...R

Ooops, sorry.

Actually, I have just tested to send datas from Processing and reading them with Arduino and the simple fact to write datas from Processing get my simulation slower. There is lags whereas if I don't write datas, there is no lags at all.

I'm quite sure my manner of reading datas with Arduino is not good.

I just want to readt 5 ints separately...

Any ideas are welcome ;)

bvking: I'm quite sure my manner of reading datas with Arduino is not good.

I just want to readt 5 ints separately...

Is the program in Reply #8 the latest version? If not please post the latest version.

What exactly is Processing sending? Can you add some code to your Processing program to display the data exactly in the format that it is sent and then post some examples of that here.

Also, you have not modified the function parseData() to deal with 5 integers. At the moment it is expecting a cstring, an int and a float.

...R

Hello Robin,

I made a lightened program to show what is send with Processing

import java.util.Arrays;
import sync.*;
// MANAGE ARDUINO
import processing.serial.*;
Serial arduinoport;


void setup() {
  
    frameRate(80); 
   //perspective setting
  size(1000, 800, P3D);
  
  //********to send value to Arduino
  String[] ports = Serial.list();
 // printArray(ports);
   printArray(Serial.list());
  arduinoport = new Serial(this,ports[6],115200);//Carefull: choose the port matching to the serial list
    
}

void draw() {
 arduinoPos ( );
}

void arduinoPos () {
   
    for (int i = 0; i < 5; i++) {

    String pos = (-232) +"*"+(4232) +"*"+(1111)+"*"+(56232)+"*"+(-222)+"*"; 
    
 // String pos = (int (i*2))+"*"+ (int (10))+"*"+ (int (-i))+"*"+ (int (12))+"*"+ (int (314))+"*";//
       
       print ("  pos   ");println (pos);
       
       arduinoport.write(pos); // Send data to Arduino.      

  }  
}

Here the 5 ints are separated with * but it could be something else.

Thank you

bvking:
Here the 5 ints are separated with * but it could be something else.

It seems as if your Processing program is sending

“-2324232111156232-222*”

but what I don’t know is whether the code arduinoport.write(pos); adds a linefeed or carriage return.

Also (and at least as important) you need to post the Arduino program that is expected to receive and interpret that.

…R

PS … I strongly recommend that you get your Processing program to add start- and end-markers so it is like this
“<-2324232111156232-222*>”

Robin2: It seems as if your Processing program is sending

"-232*4232*1111*56232*-222*"

PS ... I strongly recommend that you get your Processing program to add start- and end-markers so it is like this "<-232*4232*1111*56232*-222*>"

In the console of Processing datas are like that, so it is a natural carriage return?

-232*4232*1111*56232*-222*
-232*4232*1111*56232*-222*
-232*4232*1111*56232*-222*
-232*4232*1111*56232*-222*
-232*4232*1111*56232*-222*
-232*4232*1111*56232*-222*

So, according to you, I should rewrite in Processing my string like this, isn't it?: String pos ="<"+ (-232) +""+(4232) +""+(1111)+""+(56232)+""+(-222)+"*"+">";

My entire Arduino code below without the add of your exemple 5 of Serial Input Basics. Because you told me: Also, you have not modified the function parseData() to deal with 5 integers. At the moment it is expecting a cstring, an int and a float. And I don't know how to receive 5 ints.

//***********  Variable de Processing
int w0, w1, w2, w3, w4; // vitesse à recevoir en nombre de pas /seconde

#include 

// Define a stepper and the pins it will use
#define NBMOTEURS 5

#define NBPASPARTOUR 200

// Define a stepper and the pins it will use
#define STEP 1//-->Mode pas complet

#define PINDIRECTION0  12
#define PINSPEED0  13

#define PINDIRECTION1  10
#define PINSPEED1  11

#define PINDIRECTION2  8
#define PINSPEED2  9

#define PINDIRECTION3  6
#define PINSPEED3  7

#define PINDIRECTION4  4
#define PINSPEED4  5

/*
const uint8_t PINDIRECTION[NBMOTEURS] =  {8, 12};
const uint8_t PINSPEED[NBMOTEURS] =  {9, 13};
*/
//int stepper[] = NBMOTEURS;
//AccelStepper stepper[i](STEP, PINSPEED0, PINDIRECTION0);  


AccelStepper stepper[ NBMOTEURS] = {
AccelStepper (STEP, PINSPEED0, PINDIRECTION0), 
AccelStepper (STEP, PINSPEED1, PINDIRECTION1), 
AccelStepper (STEP, PINSPEED2, PINDIRECTION2), 
AccelStepper (STEP, PINSPEED3, PINDIRECTION3), 
AccelStepper (STEP, PINSPEED4, PINDIRECTION4),  
};

#define ANALOG_IN A0

int analog_in;
int positionX;

void setup()
{  
   //     pinMode(LED_BUILTIN, OUTPUT);
    Serial.begin (115200);
    SerialUSB.begin (9600); // it doesn't make any sens but i prefer

      SerialUSB.print ("A "); SerialUSB.println (-4);     
      SerialUSB.print ("B "); SerialUSB.println (-3);        
      SerialUSB.print ("C "); SerialUSB.println (-2);  
      SerialUSB.print ("D "); SerialUSB.println (-1);
      SerialUSB.print ("E "); SerialUSB.println (-10);

      Serial.print ("A "); Serial.println (4);     
      Serial.print ("B "); Serial.println (3);        
      Serial.print ("C "); Serial.println (2);  
      Serial.print ("D "); Serial.println (1);
      Serial.print ("E "); Serial.println (10);

    
  for(uint8_t i = 0; i < NBMOTEURS; i++) {      
  stepper[i].setMaxSpeed(500);
  stepper[i].setAcceleration(150);
   }
}

void loop() { 
   
 //testStepMotors();
 //testPotarMotor(); // work perfectly
  receiveData(); // receive data from Processing
//SendDataToMax ();



     for(uint8_t i = 0; i < NBMOTEURS; i++) {    

  stepper[i].setMaxSpeed(300);
  stepper[i].setAcceleration(150);
  stepper[i].setSpeed(250);
  

  stepper[4].moveTo(w4);
  stepper[3].moveTo(w3);
  stepper[2].moveTo(w2);
  stepper[1].moveTo(w1);
  stepper[0].moveTo(w0); // premiere donnée envoyée dansla chaine de String de Processing = virtualPositon4

  stepper[i].setSpeed(100);
  stepper[i].setAcceleration(10);
  stepper[i].runSpeedToPosition();
 //  stepper[i].run(); // turn without stop
 /*
      SerialUSB.print ("A "); SerialUSB.println (w4);     
      SerialUSB.print ("B "); SerialUSB.println (w3);        
      SerialUSB.print ("C "); SerialUSB.println (w2);  
      SerialUSB.print ("D "); SerialUSB.println (w1);
      SerialUSB.print ("E "); SerialUSB.println (w0);
    //  Serial.print ("F "); Serial.println (i-2);
  */  

  }  
}
void receiveData() {

  if (Serial.available() > 0) { 

    w0 = Serial.parseInt(); 
    w1 = Serial.parseInt(); 
    w2 = Serial.parseInt();
    w3 = Serial.parseInt();
    w4 = Serial.parseInt(); // La dernière donné qui figure sur le serial de Processing arrive en premier sur Arduino

    Serial.read();
    }
  }
void testStepMotors() {
  
    for(uint8_t i = 0; i < NBMOTEURS; i++) {      
     stepper[i].setCurrentPosition(0);

  // Run the motor forward at 400 steps/second until the motor reaches 600 steps (3 revolutions):
  while (stepper[i].currentPosition() != 400)  {
      stepper[i].setMaxSpeed(300);

    stepper[i].setSpeed(200);
       stepper[i].setAcceleration(150);
    stepper[i].runSpeed();
}
  
 //delay(1);
  // Reset the position to 0:
  stepper[i].setCurrentPosition(0);
  // Run the motor backwards at 600 steps/second until the motor reaches -200 steps (1 revolution):
  while (stepper[i].currentPosition() != -400)   {
    stepper[i].setSpeed(-200);
        stepper[i].setAcceleration(1);
    stepper[i].run();
  // stepper[i].runSpeedToPosition(); /NE FONCTIONNE PAS
  }
  }
   Serial.print (stepper[0].currentPosition());
}

void testPotarMotor ()  {

   analog_in = analogRead(ANALOG_IN);
   positionX= map ( analog_in, 0, 1023, -200, 200); // 5V

    for(uint8_t i = 0; i < NBMOTEURS; i++) {      
  stepper[i].setMaxSpeed(100);
  stepper[i].setSpeed(100);
  stepper[i].setAcceleration(10);
  stepper[i].moveTo(positionX);
  stepper[i].run();
 // stepper[i].runSpeed();
//  stepper[i].runSpeedToPosition();

  Serial.println (positionX);
  }  
}
void SendDataToMax () {

// Arduino--Max--Processing  

    for(uint8_t i = 0; i < NBMOTEURS; i++) { 

   // WORK VERY WEELL. If i read nothing on the serial port 

   
      Serial.print ("A "); Serial.println (i);     
      Serial.print ("B "); Serial.println (-i+2);        
      Serial.print ("C "); Serial.println (i+3);  
      Serial.print ("D "); Serial.println (i+4);
      Serial.print ("E "); Serial.println (i*4);
      Serial.println (i-2);

      SerialUSB.print ("A "); SerialUSB.println (-i);     
      SerialUSB.print ("B "); SerialUSB.println (-i+2);        
      SerialUSB.print ("C "); SerialUSB.println (-i+3);  
      SerialUSB.print ("D "); SerialUSB.println (-i+4);
      SerialUSB.print ("E "); SerialUSB.println (-i*4);
      SerialUSB.println (i-2);

       delay (1000);
/* Work perfectly too

 w4=i; w3=i+2; w2=i+3; w1=i+4;  w0=i+5;
      Serial.print ("A "); Serial.println (w0);     
      Serial.print ("B "); Serial.println (-w1);        
      Serial.print ("C "); Serial.println (100);  
      Serial.print ("D "); Serial.println (w3);
      Serial.print ("E "); Serial.println (w4);
     Serial.println (i-2);
     delay (1000);
  */

  /* 
    if (Serial.available() > 0) { 

    w0 = Serial.parseInt(); // 
    w1 = Serial.parseInt(); 
    w2 = Serial.parseInt();
    w3 = Serial.parseInt();
    w4 = Serial.parseInt(); // La dernière donné qui figure sur le serial de Processing arrive en premier sur Arduino

    Serial.read();    
       
 //  for(uint8_t i = 0; i < NBMOTEURS; i++) { 
   
      Serial.print ("A "); Serial.println (w0);     
      Serial.print ("B "); Serial.println (w1);        
      Serial.print ("C "); Serial.println (w2);  
      Serial.print ("D "); Serial.println (w3);
      Serial.print ("E "); Serial.println (w4);
      Serial.print ("F "); Serial.println (i-2);

//     int i = random (1, 5);
 //      Serial.println (i);
  delay(0);       
          }
          */
   }
}

bvking: My entire Arduino code below without the add of your exemple 5 of Serial Input Basics.

That's not very helpful. Your program seems to have gone back in time.

To receive 5 ints you need to remove the code for the cstring and the float and repeat the code for the int 4 times - with suitable changes to the variable names where the data is to go.

Just note that the first item needs to be like this

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    integerFromPC = atoi(strtokIndx);     // convert this part to an integer

...R

Hi,

I manage to receive my datas from Processing but its are printed as a message, not in ints.

29*42*54*66*-222* 29*41*54*66*-222*

It's due to the way I send datas from Processing, like that

String positions ="<"+ VirtualPosition4 +""+VirtualPosition3 +""+VirtualPosition2+""+VirtualPosition1+""+(-222)+"*"+ ">"; if(frameCount%2 == 0) println(frameCount + ": " + (positions)); arduinoport.write(positions); // Send data to Arduino.

In this manner, I can't recover my 5 ints separately.

So I think to do like that in Processing:

arduinoport.write(int VirtualPosition4);arduinoport.write(int VirtualPosition3);....;arduinoport.write(int VirtualPosition0);

Am I wright?

bvking: I manage to receive my datas from Processing but its are printed as a message, not in ints.

29*42*54*66*-222*

If your Arduino program is producing that output and if it is what Processing sends then the Processing code is correct.

You need to post the Arduino program if I am to help with what might be wrong or missing in it.

...R

bvking: In this manner, I can't recover my 5 ints separately.

At the Arduino side?

Everything that you needed was already given; a link to Robin's tutorial as well as a strtok example in there.

Robin2: If your Arduino program is producing that output and if it is what Processing sends then the Processing code is correct.

You need to post the Arduino program if I am to help with what might be wrong or missing in it.

...R

Hello Robin, Program is ok, because I receive good data

13:08:25.244 -> 977*977*977*-977*488
13:08:25.294 -> 978*978*978*-978*489
13:08:25.294 -> 979*979*979*-979*489

But now, I would like to have into different (int) variable each of those 5 datas

I don't know the way to split int datas from one single message

void loop() { 

   recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        newData = false;
    }
   
 //testStepMotors();
 //testPotarMotor(); // work perfectly
 // receiveData(); // receive data from Processing
//SendDataToMax ();


}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

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() {
 // SerialUSB.print("Message ");
  SerialUSB.println(messageFromPC);
/*  
  SerialUSB.print("Integer ");
  SerialUSB.print(integerFromPC);
  SerialUSB.print("Float ");
  SerialUSB.println(floatFromPC);
  */
}