Ultrasonic sensor stopwatch

Ultrasonic sensor stopwatch

Hello,I keep getting error of this program. Someone can help me? I’m new on this, thank you so much.

/* HC-SR04 Ultrasonic Sensor
This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return.  The length of the returning pulse is proportional to
the distance of the object from the sensor.
The circuit:
* Vcc connection of the HC-SR04 attached to +5V
* GND connection of the HC-SR04 attached to ground
* Echo connection of the HC-SR04 attached to digital pin 3
* Trig connection of the HC-SR04 attached to digital pin 2
*/
// this constant won't change.  It's the pin number
// of the sensor's output:
const int TrigPin = 4;
const int EchoPin1 = 5;
const int EchoPin2 = 6;
const int buttonPin = 7;     // the number of the pushbutton pin
int buttonState;         // variable for reading the pushbutton status
int rank;
long time;

void setup()
{
// initialize serial communication:
 Serial.begin(9600);
 pinMode(buttonPin, INPUT);     
}

void loop()
{
// read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);

 if (buttonState == LOW) {
  
 }
 else{
    time =  millis();
// establish variables for duration of the ping,
// and the distance result in centimeters:
   long duration1, cm;
// The HC-SR04 is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
   pinMode(TrigPin, OUTPUT);
   digitalWrite(TrigPin, LOW);
   delayMicroseconds(2);
   digitalWrite(TrigPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(TrigPin, LOW);

// The same pin is used to read the signal from the HC-SR04: 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(EchoPin1, INPUT);
   pinMode(EchoPin2, INPUT);
   duration1 = pulseIn(EchoPin1, HIGH);
   duration2 = pulseIn(EchoPin2, HIGH);
// convert the time into a distance
   cm = microsecondsToCentimeters1(duration1);
   cm = microsecondsToCentimeters2(duration2);

   if(cm<30){
    rank++;
    Serial.println();
    Serial.print("Rank:");
   if(rank==1){
    Serial.print("First");
    Serial.println();}
   else if(rank==2){
    Serial.print("Second");
    Serial.println();}
   else if(rank==3){
    Serial.print("Thrid");
    Serial.println();}
   else if(rank==4){
    Serial.print("Forth");
    Serial.println();}
   else if(rank==5){
    Serial.print("Fifth");
    Serial.println();}
   else if(rank==6){
    Serial.print("Sixth");
    Serial.println();}
   else if(rank==7){
    Serial.print("Seventh");
    Serial.println();}
   else if(rank==8){
    Serial.print("Nighth");
    Serial.println();}
   else if(rank==9){
    Serial.print("Ninth");
    Serial.println();}
    
   else{
   Serial.print(rank);
   Serial.println();}
     
   Serial.print("Time: ");
   Serial.print(time/1000);
   Serial.print(".");
   Serial.print(time/100-time/1000*10);
   Serial.print(time/10-time/100*10);
   Serial.print("s");
   Serial.println();
   
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   wait:
   if(cm<30){
   goto wait;
      }
    }
  delay(60);
  }
}
 long microsecondsToCentimeters1(long microseconds1){
// 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 microseconds1 / 29 / 2;
}
 long microsecondsToCentimeters2(long microseconds2){
// 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 microseconds2 / 29 / 2;
}

I keep getting error of this program

So?

Someone can help me?

Sure. Tell us what the error is.

   goto wait;

WTF?

Error: sketch_oct17a.cpp: In function ‘void loop()’: sketch_oct17a.cpp:63:4: error: ‘duration2’ was not declared in this scope

I'm new on this and thank your reply.

sketch_oct17a.cpp:63:4: error: ‘duration2’ was not declared in this scope

So declare it.
long duration1, duration2, cm;

The problem has been fixed. Thank you so much. :)

"Thrid"

==> Third

For all those if statements you can also use a switch case, see - www.arduino.cc/en/Reference/SwitchCase -