Sending data by serial monitor for angle control of a stepper motor with gearbox

Hello everyone,
I am making a program in python using inverse kinematics to send values to my arduino mega, but I have a problem. When I send my string of characters in my arduino serial monitor, my motor does not move, in the program is detailed lines of code for now I'm just testing a motor for later use with python.

when I send my string on my monitor it would be, for example A+30B+30C+30D+30E+30F+30.
A is my first motor with positive angle of 30 degrees, the motor does not move, the other values are given to fill my string, then it will be programmed, for now I am testing with only one motor.

//nema 14 motor = 400 pasos * 13.73 = 5492/360 = 15.255556
// ratio = 13.73:1
// driver dm542t
  int PUL1 = 10;
  int DIR1 = 11;
  int SET1 = 14;
  int number_of_steps=0;                                  // We declare the variable number of steps and start it at 0
  int num1,num2,num3,num4,num5,num6;
  int teta1,teta2,teta3,teta4,teta5,teta6;
  
/////////// note: I am working with a motor for now for testing, its specifications are above.
  float  j1stepPerGrade = 15.255556;                                  //engine compensation value
  float  j2stepPerGrade = 15.255556;
  float  j3stepPerGrade = 15.255556;
  float  j4stepPerGrade = 15.255556;
  float  j5stepPerGrade = 15.255556;
  float  j6stepPerGrade = 15.255556;


void setup() 
{
  Serial.begin(9600);
  pinMode(PUL1,OUTPUT);
  pinMode(DIR1,OUTPUT);
  pinMode(SET1,OUTPUT);
  //pinMode(SET1, HIGH);
 }

String data;
void loop() 
{
   ////////// //scheduled routines///////////////
    while(Serial.available())
    {
      char character = Serial.read();           //read the character variable in the serial port
      if(character != '\n')          //verify if there is a character in the serial port if there is not it will do a line break
      {
        data.concat(character);          //we concatenate the variable character
      }
      else
      {
        Serial.println(data);                      //we print the variable te type string in the serial port
        char a[30];                                    //We declare a variable a with a vector of dimension 30
        data.toCharArray(a,30);               // matrix
        data = "";                                    //we initialize the variable string
        
        /////////////////////// Here starts the decoding algorithm///////////////////////////////
        
        //we order the values ​​of the variable "a" for a dimension of 30
        if((a[0]=='A')&&(a[4]=='B')&&(a[8]=='C')&&(a[12]=='D')&&(a[16]=='E')&&(a[20]=='F')) 

        {
          Serial.println("OK");
          char ang1[5] = {a[1],a[2],a[3]};      //We declare character type ang1 and the positions for each character
          int  num1 = atoi(ang1);                      //convert each character of ang1 to integer
          num1 = (num1*j1stepPerGrade);       //we perform a value adjustment for the stepper motors

          char ang2[5] = {a[5],a[6],a[7]};
          int  num2 = atoi(ang2);
          num2 = (num2*j2stepPerGrade);

          char ang3[5] = {a[9],a[10],a[11]};
          int  num3 = atoi(ang3);
          num3 = (num3*j3stepPerGrade);
          
          char ang4[5] = {a[13],a[14],a[15]};
          int  num4 = atoi(ang4);
          num4 = (num4*j4stepPerGrade);

          char ang5[5] = {a[17],a[18],a[19]};
          int  num5 = atoi(ang5);
          num5 = (num5*j5stepPerGrade);
          
          char ang6[5] = {a[21],a[22],a[23]};
          int  num6 = atoi(ang6);
          num6 = (num6*j6stepPerGrade);
            
          /////////end coding and start assigning values ​​to engines////////////////////
         teta1 = num1;
         teta2 = num2;
         teta3 = num3;
         teta4 = num4;
         teta5 = num5;
         teta6 = num6;
         
        }
      }
    }
     while(teta1>number_of_steps)                          // Turn to the left in degrees
     {
          s_l();
          number_of_steps=number_of_steps+1;
          
     }
     while(teta1<number_of_steps)                     // Right turn in degrees
     {
           s_r();
           number_of_steps=number_of_steps-1;
          
      }
             
       data = "";                  // We initialize the string of received characters
       delay(2000);
       return_1();                    //we return to an initial point in this case it is 0
       shutdown_1();                 // Engine shutdown so it doesn't overheat
  
}

void s_r()                                  //steps to the right
{
   digitalWrite(DIR1,HIGH);
   digitalWrite(PUL1,HIGH);
   delayMicroseconds(500);
   digitalWrite(PUL1,LOW);
   delayMicroseconds(500);
}
void s_l()                            //steps to the left
{
   digitalWrite(DIR1,LOW);
   digitalWrite(PUL1,LOW);
   delayMicroseconds(500);
   digitalWrite(PUL1,HIGH);
   delayMicroseconds(500);
}
void shutdown_1()                       // Engine Shutdown
{         
   digitalWrite(DIR1,LOW);
   digitalWrite(PUL1,LOW);
   delayMicroseconds(500);
   digitalWrite(PUL1,LOW);
   delayMicroseconds(500);  
 }
 void return_1()                    //loopback
 {
   teta1=0;
 }

