ping sensor

hello, I have an ultrasonic ping sensor HC-SR04. I want the ping sensor to stop to rover and Serial.println (“object front”);. then once I push the next key it will move accordingly. What happens is that the rover will automatically tell me object front so I changed the code to print me the distance when this happens and it is constantly 0. any ideas? here is my code:

#include <Servo.h>
int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control
#define trigpin 3
#define echopin 10
Servo servo;
int duration, distance;
int servoPin = 9;
int minPulse = 900;
int maxPulse = 2100;
int turnRate = 100;
int refreshTime = 20;
int centerServo = 90 ;
int pulseWidth;
int moveServo;
int leftspeed = 255; //255 is maximum speed 
 int rightspeed = 255;
 int slowspeed = 0;
 int nospeed = 0;
long lastPulse = 0;


void setup(void)
{
 pinMode(servoPin, OUTPUT);
 pinMode(trigpin, OUTPUT);
 pinMode(echopin, INPUT);
  centerServo = maxPulse - ((maxPulse - minPulse)/2);
  pulseWidth = centerServo; 
 int i;
 for(i=5;i<=8;i++)
 pinMode(i, OUTPUT);
 Serial.begin(9600);
}
void loop(void)
{
    digitalWrite(trigpin, HIGH);
 delayMicroseconds(1000);
 digitalWrite(trigpin, LOW);
 duration = pulseIn(echopin, HIGH);
 distance = (duration/2) / 29.1;
  if (distance < 8) {
    halt (slowspeed, nospeed);
    Serial.println(distance);}
 while (Serial.available() < 1) {} // Wait until a character is received
 char val = Serial.read(); 
 switch(val) // Perform an action depending on the command
 {
 case 'w'://Move Forward
 forward (leftspeed,rightspeed);
 Serial.println("Moving Forward");
 break;
 case 's'://Move Backwards
 reverse (leftspeed,rightspeed);
 break;
 case 'a'://Turn Left
 left (leftspeed,rightspeed);
 break;
 case 'd'://Turn Right
 right (leftspeed,rightspeed);
 break;
 case 'f': //Stop
 halt (slowspeed,nospeed);
 break;
 case 'e':
 pulseWidth = pulseWidth - 100;
 break;
 case 'q':
 pulseWidth = pulseWidth + 100;
 break;
 case 'r':
 pulseWidth = centerServo;
 break;
 default:
 stop();
 break;

 if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }
   if (pulseWidth < minPulse) { pulseWidth = minPulse; }
 } 
 if (millis() - lastPulse >= refreshTime) {
    digitalWrite(servoPin, HIGH);   // start the pulse
    delayMicroseconds(pulseWidth);  // pulse width
    digitalWrite(servoPin, LOW);    // stop the pulse
    lastPulse = millis();           // save the time of the last pulse
  }
}
void stop(void) //Stop
{
 digitalWrite(E1,LOW);
 digitalWrite(E2,LOW);
}
void forward(char a,char b)
{
 analogWrite (E1,a);
 digitalWrite(M1,LOW);
 analogWrite (E2,b);
 digitalWrite(M2,LOW);}
void reverse (char a,char b)
{
 analogWrite (E1,a);
 digitalWrite(M1,HIGH);
 analogWrite (E2,b);
 digitalWrite(M2,HIGH);
}
void left (char a,char b)
{
 analogWrite (E1,a);
 digitalWrite(M1,HIGH);
 analogWrite (E2,b);
 digitalWrite(M2,LOW);
}
void right (char a,char b)
{
 analogWrite (E1,a);
 digitalWrite(M1,LOW);
 analogWrite (E2,b);
 digitalWrite(M2,HIGH);
}
void halt (char a, char b)
{
  analogWrite (E1,a);
  digitalWrite(M1, LOW);
  analogWrite (E2,b);
  digitalWrite(M2, LOW);
}

If your values are negative, maybe your variables are using the wrong data type. Can you try declaring your duration and distance variables as “unsigned long” instead of as “int”? This would be the return type recommended by the Arduino documentation: arduino.cc/en/Reference/PulseIn

If you try again with those data types, you should have different results and might in fact be the results you want.

We hope this helps,

I think you need to change this line

delayMicroseconds(1000);

from 1000us to 10us.

Ten microseconds is the amount recommended in the datasheet, and I think you might be missing the echo back pulse with a delay of 1000us.

I’m not sure I understand… What you do mean that you fixed the auto printing, and what do you mean by it isn’t reacting to anything?

Can you add the following lines after your distance calculation to see what values is being calculated?

Serial.print("Distance: ");
Serial.println(distance);

Sorry for such a late entry here. I uploaded the sketch and tried it out. The robot would move forward for about a sec then stop and it would print the value. Here is what was printed:

Moving Forward
Distance: -165
-165
Moving Forward
Distance: -166
-166
Moving Forward
Distance: -166
-166
Moving Forward
Distance: -167
-167
Moving Forward
Distance: -167
-167
Moving Forward
Distance: -170
-170
Moving Forward
Distance: -170
-170
Moving Forward
Distance: -169
-169
Moving Forward
Distance: -170
-170
Moving Forward
Distance: -169
-169
Moving Forward
Distance: -170
-170
Moving Forward
Distance: -169
-169
Moving Forward
Distance: -169
-169
Moving Forward
Distance: 7
7
Moving Forward
Distance: 7
7
Moving Forward
Distance: 5
5
Moving Forward
Distance: 4
4
Moving Forward
Distance: 3
3
Moving Forward
Distance: 2
2
Moving Forward
Distance: 1
1
Distance: 1
1
Distance: 3
3
Distance: 1
1

I think that fixed the auto printing of object front but now it isn’t reacting to anything

The sensor will not print the 0 as soon as the code is started. Now however when I have the rover moving forward and i put my hand in front of the sensor the rover continues to move and does not react.