I have added all my code below... Basically I am trying to get an ultrasonic sensor to know if there is an objet in front of it, the problem is that when i put my hand infront of the sensor, it sees thats the first object and thats fine but then instantly sees that as the second object also.
Do you know of a way i can leave the if statement or give the arduino a wait time before it can run the main loop again?
Welcome to the Forum. This is the second time you have posted code without using code tags. The code tags make the code look
like this
when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.
Many questions can be answered by reading the documentation which is provided with the IDE, available under the help tab, or online here.
There are many other things that programmers do to make their code understandable. Please do them, as a courtesy to the members who volunteer their time to help you here. One is to use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.
You measure the distance to an object. If the distance is small enough, you count the object.
Then, loop() runs again. The object distance, a few milliseconds later, is still the same, so the if part is false, and the else if part is true, so you do that.
Then, loop() runs again. The object distance, a few milliseconds later, is still the same, so the if part is false, and the else if part is false, so you do the else part (or would if there were one).
You need to explain why you think this has a ghost of a chance of doing something useful.