#include <AFMotor.h>
#define cambien1 A2
#define cambien2 A3
#define cambien3 A4
#define cambien4 A5
AF_DCMotor motorleft1(1, MOTOR12_1KHZ);
AF_DCMotor motorleft2(4, MOTOR12_1KHZ);
AF_DCMotor motorright1(2, MOTOR34_1KHZ);
AF_DCMotor motorright2(3, MOTOR34_1KHZ);
int crossCount = 0;
void moveForward();
void turnRight();
void turnHardRight();
void turnHardLeft();
void turnMediumLeft();
void turnSlightLeft();
void turnSlightRight();
void turnMediumRight();
void stopMoving();
void run_line();
void setup() {
pinMode(cambien1, INPUT);
pinMode(cambien2, INPUT);
pinMode(cambien3, INPUT);
pinMode(cambien4, INPUT);
Serial.begin(9600);
}
void loop() {
run_line();
}
void run_line() {
int leftOuter = analogRead(cambien1) > 200 ? 1 : 0;
int leftInner = analogRead(cambien2) > 200 ? 1 : 0;
int rightInner = analogRead(cambien3) > 200 ? 1 : 0;
int rightOuter = analogRead(cambien4) > 200 ? 1 : 0;
if (leftOuter == 0 && leftInner == 1 && rightInner == 1 && rightOuter == 1) {
crossCount++;
if (crossCount = 1) {
moveForward();
delay(1000);
} else if (crossCount = 2) {
moveForward();
delay(1000);
} if (crossCount = 3) {
moveForward();
delay(1000);
} else if (crossCount = 4) {
turnHardRight();
delay(2000);
} else if (crossCount = 5) {
turnHardRight();
delay(2000);
}
} else if (leftOuter == 0 && leftInner == 1 && rightInner == 1 && rightOuter == 0) {
moveForward();
delay(1000);
} else if (leftOuter == 1 && leftInner == 0 && rightInner == 0 && rightOuter == 0) {
turnMediumLeft();
delay(1000);
} else if (leftOuter == 1 && leftInner == 1 && rightInner == 0 && rightOuter == 0) {
turnSlightLeft();
delay(1000);
} else if (leftOuter == 1 && leftInner == 1 && rightInner == 1 && rightOuter == 0) {
turnHardLeft();
delay(2000);
} else if (leftOuter == 1 && leftInner == 1 && rightInner == 1 && rightOuter == 1) {
turnHardLeft();
delay(2000);
} else if (leftOuter == 0 && leftInner == 0 && rightInner == 1 && rightOuter == 1) {
turnSlightRight();
delay(500);
} else if (leftOuter == 0 && leftInner == 0 && rightInner == 0 && rightOuter == 1) {
turnMediumRight();
delay(500);
} else if (leftOuter == 1 && leftInner == 0 && rightInner == 1 && rightOuter == 1) {
turnSlightLeft();
} else if (leftOuter == 1 && leftInner == 1 && rightInner == 0 && rightOuter == 1) {
turnSlightRight();
stopMoving();
} else {
moveForward();
}
}
void moveForward() {
motorleft1.setSpeed(150);
motorleft2.setSpeed(150);
motorright1.setSpeed(150);
motorright2.setSpeed(150);
motorleft1.run(FORWARD);
motorleft2.run(FORWARD);
motorright1.run(FORWARD);
motorright2.run(FORWARD);
}
void turnHardRight() {
motorleft1.setSpeed(200);
motorleft2.setSpeed(0);
motorright1.setSpeed(0);
motorright2.setSpeed(0);
motorleft1.run(FORWARD);
motorleft2.run(RELEASE);
motorright1.run(RELEASE);
motorright2.run(RELEASE);
}
void turnHardLeft() {
motorleft1.setSpeed(0);
motorleft2.setSpeed(0);
motorright1.setSpeed(200);
motorright2.setSpeed(0);
motorleft1.run(RELEASE);
motorleft2.run(RELEASE);
motorright1.run(FORWARD);
motorright2.run(RELEASE);
}
void turnMediumLeft() {
motorleft1.setSpeed(100);
motorleft2.setSpeed(100);
motorright1.setSpeed(50);
motorright2.setSpeed(50);
motorleft1.run(FORWARD);
motorleft2.run(FORWARD);
motorright1.run(FORWARD);
motorright2.run(FORWARD);
}
void turnSlightLeft() {
motorleft1.setSpeed(100);
motorleft2.setSpeed(100);
motorright1.setSpeed(80);
motorright2.setSpeed(80);
motorleft1.run(FORWARD);
motorleft2.run(FORWARD);
motorright1.run(FORWARD);
motorright2.run(FORWARD);
}
void turnSlightRight() {
motorleft1.setSpeed(80);
motorleft2.setSpeed(80);
motorright1.setSpeed(100);
motorright2.setSpeed(100);
motorleft1.run(FORWARD);
motorleft2.run(FORWARD);
motorright1.run(FORWARD);
motorright2.run(FORWARD);
}
void turnMediumRight() {
motorleft1.setSpeed(50);
motorleft2.setSpeed(50);
motorright1.setSpeed(100);
motorright2.setSpeed(100);
motorleft1.run(FORWARD);
motorleft2.run(FORWARD);
motorright1.run(FORWARD);
motorright2.run(FORWARD);
}
void stopMoving()
{
motorleft1.setSpeed(0);
motorleft2.setSpeed(0);
motorright1.setSpeed(0);
motorright2.setSpeed(0);
motorleft1.run(RELEASE);
motorleft2.run(RELEASE);
motorright1.run(RELEASE);
motorright2.run(RELEASE);
}
Welcome to the forum
if (crossCount = 1)
= to set a variable to a value
== to compare values
Do you see the problem ?
all if are set value not compare.
Why did you assign motors 1 and 4 to MOTOR12 and motors 2 and 3 to MOTOR34?
1 Like
????
IF statements are for making comparisons, which means using the comparison (is equals to?) '==' symbol rather than the assign value (make equal to) symbol '=' . You also seem to have a missing 'else' in the third 'if' statement.
@BitSeeker
This code not is mine. Its OP code.
Hi, @2harry-potter_23
Welcome to the forum.
Thanks for using code tags.
This will help with your if statement.
https://docs.arduino.cc/language-reference/en/structure/control-structure/if/
Tom..