#define LT_R !digitalRead(10)
#define LT_M !digitalRead(4)
#define LT_L !digitalRead(2)
#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11
#define carSpeed 150
void forward(){ //car drives forward
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("vorwärts");
}
void back(){ //car drives backwards, dont have a use yet.
analogWrite(ENA, carSpeed); //But i thought about it searching for the line by driving backwards/forwards for a short moment
analogWrite(ENB, carSpeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("rückwärts");
}
void left(){ //car drives left
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("left");
}
void right(){ //car drives right
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("right");
}
void brake(){ //car stopping
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
Serial.println("stop");
}
void setup(){ //setup for the line detection
pinMode(LT_R,INPUT);
pinMode(LT_M,INPUT);
pinMode(LT_L,INPUT);
}
void loop() { //Loop that makes the car follow the line
if(LT_M){
forward();
}
else if(LT_L){ //car drives left
left();
while (LT_L);
}
else if (LT_R){ //car drives right
right();
while(LT_R);
}
else { //car brakes if it cant detect the line,
brake(); //currently it stops if it doesnt detect it for even the slightest moment
} //This makes the car unable to follow the line properly
} //i want it to stop only if it doesnt detect it for longer than lets say 0.5 - 1 second
Alright this is the Code, its purpose is to make the car follow a black line using 3 sensors at the front. It works decently following the Line but now i wan't it to stop once it doesnt detect it.
I used a simple else at the end that makes the car stop once it doesn't detect the line but this work too well as the car stop even at the slightest.
I have thought about potetntially adding a timer that runs down once it doesnt detect the line and then stops the car. However i am pretty new to programming and my attempts failed wich involved using integers (int i = 0; i >=500; i++) but that didnt work for me.
At one point i had it happen where the car just spins to the right once it goes off-track but im an idiot and accidentally deleted that part..
i would appreciate any help