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);