Small bug in code using NEMA17 and HCSR04 ultrasonic sensor.

I am trying to use a HC SR04 sensor for driving the carriage on a 2020 profile. Here is the program for it. I am not getting accurate results while moving the stepper motor from start position to finish position.

When moving to start position, it take 2 iterations for the code to get to the distance of less than 10cm from any given stop position of a sliding rail. I would expect that to happen in one iteration itself which means to get accurate count of no. of steps required for moving the carriage from enplane to start line.

The variable stepsforOnecm value was found by running the code multiple times. It might not be accurate.

The whole goal is to run the carriage from one end to another on a sliding rail. I want to make sure that if in case the system stop in between due to power failure or any other reason. The carriage should reposition itself to the startling and then resume the process.

#define MOTR_ENABLE 6
#define MOTR_STEP 5
#define MOTR_DIRECTION 4

#define SNSR_TRIGGER 9
#define SNSR_ECHO 10

long duration;
int distance;

int finishLine = 92; // in cm

int startLine = 10; // in cm

int currentCarriagePosition;

int Index = 0;
int direction = 1;
int stepsforOnecm = 85;

void setup() {

  Serial.begin(9600); // Starts the serial communication
  pinMode(SNSR_TRIGGER, OUTPUT); // Sets the sensor Trigger pin as an Output
  pinMode(SNSR_ECHO, INPUT); // Sets the sensor Echo pin as an Input

  pinMode(MOTR_ENABLE, OUTPUT); // Sets the motor Enable pin as an Output
  pinMode(MOTR_STEP, OUTPUT); // Sets the motor Step pin as an Output
  pinMode(MOTR_DIRECTION, OUTPUT); // Sets the motor Direction pin as an Output

  digitalWrite( MOTR_ENABLE , LOW); // Set the motor Enable to LOW initially.
  currentCarriagePosition = getDistanceTravelled();
}

void loop() {
  if (currentCarriagePosition > startLine) {
    Serial.print("Resetting the carriage position. Distance located at = ");
    Serial.println(currentCarriagePosition);
    direction = 1;
    int stepsToStartLine = currentCarriagePosition * stepsforOnecm;
    moveCarriage(direction, stepsToStartLine);
    currentCarriagePosition = getDistanceTravelled();
  } else {
    Serial.print("carriage at start position, Distance = ");
    Serial.println(currentCarriagePosition);
    delay(5000);
    Serial.print("Send carriage to finish line ");
    int stepsTofinishLine = finishLine * stepsforOnecm;
    direction = 0;
    moveCarriage(direction, stepsTofinishLine);
    currentCarriagePosition = getDistanceTravelled();
    delay(5000);
  }
}

void moveCarriage( int direction, int  steps) {
  Serial.print("Moving the carriage in steps = ");
  Serial.println(steps);
  if (direction) {
    digitalWrite(MOTR_DIRECTION, HIGH);
  } else {
    digitalWrite(MOTR_DIRECTION, LOW);
  }
  for (Index = 0; Index < steps; Index++)
  {
    digitalWrite(5, HIGH);
    delayMicroseconds(500);
    digitalWrite(5, LOW);
    delayMicroseconds(500);
  }
}


int getDistanceTravelled() {
  // Clears the SNSR_TRIGGER
  digitalWrite(SNSR_TRIGGER, LOW);
  delayMicroseconds(2);

  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(SNSR_TRIGGER, HIGH);
  delayMicroseconds(10);
  digitalWrite(SNSR_TRIGGER, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(SNSR_ECHO, HIGH);

  // Calculating the distance
  distance = (duration * 0.034 / 2); // distance in CM.
  return distance;
}

The carriage should reposition itself to the startling and then resume the process.

That sounds like the perfect job for a limit switch

The HC SR04 sensor is unlikely to be accurate enough for what you are trying to do.

The HC SR04 sensor is unlikely to be accurate enough for what you are trying to do.

Especially given that you convert the time taken to receive the echo into a floating point value and then truncate it to an int.