programming help please

Hi,
i am programming a small two wheel car using an arduino.
here is the code for making it turn if an object comes too close to it (pin 3 and 5 are the motors):

const int pingPin = 2;

void setup() {
 pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
  
}

void loop()
{
 
  long duration, inches, cm;

  
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  delay(10);

 
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  if(inches>4){
    digitalWrite(3,HIGH);
  digitalWrite(5,HIGH);
  }
  else{
     digitalWrite(3,LOW);
    digitalWrite(5,HIGH);
    delay(7600);
   
  }
  
  
  

}
long microsecondsToInches(long microseconds)
{
 
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
 
  return microseconds / 29 / 2;
}

The car just spins around in a circle and does not turn when I want it to.
any help is needed. Thanks in advance,
jared

Have you tried writing the measured distance to the serial interface? Maybe you should try your distance sensor before installing it on your robot. I suppose you got most of the code from an example for the distance sensor, didn't you? What sensor are you using?

Maybe you should try your distance sensor before installing it on your robot. I suppose you got most of the code from an example for the distance sensor, didn't you?

Yes and sort-of.
This is the distance sensor i am using:

If you tried your sensor alone (just the Arduino and the sensor), did it work then? When writing the distance values to the serial interface, what values do you get? How accurate are these values (hold your hand in front of the sensor in a known distance and note the value the sensor is measuring)?

If you tried your sensor alone (just the Arduino and the sensor), did it work then?

yes

How accurate are these values

very accurate

What does your debug output tell you is happening?

I am new to the arduino (>1year), what is a debug output?

Serial Output can be considered debug output

const int pingPin = 2;

void setup() {
 pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  if(inches>4){
    digitalWrite(3,HIGH);
  digitalWrite(5,HIGH);
  }
  else{
     digitalWrite(3,LOW);
    digitalWrite(5,HIGH);
    delay(7600);
       }
  Serial.println(inches); // debug output
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Try that

Thank you, it works. :slight_smile:

Not a problem, feel free to PM me if you need anymore help (or simply post again)