Nema-17 running Slow

Hi, I have a Nema-17 connected to a TMC2208, connected to a CNC shield connected to an Arduino Uno. I also have a HC-SR04 sensor.

The intention of my code is for the Nema-17 to run until the HC-SR04 detects something within 5cm. Once something is detected the Nema-17 will return to it's original position then the sequence will play out again but with the Nema-17 rotating in the anticlockwise direction.

When I run this code everything I just mentioned works however, when the Nema-17 runs clockwise and anticlockwise the Nema-17 runs extremely slow, but when the Nema-17 returns to its origin it runs at the speed I have set.

I'm assuming there is something in the code that is blocking the Nema-17 running at the speed I have set such as the "while" statement but I'm not sure how to fix it.

Additionally, I'm using the CNC shield because I am planning on running multiple motors off the same arduino, there are also photos below the code showing the wiring.

Any help would be greatly appreciated, thank you.

#include <AccelStepper.h>

#define EN 8
#define X_DIR 5
#define X_STP 2

#define TRIG_PIN A0
#define ECHO_PIN A1

AccelStepper stepperX(AccelStepper::DRIVER, X_STP, X_DIR);

long measureDistance() {

  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH, 30000);

  if (duration == 0) {
    return -1;
  }

  long distance = (duration / 2) / 29.1;

  return distance;
}

void setup() {
  stepperX.setMaxSpeed(1000);
  stepperX.setAcceleration(500);

  Serial.begin(9600);

  pinMode(EN, OUTPUT);
  digitalWrite(EN, LOW);

  stepperX.setCurrentPosition(0);

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  Serial.println("Home position set to current location: 0");
}

void loop() {
  long distance;

  Serial.println("Rotating clockwise until object is detected");
  stepperX.setSpeed(1000);

  while (true) {
    distance = measureDistance();

    if (distance != -1) {
      Serial.print("Distance: ");
      Serial.print(distance);
      Serial.println(" cm");
    } else {
      Serial.println("No object detected");
    }

    if (distance > 0 && distance < 5) {
      Serial.println("Object detected within 5 cm, stopping motor");
      stepperX.stop();
      break;
    }

    stepperX.runSpeed();
  }
  delay(1000);

  Serial.println("Returning to home position");
  stepperX.moveTo(0);

  while (stepperX.distanceToGo() != 0) {
    stepperX.run();
  }
  delay(1000);

  Serial.println("Rotating counterclockwise until object is detected");
  stepperX.setSpeed(-1000);

  while (true) {
    distance = measureDistance();

    if (distance != -1) {
      Serial.print("Distance: ");
      Serial.print(distance);
      Serial.println(" cm");
    } else {
      Serial.println("No object detected");
    }

    if (distance > 0 && distance < 5) {
      Serial.println("Object detected within 5 cm, stopping motor");
      stepperX.stop();
      break;
    }

    stepperX.runSpeed();
  }
  delay(1000);

  Serial.println("Returning to home position");
  stepperX.moveTo(0);

  while (stepperX.distanceToGo() != 0) {
    stepperX.run();
  }
  delay(1000);
}


Sorry I cannot follow your gibberish, post your code using code tags and post an annotated schematic showing exactly how you have wire it.
Guidelines: Please read the advice in the topic "How to get the best from this forum". How to get the best out of this forum

You have a whole section of code outside of any function, like this:-

What do you think it is returning to? Nothing has been called yet. Does this actually compile?

Also having a 30 second timeout on a pulseIn statement is hardly going to speed anything up is it?

Sorry, I did look but it's my first time posting so I was a bit confused. I have now updated it so the code presents better and there is a schematic and a drawing. Thanks for your time.

Yes it does compile. The 30000 is the timeout in microseconds (30 milliseconds). If the pulse does not end within this time, the function returns 0. It's not 30 seconds.
The if statement is outside of the rest of the code on purpose, placed immediately after obtaining the duration value, this allows the function to quickly check for an error and handle it right away. If the duration is 0, the function can immediately return -1, signaling that no valid distance measurement could be obtained. The whole point of this is to prevent any further unnecessary computation with an invalid duration value.

Please do not keep altering the first post. It makes a nonsense of the comments that follow. The only acceptable thing you should alter on the first post is to put code into code tags. Otherwise this drip drip of information should be posted in the post where now you are saying "I updated the first post"

In future think about your first post before you make it.

Your so called schematic makes little sense as the CNC Shield doesn't have 103 pins. Note here that fritzing physical layout diagrams are almost universally hated here.

As to your code, your use of while(true) structures is very odd and quite bad practice.

That is because you are not adding delays on the return to origin part, where as you are adding delays by keeping on checking the distance to the object in the clockwise and anticlockwise runs.

That does not compute, the parts do not match your description and it is not a schematic but a frizzy which I do not work with as I do not have the parts and do not want to spend the time trying to find each one.

You can spend weeks spinning your wheels or you may get lucky and get your problem solved. We cannot see what you have in detail so we ask for an annotated schematic as you have it wired showing all connections including power, ground, supplies etc. I will normally spend a moment on a question if it is missing information I need I ask, if somebody has asked for it, great but if it is missing that I wonder if the OP is serious and probably will just go to another question. We also know there are many modules etc that look alike and may be called the same thing but they are different, that is why we ask for links to "Technical Information". We also know a lot stuff is purchased from the azon marketplace by sellers that do not know what they are selling and they do not bother to add all the technical information.

2 Likes

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