Gap Crossing Robot

Hi GalanAleni

When the distance is higher; led pin 12 turns on instead. However I only want this to occur once;

Distance being higher means the sensor is over the gap? So that is when you want to drop the bridge?

I would keep the code in loop() because, I assume, you need to repeatedly check the sensor as your robot moves towards the bridge.

I would add a boolean variable which you set when you reach the bridge. Something like this ...

void loop()
{
    static boolean atBridge = false;  // Set to false first time in void loop()
  
    if (!atBridge) // If not yet at bridge, check sensor
    {
        distance = Dist.getDistanceCentimeter();
        Serial.print("\nDistance in centimers: ");
        Serial.print(distance); 
        delay(500); //make it readable
        if (distance < 10)
        {
            digitalWrite(13, HIGH);
            digitalWrite(12, LOW);
        }
       else    // Distance >= 10 so now at bridge
       {
            atBridge = true;  //  Next time round loop(), distance measuring will not take place
            digitalWrite(13, LOW);
            delay (2000);
            digitalWrite(12, HIGH);
            delay (2000);
       }
    }
    
    if (atBridge)
    {
         // Code to drop bridge
    }

}

Hope this helps

Ray