 Hi, I been lerarning the arduino, I am newbe in programing and arduino. I have a parallax Ping sesor working, using the example code from arduino IDE, my question is... How can make it to printline when and object is at lets say 24 inches away from sensor. thank you in advance

newbe ansious to learn and help :)

Assuming you have been playing around with the playground code found here: http://www.arduino.cc/playground/Main/UltrasonicSensor

I would probably do something like this.

``````void loop()
{
int x = 0;
x = ping();
if(( x < 25)||(x > 24)) {
Serial.println(x);
}
delay(250); //delay 1/4 seconds.
}
``````

As there are many ways to do this, this is just a quick example of one of them. The line I added is the “if” statement, and what I’m doing is saying if x is less than 25 OR x greater than 24, print the line out from the serial.

To understand more on how to read and work with conditional data check out http://arduino.cc/en/Reference/HomePage control structures, and specifically the if, and if…else options.

Hope this helps,

-Chris G.

thank you somuch
this code http://www.arduino.cc/playground/Main/UltrasonicSensor
is actually the best so far, measure more acure and clean

now this code

``````void loop()
{
int x = 0;
x = ping();
if(( x < 25)||(x > 24)) {
Serial.println(x);
}
delay(250); //delay 1/4 seconds.
}
``````

would you explain the line

``````f(( x < 25)||(x > 24)) {
``````

I really thank you for your help.
;D

The double bar symbol is the C "logical OR" operator. "If x is less than 25 OR x is greater than 24".

Hmm... now that doesn't make sense. It'll always be TRUE, I think. Maybe it should be:

``````if (x == 24) {
``````

...assuming that the original plan was to print the message only when the detected target was 24 inches from the sensor, and the "ping" function returns distance directly in inches.

is making sence

thanks guys
here is code that I am using now

``````// * Copyleft 2007 Jason Ch

//This code returns the distance in Inches... I think it's more accurate than other code I have seen online and returns more usable results. Remove the *.39 to return cm instead of inches. You could make float ultrasoundValue = 0; but then you can't print it unless you transfer it into another type, but it could be used for further calculations.

unsigned long echo = 0;
int ultraSoundSignal = 7; // Ultrasound signal pin
unsigned long ultrasoundValue = 0;

void setup()
{
Serial.begin(9600);
pinMode(ultraSoundSignal,OUTPUT);
}

unsigned long ping(){
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
ultrasoundValue = (echo / 58.138) * .39; //convert to CM then to inches
return ultrasoundValue;
}

void loop()
{
int x = 0;
x = ping();
if( x < 25) {
Serial.println("AAAAAAAAAAAAAAA");
}
delay(2250); //delay 1/4 seconds.
}
``````

what is 25 for, inches, cm. or just raw data.

``````delay(2250); //delay 1/4 seconds.
``````

Is that a typo? One quarter of a second would be 'delay (250);'.

The double bar symbol is the C "logical OR" operator. "If x is less than 25 OR x is greater than 24".

Hmm... now that doesn't make sense. It'll always be TRUE, I think. Maybe it should be:

Correct me if I'm wrong, but since we are dealing with floats, the logic would be if the value is less than 25.0 but more than 24.0 (aka 24.1, 24.3, 24.989283940890) do the function. I guess the better math would have been to use if x >= 24 so that you will do the function at 24.0.

-Chris G.

Correct me if I’m wrong, but since we are dealing with floats,

We’re dealing with ints:

``````int x = 0;
``````

the logic would be if the value is less than 25.0 but more than 24.0

That would require the AND operator:

``````if ((x < 25) && (x > 24)) {
``````

Which won’t work with ints, but would work with floating-point:

``````double x;
if ((x < 25.0) && (x > 24.0)) {
``````

Wow, yup, I see I was wrong on almost every part. Teach me to try to reply to posts late at night after a long day at work.

Thanks for correcting me Anachrocomputer

-Chris G.