Hello,
I' m reading 10 ints in Arduino from Processing.
I have only 7 datas matching as below. Last one should be -7, -7, -7.
I can read this with the other Native serial port with Arduino Due.
12:00:49.940 -> A 1330
12:00:49.940 -> B 1351
12:00:49.940 -> C 1371
12:00:49.940 -> D 1392
12:00:49.940 -> E 1413
12:00:49.940 -> F -7
12:00:49.940 -> G -7
12:00:49.940 -> H 0
12:00:49.940 -> I 0
12:00:49.940 -> J 0
But sometimes 10 datas match...
I send this from Processing (it's an example, not what I have actually send with the data above)
<3155,3370,3586,3801,4017,-16,-17,-18,-19,-20>
I put a video on youtube to be more explicit.
My Arduino Program
//*********** Managing Variable coming from Processing
long w0, w1, w2, w3, w4; // position in step
boolean dataReady = false;
int i; // test speed of motor
int j;
// 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}; //not used for the moment
int integerFromPC0 = 0;
int integerFromPC1 = 0;
int integerFromPC2 = 0;
int integerFromPC3 = 0;
int integerFromPC4 = 0;
int integerFromPC5 = 0;
int integerFromPC6 = 0;
int integerFromPC7 = 0;
int integerFromPC8 = 0;
int integerFromPC9 = 0;
float floatFromPC0 = 0.0; // not used for the moment
float floatFromPC1 = 0.0; // not used for the moment
float floatFromPC2 = 0.0; // not used for the momen
float floatFromPC3 = 0.0; // not used for the moment
float floatFromPC4 = 0.0; // not used for the momen
boolean newData = false; // In order to not copy any datas from the serial if there is no datas coming.
//============
void setup()
{
// Does change baud of serial begin change precisio of the movement or NO!!!!!!
Serial.begin (250000); //Port to receive datas: It's a serial port called the Programming port, the program of Arduino is transfered with. And in which go datas from Processing
SerialUSB.begin (250000); //Port to send datas.It's a NativeUSB port in the Arduino Due just to transfer datas from Arduino to an other software.
// I read datas in the Arduino monitor with this Native serial port. In this one go each of 5 datas,
// from Processing named with a letter. example 10, 12, 14,16, 18--> A 10, B 12, C 14, D 16, E 18
// Thoses datas indexed will go in Max Msp.
// Select the good port to see datas in the monitor
Serial.print ("To see datas from Processing, don't forget to select the Native Port, you are on the programming port");
// Set MaxSpeed, Acceleration and speed of each motors
for(uint8_t i = 0; i < NBMOTEURS; i++) {
stepper[i].setMaxSpeed(600);
stepper[i].setAcceleration(150); // set number of step * seconde -2. 250 bof. 300 trop? 200 OK (sauf 1)
stepper[i].setSpeed(400); // set number of step * second -1. 500 OK. 400
}
}
void loop() {
// testStepMotors();
recvWithStartEndMarkers(); // Receive datas from Processing.
// recvWithStartEndMarkersBis();
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();
controlPositionMotor();
sendDataToMaxNativePort();
sendDataToMaxProgrammingPort();
newData = false;
}
// sendCounterToMaxProgrammingPort();
//testPotarMotor(); // work perfectly
//TestSpeedSendDataToMax (); //work perfectly
// Serial.print("X"); // Richard solution
}
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
// POSITIONS // REVOLUTIONS
integerFromPC0 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC1 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC2 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC3 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC4 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC5 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC6 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC7 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC8 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC9 = atoi(strtokIndx); // convert this part to an integer
}
//============
void controlPositionMotor () {
stepper[4].moveTo(integerFromPC4);
stepper[4].run();
stepper[3].moveTo(integerFromPC3);
stepper[3].run();
stepper[2].moveTo(integerFromPC2);
stepper[2].run();
stepper[1].moveTo(integerFromPC1);
stepper[1].run();
stepper[0].moveTo(integerFromPC0); // First data from Processing reachs here.
stepper[0].run();
}
void sendDataToMaxNativePort() { // Arduino --> Max --> Processing
SerialUSB.print ("A "); SerialUSB.println (integerFromPC0);
SerialUSB.print ("B "); SerialUSB.println (integerFromPC1);
SerialUSB.print ("C "); SerialUSB.println (integerFromPC2);
SerialUSB.print ("D "); SerialUSB.println (integerFromPC3);
SerialUSB.print ("E "); SerialUSB.println (integerFromPC4);
SerialUSB.print ("F "); SerialUSB.println (integerFromPC5);
SerialUSB.print ("G "); SerialUSB.println (integerFromPC6);
SerialUSB.print ("H "); SerialUSB.println (integerFromPC7);
SerialUSB.print ("I "); SerialUSB.println (integerFromPC8);
SerialUSB.print ("J "); SerialUSB.println (integerFromPC9);
}
void sendDataToMaxProgrammingPort() { // Arduino --> Max --> Processing
Serial.print ("A "); Serial.println (integerFromPC0);
Serial.print ("B "); Serial.println (integerFromPC1);
Serial.print ("C "); Serial.println (integerFromPC2);
Serial.print ("D "); Serial.println (integerFromPC3);
Serial.print ("E "); Serial.println (integerFromPC4);
Serial.print ("F "); Serial.println (integerFromPC5);
Serial.print ("G "); Serial.println (integerFromPC6);
Serial.print ("H "); Serial.println (integerFromPC7);
Serial.print ("I "); Serial.println (integerFromPC8);
Serial.print ("J "); Serial.println (integerFromPC9);
}
void sendCounterToMaxProgrammingPort() { // Arduino --> Max --> Processing
Serial.print ("F "); Serial.println (integerFromPC5);
Serial.print ("G "); Serial.println (integerFromPC6);
Serial.print ("H "); Serial.println (integerFromPC7);
Serial.print ("I "); Serial.println (integerFromPC8);
Serial.print ("J "); Serial.println (integerFromPC9);
}