How to make a sensor working once only when arduino is restarted?

Hello Basically what i want to do is :

When arduino board is started a motor turns to the right until an optical sensor detects an object, and once the sensor detects an object it repeat the loop.
So I want to sensor only works once in the beginning of every time Arduino restarted.
Any comment or advise would be really helpful. thanks

const int tcrtPin = 2;     // connected to the TCRT5000 C pin
const int led =  13;      // the number of the LED pin
 
// variables will change:
int tcrtState = 0;         // variable for reading the TCRT5000 status
 
void setup() {
  // initialize the LED pin as an output:
  pinMode(led, OUTPUT);     
   pinMode(6, OUTPUT);       // Motor Dir
   pinMode(5, OUTPUT); 
   pinMode(7, OUTPUT);
  // initialize the tcrt5000 pin as an input, and turn on the internal pullup resistors:
  pinMode(tcrtPin, INPUT); 
  digitalWrite(tcrtPin, HIGH);  
  digitalWrite(7, HIGH);
}
 
void loop(){
  // read the state of the tcrt5000:
  tcrtState = digitalRead(tcrtPin);
 
  // check if the tcrt5000 sensor detects something.
  // if it is, the tcrtState is LOW:
  if (tcrtState == LOW) {     
    // turn LED on:    
    digitalWrite(7, HIGH);
    digitalWrite(led, HIGH);  
    digitalWrite(6,HIGH); 
      digitalWrite(5,HIGH); 
      delayMicroseconds(500); 
      digitalWrite(5,LOW);
      delayMicroseconds(500); 
  } 
  else {
        digitalWrite(6,LOW); 
      digitalWrite(5,HIGH); 
      delayMicroseconds(500); 
      digitalWrite(5,LOW);
      delayMicroseconds(500); 
  }
}

and this is the loop I want to repeat

     digitalWrite(6,LOW); 
      digitalWrite(5,HIGH); 
      delayMicroseconds(500); 
      digitalWrite(5,LOW);
      delayMicroseconds(500);

Can you just put the code in setup() as it only gets run once per arduino restart and leave loop() empty?

Put the code you only want to run once in the setup() function.


Rob

Yes I tried but that's not working. maybe it shouldn't be in if statement?

Yes I tried but that's not working.

..and I didn't post the code.

I am sorry.
This is the code that i tried.

const int tcrtPin = 2;     // connected to the TCRT5000 C pin
const int led =  13;      // the number of the LED pin
 
// variables will change:
int tcrtState = 0;         // variable for reading the TCRT5000 status
 
void setup() {
  // initialize the LED pin as an output:
  pinMode(led, OUTPUT);     
   pinMode(6, OUTPUT);       // Motor Dir
   pinMode(5, OUTPUT); 
   pinMode(7, OUTPUT);
  // initialize the tcrt5000 pin as an input, and turn on the internal pullup resistors:
  pinMode(tcrtPin, INPUT); 
  digitalWrite(tcrtPin, HIGH);  
  digitalWrite(7, HIGH);
    // read the state of the tcrt5000:
  tcrtState = digitalRead(tcrtPin);
 
  // check if the tcrt5000 sensor detects something.
  // if it is, the tcrtState is LOW:
  if (tcrtState == LOW) {     
    // turn LED on:    
    digitalWrite(7, HIGH);
    digitalWrite(led, HIGH);  
    digitalWrite(6,HIGH); 
      digitalWrite(5,HIGH); 
      delayMicroseconds(500); 
      digitalWrite(5,LOW);
      delayMicroseconds(500); 
  } 
}
 
void loop(){

 
    // turn LED off:
      digitalWrite(7, HIGH);
    digitalWrite(led, HIGH);  
    digitalWrite(6,LOW); 
      digitalWrite(5,HIGH); 
      delayMicroseconds(500); 
      digitalWrite(5,LOW);
      delayMicroseconds(500); 

}

This is the code that i tried.

And the results?

but that's not working.

Fire it. Hire some code that will work. Of course, you'll have to explain to it what to do, and then you'll need to observe that it is working, and doing what you told it do.