How can I stop my else if statement from running straight after my first if

Hi,

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?

int distance_thresh = 12;
int object_count = 0;

#define trigPin 8
#define echoPin 9

void setup() {

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

Serial.begin(9600);

}

void loop() {
// Below is the calculation for the ultrasonic sensor to test how far away an object is
long duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (object_count == 0 && distance < distance_thresh){
Serial.println("First Object Found");
Serial.write("\n");
object_count = object_count + 1;
}
else if (object_count == 1 && distance < distance_thresh){
Serial.println("Second Object Found");
Serial.write("\n");
object_count = object_count + 1;
}
}

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.

Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question ...

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.

distance = (duration/2) / 29.1;
    if (object_count == 0 && distance < distance_thresh){
          Serial.println("First Object Found");
          Serial.write("\n");
          object_count = object_count + 1;
  }
    else if (object_count == 1 && distance < distance_thresh){ 
          Serial.println("Second Object Found");
          Serial.write("\n");
          object_count = object_count + 1;
    }

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.