here is the entire code: note that in Matlab I transformed the matrix into a string and saved it into a .txt file: the string is like this one:
2 3 4;5 6 8; 12923 3475 -2] for example
(I used ; char to understand when to fill the following row of the matrix and ] as a string terminator)
I have six motors representing the joints of my robotic arm and I use accelstepper and multi stepper libraries to have a simultaneous movement of all the motors.
String stringa= ""; //create a string to store what comes from serial
char c; //create a variable char to store each character from serial
int B [298][6]; //matrix that must be filled with data read from serial
int count=0; //counter that increased if a ';' is found
int conta4=0; //counter to store the length of the string
int conta5=0; //counter that increases if a value is added to B
int conta2=0; //counter that increases if a numeric value is found on the string
int conta3=0; //counter that increases if a value is added to B
//(it is set to 0 if a row of B has been entirely filled)
int num; //variable that stores the value of x(char array) into integer
bool s=false; //to repeat the loop only once
char q=';';
char w=' ';
char y=']';
char x[9];//variable char to store values until ';' ' ' or ']' will be found
#include <AccelStepper.h>
#include <MultiStepper.h>
// Define some steppers and the pins the will use (first pin for the number of wires,
// second one for the number of steps,
// and third one for direction)
AccelStepper stepper1(1,9,8);
AccelStepper stepper2(1,2,3);
AccelStepper stepper3(1,10,11);
AccelStepper stepper4(1,22,23);
AccelStepper stepper5(1,24,25);
AccelStepper stepper6(1,26,27);
MultiStepper steppers;
void setup() {
Serial.begin(4800);
stepper1.setMaxSpeed(500.0);
Serial.print("stepper1 in posizione:");
Serial.println(stepper1.currentPosition());
stepper2.setMaxSpeed(500.0);
Serial.print("stepper2 in posizione:");
Serial.println(stepper2.currentPosition());
stepper3.setMaxSpeed(500.0);
Serial.print("stepper3 in posizione:");
Serial.println(stepper3.currentPosition());
stepper4.setMaxSpeed(500.0);
Serial.print("stepper4 in posizione:");
Serial.println(stepper3.currentPosition());
stepper5.setMaxSpeed(500.0);
Serial.print("stepper5 in posizione:");
Serial.println(stepper3.currentPosition());
stepper6.setMaxSpeed(500.0);
Serial.print("stepper6 in posizione:");
Serial.println(stepper3.currentPosition());
steppers.addStepper(stepper1);
steppers.addStepper(stepper2);
steppers.addStepper(stepper3);
steppers.addStepper(stepper4);
steppers.addStepper(stepper5);
steppers.addStepper(stepper6);
delay(3000);
}
void loop() {
if (Serial.available() > 0) { //read from serial
c = Serial.read(); // read Char
stringa = stringa + c; // add Char to string
conta4=conta4+1; //increase counter to store the length of the string
}
if(s==false){
if(stringa.charAt(conta4-1)==y){//enter in if only is the string has been acquired
for ( int i=0;i<conta4-1;i++)//go through the i-char of the string
{
if(stringa.charAt(i)==q) //if ';' is found
{
count=count+1; //increase count (go to the following row of the matrix)
}
if (stringa.charAt(i)!= q && stringa.charAt(i)!= w) //(a number has been found)
{
x[conta2]=stringa.charAt(i); //add number to x
conta2=conta2+1; //increase the counter if a number or a sign has been found
}
if(stringa.charAt(i)==w || stringa.charAt(i+1)==q|| stringa.charAt(i+1)==y){ //the number
//to be stored in the matix
//has been read
num= atoi(x); //convert the char array x into a number
if (conta3==6){ //the row of B has been completed
conta3=0;
}
for (int i=0; i<conta2;i++){
x*=' ';//set all the values of char array x to ' ' once x has been converted to integer*
-
}*
-
B[count][conta3 % 6]=num; //add num to B*
-
s=true; //exit from loop once the for cycle is over*
-
conta2=0; //set to 0 the index of x*
-
conta3=conta3+1; //increase counter because a number has been added to the matrix B*
-
conta5=conta5+1;//increase counter because a number has been added to the matrix B*
-
}*
-
}*
-
if (conta5==1788){ //once B has been completed store each row in a 1-D array and move the robot*
long positions [6];
for (int i=0;i<298;i++){
-
for (int j=0;j<6;j++){*
positions [j]=B*[j];*
}
steppers.moveTo(positions);
steppers.runSpeedToPosition();
}
}
}
* }*
* }*