Code does not work

Trying to modify PING code to set a distance limit.

We'll need a lot more information before we can help you.

Please post your full sketch.

If possible, you should always post code directly in the forum thread as text using code tags:

  • Do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code. This will make it easier for you to spot bugs and make it easier for us to read.
  • In the Arduino IDE or Arduino Web Editor, click on the window that contains your sketch code.
  • Press "Ctrl + A". This will select all the text.
  • Press "Ctrl + C". This will copy the selected text to the clipboard.
  • In a forum reply here, click the "Reply" button.
  • click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum's code tags markup to your reply.
  • Press "Ctrl + V". This will paste the sketch between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.
  • Repeat the above process if your sketch has multiple tabs.

This will make it easy for anyone to look at it, which will increase the likelihood of you getting help.

If the sketch is longer than the 9000 characters maximum allowed by the forum, then it's OK to add it as an attachment. After clicking the "Reply" button, you will see an "Attachments and other settings" link that will allow you to make the attachment.

When your code requires a library that's not included with the Arduino IDE please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.


Please provide a detailed description of the behavior you expect from your code as well as the behavior you're observing that doesn't match your expectations.

If you're getting any error messages, then you need to post the full and exact text of the error message here (as text, not a picture!).

I have used the "Ping" program using a MB 1260 sensor. That seems to work. I want to know when the distance is less than a given point. Every time I add an "if" statement it does not work. Error messages are not helping. I am good at programing. Thanks

Give it a stern talking-to and send it to bed without supper.

Or post your code and explain your problem.

(deleted)

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

  • am good at programing*

But can't work out how to add an IF statement!

we (the others users) have no glas-phere to look into in which way you coded a if-condition wrong.
There is no other way than posting the code.

have you ever looked into the reference?

I'm willing to help but not as long as you use such cryptic names like "T1", "T2" "and"
The compiler doesn't care about the names as long as they are all different for things that have to be different.
But humans care about names. It will become much easier to read the code if you consequently use self-explaining names.

best regards Stefan

I have tried to add an "if" condition to the code for the PING example. I can not make it work.

And only YOU know where you added that "if" statement and what the compiler reported.

Please take a moment to read the sticky post at the top of the forum to learn how to create good posts with include your code and/or any error messages and how to do that include using code tags.

It helps people help you.

@frog11

TOPIC MERGED.

Any more cross posting duplication of the same question WILL result in a time out from the forum !

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.

Here is the code that is my problem. I want it to print out the distance and light the LED when short of a given distance.

// The same pin is used to read the signal from the PING))): a HIGH pulse
// whose duration is the time (in microseconds) from the sending of the ping
// to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

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

delay(500);

long microsecondsToInches(long microseconds) {
// According to Parallax's datasheet for the PING))), there are 73.746
// microseconds per inch (i.e. sound travels at 1130 feet per second).
// This gives the distance travelled by the ping, outbound and return,
// so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

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 < 180) {
digitalWrite(11, High);

}
else
{
digitalWrite(11, LOW);

}

That is not a complete sketch. There is no setup() and no loop() function.

As pointed out to you several times, read the sticky post and learn how to post your code using code tags.

What does the code actually do? How is that different from what you want the code to do?

Please post a schematic of the wiring.

Do you actually read the responses to your questions?

OK that was the last merge !

You were given advice by not one but TWO moderators.

Third time will be a strike out.

Please read How to use this forum - please read. - Installation & Troubleshooting - Arduino Forum and pay specific attention to point #7 about posting code.

As indicated, your code is not complete. Post you complete code between
** **[code]** **
and
** **[/code]** **
(so called code tags).

I suspect that even without an if your code will not compile.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

return microseconds / 29 / 2;


    if (cm < 180) {

Guess what "return" does.

Thanks for the reply. I really do not understand. I think that is what I did in my code. I will check.

frog11:
Thanks for the reply. I really do not understand. I think that is what I did in my code. I will check.

The “return” statement leaves the current function. Any code after a “return” cannot execute, so where you placed the “if” in your posted code (i.e. directly after a return) is completely pointless.

As we’ve been telling you for some time. Post ALL of your sketch. Right from the very first line to the very last.
Place the sketch inside code tags [​code][​/code] so it formats correctly on the forum. You can insert them with the </> button on the post toolbar.
It’s no good posting what you think you tried, a snippet, or some re-typing of your original program.