Problem by receiving datas in Arduino? Or problem by sending datas to Arduino?

Continuing the discussion from RE: My program getting slower when I write datas on Serial Port:

Hello all,

I send 5 datas from Processing to Arduino and I re write those datas with Arduino into an other serial port thanks to the Arduino Due.

I can read my datas on the monitor of the Arduino Due, but datas are printed too quickly. I think I have to create a fonction in Arduino to print them less quickly but it is not my main problem for the moment.

Actually I can control from with my Arduino program, 5 stepper motors very correctly with two tests void testStepMotors() and void testPotarMotor () . My motors move to theirs positions as fast as I have set them. But when I receive theirs positions from Processing, motors go slower than they should. I think there is a problem with the Processing code because in the function testPotarMotor, motors move to theirs positions at good speed. I put my entire Arduino code after the Processing one

I’m quite sure I don’t send datas in good manner from Processing because when I stop the Processing program, it naturally send 0,0,0,0,0 and my motor move to position 0 very quickly (as set in Arduino).

import java.util.Arrays;

// MANAGE ARDUINO
import processing.serial.*;
Serial arduinoport;

int i;


void setup() {
  
    frameRate(60); 
   //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[7],115200);//Carefull: chosse the port matching to the serial list
    
}

void draw() {
  
  // if(frameCount%0 == 0) // do the function one frame each 2 frame.
   arduinoPos ( );
}

void arduinoPos () {
  
       String positions = (int (300))+"*"+ (int (600))+"*"+ (int (-300))+"*"+ (int (300))+"*"+ (int (-600))+"*";//
  
       println(frameCount + ": " + (positions)); 
       
       arduinoport.write(positions); // Send data to Arduino.
} 

The Arduino code


//***********  Variable de Processing
long w0, w1, w2, w3, w4; // 5 different positions to receive in step.
boolean dataReady = false;

#include <AccelStepper.h>

// 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);
  
  stepper[i].setSpeed(100);
//  stepper[i].runSpeedToPosition();
   }
}

void loop() { 
   
 //testStepMotors();
// testPotarMotor(); // work perfectly
 receiveData(); // receive data from Processing
 // SerialUSB.println ("loopcorrigé"); // Mostly for debugging to see of receivData() was blocking code.
//SendDataToMax ();
 
}
void receiveData() {

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

    w0 = Serial.parseInt(); // FONCTIONNE AVEC VIRTUALPOSITION0 dans Processing 
    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

    dataReady = true;
    }

  if (dataReady)
  
    {  stepper[4].moveTo(w4);
       stepper[4].run();
       stepper[3].moveTo(w3);      
       stepper[3].run();
       stepper[2].moveTo(w2);        
       stepper[2].run();
       stepper[1].moveTo(w1);
       stepper[1].run();
       stepper[0].moveTo(w0); // premiere donnée envoyée dansla chaine de String de Processing = virtualPositon4
       stepper[0].run();
      
      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); 
    dataReady == false;
    }
      
  }

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

  // Run the motor forward at 300 steps/second until the motor reaches 600 steps (3 revolutions):
  while (stepper[i].currentPosition() != 600)  {
      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(); /don't work
  }
  }
   Serial.print (stepper[0].currentPosition());
}

void testPotarMotor ()  {

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

  stepper[4].moveTo(positionX);
  stepper[4].run(); // work perfectly
  
  stepper[3].moveTo(positionX);
  stepper[3].run(); // work perfectly
   
  stepper[2].moveTo(positionX);
  stepper[2].run(); // work perfectly

  stepper[1].moveTo(positionX);
  stepper[1].run(); // work perfectly
  
  stepper[0].moveTo(positionX);
  stepper[0].run(); // work perfectly

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

// Order to plug software Arduino--Max--Processing  

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

   
      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);

   }
}
 
 
1 Like

Hello,

You are sending one long stream of data with no start or end (print() was used to show this):

Consider adding a “terminator” to the end of the data sent.

On the Arduino side consider:

The Arduino flags this:

Keep in mind that you are responsible for every line of your code even when you use code examples.

That function is not used but if it is called there will be a
NBMOTEURS*1000ms delay
each time it is called.

:)

1 Like

Hello glv,

In Arduino, I changed dataReady == false; to dataReady = false;

And I have tried 3 different manner to write datas from Processing, and strangely, there is no changes???.

Motors go to good positions but without the good speed.

void arduinoPos () {  
  delay (10);
 // loop();
    i++;
   int j=(i)/2;
   
 //   String positions0 = (int (i))+"*"+ (int (i))+"*"+ (int (j))+"*"+ (int (-i))+"*"+ (int (j))+"*";//
 //   String positions0 = "<"+(int (i))+"*"+ (int (i))+"*"+ (int (i))+"*"+ (int (-i))+"*"+ (int (j))+"*"+">";//
      String positions0 = "<"+(int (i))+"*"+ (int (i))+"*"+ (int (i))+"*"+ (int (-i))+"*"+ (int (j))+">";//
//   String pos ="<"+ (-232) +"*"+(4232) +"*"+(1111)+"*"+(56232)+"*"+(-222)+"*"+">";          
      if(frameCount%2 == 0)  { 
       
       println(frameCount + ": " +i+ " positions : "+ (positions0) ); 
       
        } 
       arduinoport.write(positions0); // Send data to Arduino.
    } 

Here my Arduino code with two manner to receive datas
First and second works.
The new one with markers looks better but now, I don’t know the way to put my 5 datas in five separate ints ??


//***********  Variable de Processing
int w0, w1, w2, w3, w4; // position in step
boolean dataReady = false;

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


#include <AccelStepper.h>

// 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;
    }
   
 //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);
  */
}
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

    dataReady == true;
    }

  if (dataReady)
  
    {  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[4].run();
       stepper[3].run();
       stepper[2].run();
       stepper[1].run();
       stepper[0].run();
      
      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); 
    dataReady = false;
    }
      
  }
  
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. 

   
      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);
       

   }
}
 
 

Thanks again and again…

1 Like