Go Down

Topic: Ping sensor with if statement (Read 722 times) previous topic - next topic

isaac868

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?




Code: [Select]
#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);
}
}

PaulS

Quote
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.

holmes4

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

Mark

AWOL

Quote
I keep getting error messages

This is the only message I got:
Code: [Select]
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")
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

isaac868

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.

Code: [Select]
#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);
}
}

isaac868

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.

Arrch


AWOL

Please DO NOT CROSS POST.
It wastes time.

Threads merged
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

whiskey

#8
Jul 09, 2013, 07:52 pm Last Edit: Jul 09, 2013, 07:54 pm by whiskey Reason: 1
Code: [Select]

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.

isaac868


isaac868

Thank you, every body for helping me out i finally fixed it.
Code: [Select]

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

Go Up