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
}