Is there a better library for the HC-SR04 ultrasonic sensor?

This is my first post on these forums so I’m sorry if it’s in the wrong section however, I was wondering if there was a better library for this type of sensor. The problem that I encounter is that the sensor doesn’t return the values it should. I am using the Ultrasonic_hc_sr04 library available from the public libraries on this site. I am also using an Arduino Mega 2560.

#include <Stepper.h>
#include <Servo.h>
#include "Ultrasonic.h"

String command;
String desDestination;

#define TRIG_PIN 12
#define ECHO_PIN 13

#define BAUDRATE 9600

const int STEPS_PER_REV = 2048;
int firstIndex = 0;
int startTime;
Ultrasonic us(TRIG_PIN, ECHO_PIN);
Stepper myStepper = Stepper(STEPS_PER_REV, 8, 10, 9, 11);
Servo myservo;

void setup() {
  Serial.begin(9600);
  myservo.attach(7);
  startTime = millis();
}

void loop() {
  command = "";
 if(Serial.available()>0)
 {
  double Destination;
  firstIndex = command.indexOf(',');
  desDestination = command.substring(0,firstIndex);
  Destination = desDestination.toInt();
  int Floor;
  us.measure();
  Floor = round(us.get_cm()/25.4 + 0.6);
   if(Floor == Destination)
   {
    myservo.write(90);
    myStepper.setSpeed(0);
   }
   else if(Floor < Destination){
    myservo.write(0);
    myStepper.setSpeed(10);
    myStepper.step(1);
   }
   else{
    myservo.write(90);
    myStepper.setSpeed(10);
    myStepper.step(-1);
   }
 };
}

The else case seems to be the only case that runs all of the time unless the sensor has an object pressed right up against the ultrasonic sensor, in which case the beginning if statement happens and the motor doesn’t run. This could be a problem in my application of code although it could be with the sensor. To be clear I am trying to adjust the Destination value so that the Floor value is changed over time until the two values are the same. I have tried multiple different types of math although this could be a problem that I have with the injection of the Destination. If the value of destination is not being changed could you let me know. Also, I am aware of the issue in the library with the centimeters being 2.54 for every inch which is why the denominator in the Floor calculation is 25.4. If there is something wrong with my code please let me know. If you suggest something else like a change in library if you could make some suggestions please. Thanks for any help you can give.

The code for ultrasonic sensor is very simple. You can use the code instead of library. See Ultrasonic Sensor tutorial

What does startTime = millis(); do besides hold a millis() value?

Seeing a }; can be a bit off putting.

Have you looked at the NewPing Library?

On a Uno and Mega a double is the same as a float.

double Destination;
 Destination = desDestination.toInt();

So you declare a double, which on a Uno/Mega is the same as a float and you assign a int to the float variable, what up w that?

And here

int Floor;
  
  Floor = round(us.get_cm()/25.4 + 0.6)

You make an int, do floating point math and store it as an int, why not just do int math, saves on time and memory storage.

if(Floor == Destination)

Then you want to compare floats and int, what up with that?

This is getting crazy.

And it was not clear.

Thanks for the feedback on the system, I changed out the data values to all floats so now they can all be compared a little easier. I also implemented that other form of ultrasonic sensor. It seems to do the same as the last code, which is only run the code in the else case even with the round value changing the value of Floor. Maybe this discussion would be better for the project forum now but thanks for the help here.

#include <Stepper.h>
#include <Servo.h>

String command;
String desDestination;

int trigPin = 12;
int echoPin = 13;

float duration_us, distance_cm;

const int STEPS_PER_REV = 2048;
float firstIndex = 0;
Stepper myStepper = Stepper(STEPS_PER_REV, 8, 10, 9, 11);
Servo myservo;

void setup() {
  Serial.begin(9600);
  myservo.attach(7);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  command = "";
 if(Serial.available()>0)
 {
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  float Destination;
  firstIndex = command.indexOf(',');
  desDestination = command.substring(0,firstIndex);
  Destination = desDestination.toFloat();
  float Floor;
  duration_us = pulseIn(echoPin, HIGH);
  distance_cm = 0.017*duration_us;
  Floor = round(distance_cm/10 + 0.6);
   if(Floor == Destination)
   {
    myservo.write(90);
    myStepper.setSpeed(0);
   }
   else if(Floor < Destination){
    myservo.write(0);
    myStepper.setSpeed(10);
    myStepper.step(2048);
   }
   else{
    myservo.write(90);
    myStepper.setSpeed(10);
    myStepper.step(-2048);
   }
   delay(500);
 }
}

I have found why the code isn’t running at this point. It is that the value of Destination is not getting changed by the serial monitor when I input the value. I know this should be basic but I am extremely new to arduinos. I’ll work on it.