#28015 ultrasonic rang finder

Had been looking around clues for my project (which is navigate around obstacles) but when i read on the codes online, i found out they had use these two const int.

const int pingPin = 7;
const int TxPin = 6;

I know that int pingpin is my sensor, yet i have no idea what int TxPin means.

I know that int pingpin is my sensor

No, it is the designator of the pin to which your sensor is presumably connected.
It is not your sensor.

yet i have no idea what int TxPin means

"Tx" == "transmitter", "Rx" == "receiver"

Any code to be recommended for obstacle avoidance? Have gone through many codes but little idea what it means cause all codes differ from each other.

The principle is simple.

Start the vehicle moving forward then, each time through the loop() function measure the distance ahead. If the distance falls below the required threshold then stop the vehicle.

Now it is up to you what you do to avoid the obstacle.
(a) Back off and turn randomly left or right and continue forward again.
(b) Move the distance scanner left and right to find a clear path then turn that way and continue forward again.
(c) Any other strategy that you can think of.

Start with the simple "move forward and stop when an obstacle is detected" part of the program then expand it. Or start even easier and write a program to just print the distance in front of the sensor and forget the vehicle movement for now.

Which part are you stuck on ?
How far have you got ?

Hi this is my current code. I am stuck here as i could not figure out how to insert another code to move the robot

const int pingPin = A2;

void setup() {
Serial.begin(9600);
}

void loop()
{
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);

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
}

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

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

I am stuck here as i could not figure out how to insert another code to move the robot

And because you have told us nothing about your robot, so are we.

Please use code tags when posting code..

I'm using this code, and my problem is that my robot only can sense distance for only first few seconds than it cannot sense anymore, the serial monitor just show the value "0"
Sorry pretty new to this, that's why i don't really know how to express my questions

const int pingPin = A2;
const int distance = 18;

void setup() {
Serial.begin(9600);
pinMode(5,OUTPUT);
pinMode(9,OUTPUT);
pinMode(6,OUTPUT);
pinMode(10,OUTPUT);
}

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

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

if ((pingPin <= distance))
{
analogWrite(5,150);
analogWrite(9,120);
digitalWrite(6,LOW);
digitalWrite(10,LOW);
}
else if ((pingPin >= distance))
{
analogWrite(5,LOW);
analogWrite(9,LOW);
digitalWrite(6,20);
digitalWrite(10,170);
}
}

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

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

Have you checked the supply voltage?

Please use tags when posting code.

Oh yeah, and DO NOT CROSS-POST.

Yes, i've come to realise that whenever i use the "if" or "else" code, the machine would not be giving any numberical readings, instead it'll be stuck at 0, have any idea why?

Because you're turning on motors, increasing current drain and lowering the supply voltage?

if ((pingPin <= distance))

And the value of "pingPin" is what, exactly?

the value of Pingpin will be the value shown on the serial monitor, that's why it cannot be defined as there might be different values

the value of Pingpin will be the value shown on the serial monitor, that's why it cannot be defined as there might be different values

Nice try, but wrong.

Please Sir, I know the value of pingPin !

const int pingPin = A2;

Do I get extra points for knowing that it also has another value ?

No, you only get extra points for knowing it has at least one other value. :wink:

Oh you are a tease sometimes !
Other values ?????

Well, maybe not so much a value as an alias (at least as far as analogRead is concerned) - there's 2, 16 or 56 on the common platforms, off the top of my head.