Help with line follower code

Im using an arduino mega and this is my code im trying to make it count how many times i cross over lines but the code i wrote doesn't work in progress i have 2 ir sensors the sensors activate but the counter doesnt seem to count using l293d adn l324 for sensors and motor.

int sensorCount = 0;
int sensorPinLeft = A1;
int sensorPinRight = A2;
int motorSpeed = 255;

void setup() {
  // put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);



}
void loop() {
  int VoltageValueLeft = analogRead(sensorPinLeft);
  int VoltageValueRight = analogRead(sensorPinRight);

  // Check if both sensors are activated
  if (VoltageValueRight > 500 && VoltageValueLeft > 500) {
    sensorCount++;
      if (sensorCount < 2) {
        moveForward();
      } else {
        stopRobot();
    }
  }
  // Both sensors off the line
  else if (VoltageValueRight < 500 && VoltageValueLeft < 500) {
    moveForward(); 
  }
  // Right sensor off the line
  else if (VoltageValueRight < 500 && VoltageValueLeft > 500) {
    turnRight();
  }
  // Left sensor off the line
  else if (VoltageValueRight > 500 && VoltageValueLeft < 500) {
    turnLeft();
  }
}

// Function to move forward
void moveForward() {
  digitalWrite(13, HIGH);
  digitalWrite(12, LOW);
  analogWrite(9, motorSpeed);

  digitalWrite(11, HIGH);
  digitalWrite(10, LOW);
  analogWrite(8, motorSpeed);
}

// Function to turn left
void turnLeft() {
  digitalWrite(13, HIGH);
  digitalWrite(12, LOW);
  analogWrite(9, motorSpeed);

  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  analogWrite(8, motorSpeed);
}

// Function to turn right
void turnRight() {
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  analogWrite(9, motorSpeed);

  digitalWrite(11, HIGH);
  digitalWrite(10, LOW);
  analogWrite(8, motorSpeed);
}

// Function to stop the robot
void stopRobot() {
  // Stop both motors by setting all motor control pins to LOW
  digitalWrite(13, LOW); // Right motor forward
  digitalWrite(12, LOW); // Right motor backward
  digitalWrite(11, LOW); // Right motor enable
  digitalWrite(10, LOW); // Left motor forward
  digitalWrite(9, LOW);  // Left motor backward
  digitalWrite(8, LOW);  // Left motor enable
}


How do you power the motor and the board?
You might have restarts due to low voltage situation.
Add a led blink after setup to catch this issue.
How do you know your sensors work?

using a powerbank it has 5v=3a/9v=2a/12v/1.5a 22.5W max port
and i have a led on top of the ir sensor so when it senses it lights up
you might be on to something with low voltage ill try it again

You need to check whether the sensor signal is caught by the controller...
(Add a Serial.println(A1); in your code).
You also need to know if the signal ever reaches 500...
Or ever goes below 500...

i've seen my count++ part and serialprinted it it counts way too much when both sensors are detecting the correct amount 500> its counting around 10 per secondish

Cross over the lines laterally (side to side) while steering?

You should not be looking for an analog value of sensorPinLeft/Right. The sensor is over the line or not over the line. The sensor will never be 33.3% (an arbitrary, analog value) over the line. You should be looking for digital "line/no line" values and count those changes. If you count "left" versus "right" you might see how the motors are "pulling" to one side or the other.

When the sensor is "over" the line, disable counting for that sensor until the state of that sensor changes to "not over" the line.

Another option is PulseIn , when "value" is greater than a certain amount increment count.

https://docs.arduino.cc/language-reference/en/functions/advanced-io/pulseIn/

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