Hello guess. Edit your post and move all your formatted code into a code box by clicking the "</>" and pasting your code where it says "paste code here"

//nema 14 motor = 400 pasos * 13.73 = 5492/360 = 15.255556
// ratio = 13.73:1
// driver dm542t
int PUL1 = 10;
int DIR1 = 11;
int SET1 = 14;
int number_of_steps=0; // We declare the variable number of steps and start it at 0
int num1,num2,num3,num4,num5,num6;
int teta1,teta2,teta3,teta4,teta5,teta6;

/////////// note: I am working with a motor for now for testing, its specifications are above.
float j1stepPerGrade = 15.255556; //engine compensation value
float j2stepPerGrade = 15.255556;
float j3stepPerGrade = 15.255556;
float j4stepPerGrade = 15.255556;
float j5stepPerGrade = 15.255556;
float j6stepPerGrade = 15.255556;

void setup()
{
Serial.begin(9600);
pinMode(PUL1,OUTPUT);
pinMode(DIR1,OUTPUT);
pinMode(SET1,OUTPUT);
//pinMode(SET1, HIGH);
}

String data;
void loop()
{
////////// //scheduled routines///////////////
while(Serial.available())
{
char character = Serial.read(); //read the character variable in the serial port
if(character != '\n') //verify if there is a character in the serial port if there is not it will do a line break
{
data.concat(character); //we concatenate the variable character
}
else
{
Serial.println(data); //we print the variable te type string in the serial port
char a[30]; //We declare a variable a with a vector of dimension 30
data.toCharArray(a,30); // matrix
data = ""; //we initialize the variable string

    /////////////////////// Here starts the decoding algorithm///////////////////////////////
    
    //we order the values ​​of the variable "a" for a dimension of 30
    if((a[0]=='A')&&(a[4]=='B')&&(a[8]=='C')&&(a[12]=='D')&&(a[16]=='E')&&(a[20]=='F')) 

    {
      Serial.println("OK");
      char ang1[5] = {a[1],a[2],a[3]};      //We declare character type ang1 and the positions for each character
      int  num1 = atoi(ang1);                      //convert each character of ang1 to integer
      num1 = (num1*j1stepPerGrade);       //we perform a value adjustment for the stepper motors

      char ang2[5] = {a[5],a[6],a[7]};
      int  num2 = atoi(ang2);
      num2 = (num2*j2stepPerGrade);

      char ang3[5] = {a[9],a[10],a[11]};
      int  num3 = atoi(ang3);
      num3 = (num3*j3stepPerGrade);
      
      char ang4[5] = {a[13],a[14],a[15]};
      int  num4 = atoi(ang4);
      num4 = (num4*j4stepPerGrade);

      char ang5[5] = {a[17],a[18],a[19]};
      int  num5 = atoi(ang5);
      num5 = (num5*j5stepPerGrade);
      
      char ang6[5] = {a[21],a[22],a[23]};
      int  num6 = atoi(ang6);
      num6 = (num6*j6stepPerGrade);
        
      /////////end coding and start assigning values ​​to engines////////////////////
     teta1 = num1;
     teta2 = num2;
     teta3 = num3;
     teta4 = num4;
     teta5 = num5;
     teta6 = num6;
     
    }
  }
}
 while(teta1>number_of_steps)                          // Turn to the left in degrees
 {
      s_l();
      number_of_steps=number_of_steps+1;
      
 }
 while(teta1<number_of_steps)                     // Right turn in degrees
 {
       s_r();
       number_of_steps=number_of_steps-1;
      
  }
         
   data = "";                  // We initialize the string of received characters
   delay(2000);
   return_1();                    //we return to an initial point in this case it is 0
   shutdown_1();                 // Engine shutdown so it doesn't overheat
}

void s_r() //steps to the right
{
digitalWrite(DIR1,HIGH);
digitalWrite(PUL1,HIGH);
delayMicroseconds(500);
digitalWrite(PUL1,LOW);
delayMicroseconds(500);
}
void s_l() //steps to the left
{
digitalWrite(DIR1,LOW);
digitalWrite(PUL1,LOW);
delayMicroseconds(500);
digitalWrite(PUL1,HIGH);
delayMicroseconds(500);
}
void shutdown_1() // Engine Shutdown
{
digitalWrite(DIR1,LOW);
digitalWrite(PUL1,LOW);
delayMicroseconds(500);
digitalWrite(PUL1,LOW);
delayMicroseconds(500);
}
void return_1() //loopback
{
teta1=0;
}

Have you ever just printed the received string on the serial monitor? Let's start at the beginning and see where the problem is located.

1 Like

ok, i will edit it.

@guess29, please do not cross-post. Threads merged (and moved).

You might get some good ideas from reading Serial Input Basics - updated.

Actually OP does; after receiving a '\n'.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.