Ping sensor with if statement

I'm trying to make a robot that when it gets under 21 centimeters away from something, it backs up, turns around and keeps going. I keep getting error messages, could someone please help?

#include <Servo.h>
Servo leftmotor;
Servo rightmotor;
int pwmL,pwmR;
int pingPin = 3;
int threshold = 21;
int cm = 0;

void setup()
{
leftmotor.attach(9); // af-motorshield servo 2
rightmotor.attach(10); // af-motorshield servo 1
Serial.begin(9600);

}

void loop()
{
  
leftmotor.write(pwmL -180); //  for use with continuous motor
rightmotor.write(pwmR +180);

long duration, inches, cm;
pinMode(pingPin,OUTPUT);
digitalWrite(pingPin,LOW);
delayMicroseconds(2);
digitalWrite(pingPin,HIGH);
delayMicroseconds(5);
digitalWrite(pingPin,LOW);


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


cm = microsecondsToCentimeters(duration);

Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(10);
}
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;

  
if(cm < threshold){
  leftmotor.write(pwmL +180);
  rightmotor.write(pwmR -180);
  delay(500);
  leftmotor.write(pwmL +180);
  rightmotor.write(pwmR +180);
  delay(1000);
}else{
  leftmotor.write(pwmL -180); 
  rightmotor.write(pwmR +180);
}
}

I keep getting error messages, could someone please help?

Without seeing them? No.

Though it's a bit of a mystery why you added code AFTER the return statement in that function. Seems to indicate that you don't comprehend what return does.

Auto format (Tools in the IDE) your code and you will see at least one of your problems!

Mark

I keep getting error messages

This is the only message I got:

Binary sketch size: 4654 bytes (of a 14336 byte maximum)

(though, as PaulS pointed out, a compilation with full warnings enabled would have squawked about the code after the "return")

I attached the ping to a project I'm making and tried to program it so that when it gets within 21 cm of something, it backs up, turns around and keeps going.
The code doesn't get any error messages,but when i put my hand or any other object within 21 cm of the pin, it just keeps going forward.

I'm new at coding, could someone please tell me where i went wrong.

#include <Servo.h>
Servo leftmotor;
Servo rightmotor;
int pwmL,pwmR;
int pingPin = 3;
int threshold = 21;
int cm = 0;

void setup()
{
leftmotor.attach(9); // af-motorshield servo 2
rightmotor.attach(10); // af-motorshield servo 1
Serial.begin(9600);

}

void loop()
{
  
leftmotor.write(pwmL -180); //  for use with continuous motor
rightmotor.write(pwmR +180);

long duration, inches, cm;
pinMode(pingPin,OUTPUT);
digitalWrite(pingPin,LOW);
delayMicroseconds(2);
digitalWrite(pingPin,HIGH);
delayMicroseconds(5);
digitalWrite(pingPin,LOW);


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


cm = microsecondsToCentimeters(duration);

Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(10);
}
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;

  
if(cm < threshold){
  leftmotor.write(pwmL +180);
  rightmotor.write(pwmR -180);
  delay(500);
  leftmotor.write(pwmL +180);
  rightmotor.write(pwmR +180);
  delay(1000);
}else{
  leftmotor.write(pwmL -180); 
  rightmotor.write(pwmR +180);
}
}

sorry, i tried again and i didnt get any error messages but when something gets within 21 cm it doesn't turn.
I'm new to coding, could you please explain what you mean about the coding before the return.

isaac868:
I'm new to coding, could you please explain what you mean about the coding before the return.

https://www.google.com/search?btnG=1&pws=0&q=c%2B%2B+return&safe=active

Please DO NOT CROSS POST.
It wastes time.

Threads merged

int pingPin = 3;

void setup()
{
leftmotor.attach(9); // af-motorshield servo 2
rightmotor.attach(10); // af-motorshield servo 1
Serial.begin(9600);

are you using the adafruit shield? if so pin 3 that your PING is connected to isn't available.

No I was not using shield.

Thank you, every body for helping me out i finally fixed it.

#include <Servo.h>

const int threshold = 21;
const int pingPin = 3;
Servo leftmotor;
Servo rightmotor;
int pwmL,pwmR;

void setup() {

  leftmotor.attach(9); 
  rightmotor.attach(10); 
}

void loop()
{
 
  long duration, cm;


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


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



  cm = microsecondsToCentimeters(duration);
  
if(cm < threshold){
  leftmotor.write(pwmL -180);
  rightmotor.write(pwmR +180);
  delay(500);
  leftmotor.write(pwmL +180);
  rightmotor.write(pwmR +180);
  delay(1000);
}else{
  leftmotor.write(pwmL +180); 
  rightmotor.write(pwmR -180);
}
  
  
  
 
delay(100);
}
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}