In three short words: I'm a beginner. I have a project due the day after tomorrow. I know it's supposed to be a really easy thing, but somehow I've been working on it for more than a week now and there's always some problem.
Here's the project: I have to make a car with Arduino Uno that will avoid obstacles and follow a black line.
They didn't say much about how we should assemble the car, but I googled at least a hundred times anything I had questions about and read as much as I could and watched as many videos on Youtube as I could. But somehow I still can't get it to work correctly.
Now, I'm not able to attach the photo cause it tells me the entity's too large, but I'm attaching the code I wrote for it. I know I'm asking a lot, but at this point I'm kinda desperate. I really hope someone can help me figure stuff out.
void loop() {
// clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// calculating the distance
distance = duration * 0.034/2;
Serial.print("Distance: ");
Serial.println(distance);
int valR = digitalRead(infraR);
int valL = digitalRead(infraL);
int valC = digitalRead(infraC);
// if the right IR sensor detects black and other two don't
// or if the right and the middle one detect black, turn right
if (valL == 1 && valC == 1 && valR == 0 || valL == 1 && valC == 0 && valR == 0) {
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
// if the left IR sensor detects black and other two don't
// or if the left and the middle one detect black, turn left
if (valL == 0 && valC == 1 && valR == 1 || valL == 0 && valC == 0 && valR == 1) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
// if the middle IR sensor detects black and other two don't
// or if all three detect black, go forward
if (valL == 1 && valC == 0 && valR == 1 || valL == 0 && valC == 0 && valR == 0) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
// car moves forward while the distance between it and an obstacle is more than 25cm
// and goes backwards and then turns left if the distance is less than or equal to 25cm
if (distance > 25) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
else {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(500);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
}
P.S. English isn't my first language, so if you could please tell me stuff as simply as possible I will be forever grateful. Also, ask me anything you need to know about this project. Thank you in advance.
P.P.S. the battery is 9V.