Abnormal Vibrations and Constant RPM

Hello,

I am attempting to make a stepper rotate a certain amount of times based on user input. The first set of code (Did not include user input) was used as a test to make that the wiring was done properly. It worked and it made the motor move at different RPMs because of the different delays defined. However, when I then uploaded some code I wrote that requires user input, the motor did move the correct amount of times, but the speed of the motor was constant no matter how I changed the delays and re-uploaded the code. Also, the motor seems to vibrate much more than with the automatic code which is slightly concerning. For reference, I am not micro-stepping (yet) and I am just using the driver board's default settings. Any ideas why this is happening?

Motor: PK264-01A
Driver Board: A4988

First set of code (automated version):

const int dirPin = 2;  // Direction
const int stepPin = 3; // Step
 
// Motor steps per rotation
const int STEPS_PER_REV = 200;
 
void setup() {
  
  // Setup the pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
}
void loop() {
  
  // Set motor direction clockwise
  digitalWrite(dirPin,HIGH); 
  
  // Spin motor one rotation slowly
  for(int x = 0; x < STEPS_PER_REV; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(2000); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(2000); 
  }
  
  // Pause for one second
  delay(2000); 
  
  // Set motor direction counterclockwise
  digitalWrite(dirPin,LOW);
  
  // Spin motor two rotations quickly
  for(int x = 0; x < (STEPS_PER_REV * 2); x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(1000);
  }
  
  // Pause for one second
  delay(2000);
}

Second set of code (user input required):

// Define Constants

// User Inputs (Changes based on motor and application)
// Set default frequency of motor when there is no user input
const int defaultfreq = 25; // In Hz

// Other constants
const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data
boolean userinput = false;
int dataNumber = 0;             // new for this version
float recerveddec;

// Connections to A4988
const int motor1dirPin = 2;  // Motor 1 direction
const int motor1stepPin = 3; // Motor 1 movement

// Connections to User Interface
const int numposmove = 10; // Nuemrical Input Positive Movement
const int numnegmove = 11; // Numerical Input Negative Movement
const int motorsetrpm = A0; // Set RPM for Pk266-01A and Pk266-01B (Input should be a voltage between 0 and 5 V) [RPM/Hz range: 0 to 4500/750] Recomends ~150 RPM or 25 Hz
const int motor1ID = A1; // ID pin for motor 1

void setup() {
  
  // Designate Output Pins
  pinMode(motor1stepPin,OUTPUT);
  pinMode(motor1dirPin,OUTPUT);
  // Designate Input Pins
  pinMode(motor1ID,INPUT_PULLUP);
  pinMode(numposmove,INPUT_PULLUP);
  
  {
    // Initialize serial and wait for port to open:
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
  }
  delay(5000);
}


// Stepper Motor Program Begins and Loops
void loop()
{
   recData();
   displayData();
   Motor1Move();
   //Motor2Move();
   //Motor3Move();
}

float setspeedfreq = defaultfreq / (1E6); // Sets the freq in to default when no user input

void recData()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char c;
   if (Serial.available() > 0)   // If there is a serial available, read it
   {
      // read the incoming byte:
      c = Serial.read();      
      if (c != endMarker)
      {
         receivedChars[ndx] = c;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         Serial.println(receivedChars);
         ndx = 0;
         userinput = true;
      }
   }
}

void displayData()
{
   if (userinput == true)
   {
      recerveddec = atoi(receivedChars);
   }
}

void Motor1Move()
{
  
  // To control motor 1 positive movements via numerical input (Input must be an integer value)
  if (digitalRead(motor1ID) == LOW) { // ID motor 1
    // Serial.print("Motor 1 Selected");
    if (digitalRead(numposmove) == LOW) {
      // Serial.print("Numerical positive movement selected");
      // Send data only when you receive data greater than or equal to the minimum distance motor can move
      if (userinput == true) { // If there is a serial available, read it
        Serial.print("Identified user input");
        digitalWrite(motor1dirPin,HIGH); // Set motor rotation direction
        if (abs(recerveddec) >= 1) {
          Serial.print("Number of steps in for loop");
          Serial.println(recerveddec);
          for (int substep = 1; substep <= recerveddec; substep++) {
            // Serial.print("Loop in progress");
            digitalWrite(motor1stepPin,HIGH);
            delayMicroseconds(3000);
            digitalWrite(motor1stepPin,LOW);
            delayMicroseconds(3000);
          }
        }
      }
      userinput = false;
    }
  }
}

A4988-datasheet.pdf (283 KB)

pk2-catalogue-2013-en.pdf (1.24 MB)

[BTW The default setting probably is microstepping]

You cannot drive a stepper like this, you have to ramp up the step rate and ramp it down again (the motor
has inertia and will invariably fail to work if asked to jump instantly to 500 steps/second).

Try AccelStepper library to make this aspect easy as it does automatic ramping for you.