Arduino Uno line tracer is not working properly

#define enA 9
#define enB 10
int L = A0;
int M = A1;
int R = A2;

int in1 = 7;
int in2 = 6;
int in3 = 5;
int in4 = 4;

void setup() {
  pinMode(L, INPUT);
  pinMode(R, INPUT);
  pinMode(M, INPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
}
void loop() {
  if (digitalRead(L) == HIGH && digitalRead(M) == HIGH && digitalRead(R) == HIGH) {
    //go forward
    forward();
  }
  if (digitalRead(L) == HIGH && digitalRead(M) == LOW && digitalRead(R) == LOW) { 
    //go left
    forward(); //but go left
  }
  if (digitalRead(L) == HIGH && digitalRead(M) == LOW && digitalRead(R) == HIGH) {
    //go forward
    forward();
  }
  if (digitalRead(L) == HIGH && digitalRead(M) == HIGH && digitalRead(R) == LOW) { //
    //go foward
    Bleft(); //but go forward
  }
  if (digitalRead(L) == LOW && digitalRead(M) == LOW && digitalRead(R) == HIGH) { //
    //go right
    forward(); //but go right
  }
  if (digitalRead(L) == LOW && digitalRead(M) == HIGH && digitalRead(R) == HIGH) { //
    //go forward
    delay(50);
    Bright(); //but go forward
  }
  if (digitalRead(L) == LOW && digitalRead(M) == HIGH && digitalRead(R) == LOW) {
    forward();
010
  }
  if (digitalRead(L) == LOW && digitalRead(M) == LOW && digitalRead(R) == LOW) {
    forward();
    //goback();
  }
}
void forward() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enA, 150); 
  analogWrite(enB, 158); 
}
void Bright() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  analogWrite(enA, 100);
  analogWrite(enB, 0);
}
void Bleft() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enA, 0);
  analogWrite(enB, 108); 
}
void goback() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enA, 100);
  analogWrite(enB, 100); 
}

I write code like this and connected the circuit like this.

  1. when 3 IR sensors have a value of 100, 110, 001 and 011, it works the other way around. I also checked several times that the circuit connection is also correct with three people. Is this problem unconditionally a circuit problem?
  2. If I write it according to the code above, the machine senses it as desired and makes the operation correct. However, if the machine goes along the black line and misses the black line, it has to catch it again, but once it goes off the path, it goes the wrong way. How can you solve it?
  3. Is it necessary to move backward?

I adjusted the sensitivity of the IR sensor. I narrowed down the width of the three IR sensors. I thought the machine was too fast to sense it properly, so I slowed it down. But it hasn't been resolved.

your code is incomplete

if you have two power supplies you need to join gnds in some way
(a 9V battery is not suited for motors)

That's right. So I use 4 1.5 volt batteries and 1 9V battery. I edited the code. Thank you.
I think the battery will be okay because the professor gave it to me to use it like that. Please think about the battery except for the problem.

think about the GNDs

I'm sorry I didn't upload the circuit picture properly. Unlike that circuit picture, the GND is also connected correctly, so the motor works properly. Sensing is the problem...

if you run this code:

#define enA 9
#define enB 10
int in1 = 7;
int in2 = 6;
int in3 = 5;
int in4 = 4;

void goForward() {
  digitalWrite(in2, LOW);
  digitalWrite(in4, LOW);
  digitalWrite(in1, HIGH);
  digitalWrite(in3, HIGH);
  analogWrite(enA, 150);
  analogWrite(enB, 158);
}
void goRight() {
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  digitalWrite(in1, HIGH);
  analogWrite(enA, 100);
  digitalWrite(enB, LOW);
}
void goLeft() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  digitalWrite(enA, LOW);
  analogWrite(enB, 108);
}
void goBack() {
  digitalWrite(in1, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in4, HIGH);
  analogWrite(enA, 100);
  analogWrite(enB, 100);
}
void stop() {
  digitalWrite(enA, LOW);
  digitalWrite(enB, LOW);
}

void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);


  goForward(); delay(5000);
  goRight(); delay(5000);
  goLeft(); delay(5000);
  goBack(); delay(5000);
  stop();
}


void loop() {}

do you see the motors doing the right thing ?

  • go forward for 5 secs
  • go right for 5 secs
  • go left for 5 secs
  • go back for 5 secs
  • then stop

?

Yes It works..

OK then check your sensors

this code should print 3 letters (a * for ON, a dot for off) for the state of the 3 sensors, each time there is a change

const byte leftSensorPin = A0;
const byte middleSensorPin = A1;
const byte rightSensorPin = A2;

bool leftSensorIsOn, middleSensorIsOn, rightSensorIsOn;

bool sensorsChanged() {
  static bool previousStateLeft, previousStateMidlle, previousStateRight;

  leftSensorIsOn   = digitalRead(leftSensorPin) == HIGH;
  middleSensorIsOn = digitalRead(middleSensorPin) == HIGH;
  rightSensorIsOn  = digitalRead(rightSensorPin) == HIGH;

  if ((previousStateLeft != leftSensorIsOn) || (previousStateMidlle != middleSensorIsOn) || (previousStateRight != rightSensorIsOn)) {
    previousStateLeft = leftSensorIsOn;
    previousStateMidlle = middleSensorIsOn;
    previousStateRight = rightSensorIsOn;
    return true;
  }
  return false;
}

void setup() {
  pinMode(leftSensorPin, INPUT);
  pinMode(middleSensorPin, INPUT);
  pinMode(rightSensorPin, INPUT);
}

void loop() {
  if (sensorsChanged()) {
    Serial.write(leftSensorIsOn ? '*' : '.');
    Serial.write(middleSensorIsOn ? '*' : '.');
    Serial.println(rightSensorIsOn ? '*' : '.');
  }
}

does this work?

Yes It works. Thank you for helping me!! :smile:

What's wrong with sensors and motors when they work properly? If there's anything weird in a few days, can I come ask you?

so now it's a matter of putting both codes together

give it a try and post your work

you can of course come back with questions

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.