Pages: [1]   Go Down
Author Topic: Ping sensor with if statement  (Read 471 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1872
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Mark
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I keep getting error messages
This is the only message I got:
Code:
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")
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please DO NOT CROSS POST.
It wastes time.

Threads merged
Logged

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 81
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
« Last Edit: July 09, 2013, 12:54:57 pm by whiskey » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No I was not using shield.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, every body for helping me out i finally fixed it.
Code:
#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;
}
Logged

Pages: [1]   Go Up
Jump to: