Control 2 stepper motors using serial read

Hello,

I have 2 stepper motors Nema 17 and I want each motor to do a number of rotations based on serial read and some calculations.

Example if I enter 5 I want for the firs motor to do 6.41 turns and for the second motor to do 121.79 turns.

My problem is that inside the loop function it does the calculus and then it goes again with 0 values and gives the second motor a value of 128.21 turns, which is incorect.

Example:

val = 5
V1 will be val * 10
N1 will be V1 / 7.8 = 6.41 (turns should do stepper 1)

V2 will be 1000 - V1
N2 will be V2 / 7.8 = 121.79 (turns should do stepper 2)

void loop() {
  
 if (Serial.available() >0)
  {
      val = Serial.parseInt();
      val2 = val;
     
      int c2 = val * 10;
      float n1 = c2/x;
      Serial.println(val, DEC);
    
          if (n1 > 0)
            {
              for (int i=1; i<=n1; i++)
                {
                  stepperN();
                }
              Serial.print(n1, 2);
              Serial.println("[turns]"); //6.41 turns
            }
            
      int c3 = y - (val2 * 10);
      float n2 = (c3)/x;
      Serial.println(val2, DEC);
      
          if (n2 > 0)
            {
              for (int i=1; i<=n2; i++)
                { 
                  stepperH();
                }
              Serial.print(n2, 2);
              Serial.println("[turns]"); //121.79 turns
            }
  }
     if (Serial.available() <0)
       {
        digitalWrite(enPin,HIGH); 
       } 
}

Turn off line endings in serial monitor in the box in the attached image. Change Newline to No line ending. With line ending set to Newline a line feed character (0x0a or '\n')is sent after your number. Since parseInt only removes the number from the serial buffer, the line feed is left over and is evaluated the next time through loop(). The parseInt function interprets that character as a 0 (non numeric).

line ending.jpg

For information on doing reliable and non blocking serial communication see the serial input basics tutorial. The parseInt function is a blocking function that can slow the response of your program.

line ending.jpg

groundFungus:
Turn off line endings in serial monitor in the box in the attached image. Change Newline to No line ending. With line ending set to Newline a line feed character (0x0a or '\n')is sent after your number. Since parseInt only removes the number from the serial buffer, the line feed is left over and is evaluated the next time through loop(). The parseInt function interprets that character as a 0 (non numeric).

line ending.jpg

For information on doing reliable and non blocking serial communication see the serial input basics tutorial. The parseInt function is a blocking function that can slow the response of your program.

Aw I see, now it shows the right value so I have to see if the steppers work and it should

As you said, now is set to No editing line.

Now I have to figure out how to control the stepper speed.

const int stepPin = 5; 
const int dirPin = 2; 
const int enPin = 8;

const int stepPin2 = 7;
const int dirPin2 = 3;
const int enPin2 = 10;

long val;
long val2;
float x = 7.8;
float y = 1000;

void setup() {
  Serial.begin(9600);
  Serial.println("Input value:");

  
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode(enPin,OUTPUT);

  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);
  pinMode(enPin2, OUTPUT);
  
  digitalWrite(enPin,LOW);
  digitalWrite(enPin2,LOW);
}

void stepperN()
{
  digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
    }
  //delay(500); //
}

 digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
    }

You don't say which stepper driver that you are using but common stepper drivers step on the low to high transition. So the high part of the pulse only needs to stay high for a few microseconds and the duration of the low part sets the speed.

See Robin2's simple stepper code.

Really consider using the techniques in the serial input basics instead of parseInt(). Later when the program grows you will see why parseInt is not the best way to do serial input.

groundFungus:

 digitalWrite(stepPin,HIGH);

delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
    }




You don't say which stepper driver that you are using but common stepper drivers step on the low to high transition. So the high part of the pulse only needs to stay high for a few microseconds and the duration of the low part sets the speed.

[See Robin2's simple stepper code.](https://forum.arduino.cc/index.php?topic=277692.0)

Really consider using the techniques in the serial input basics instead of parseInt(). Later when the program grows you will see why parseInt is not the best way to do serial input.

Hello and sorry for that, I am using TB6600 stepper driver.
I am using parseInt() because I saw an youtube example of it. That is the only reason.