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);
} 
								