// motors
#define MOT_A_ENB 5 // motor A enable and PWM/speed control ~
#define MOT_B_ENB 6 // motor B enable and PWM/speed control ~
#define MOT_A_FWD 10 // motor A forward ->
#define MOT_A_REV 11 // motor A <- reverse
#define MOT_B_FWD 12 // motor B forward ->
#define MOT_B_REV 13 // motor B <- reverse
#define LOW_SPEED 63 // PWM speed 0 - 255 ___
#define MED_SPEED 127 // PWM speed 0 - 255 ---
#define HI_SPEED 255 // PWM speed 0 - 255 ^^^
#define SPEED LOW_SPEED // set to low, med, hi
// line detectors
int Line_Detect_Right = 2; // Digital I/O pin means 1 or 0
int Line_Detect_Left = 3;
int val_Right = 0;
int val_Center = 0;
int val_Left = 0;
void setup() {
Serial.begin(9600); // configure Serial Monitor for output
pinMode(Line_Detect_Right, INPUT); // line detect right
pinMode(Line_Detect_Left, INPUT); // line detect left
pinMode(MOT_A_FWD, OUTPUT); // pin 10, motor A (usually IN3 with DC motors)
pinMode(MOT_A_REV, OUTPUT); // pin 11, motor A (usually IN4 with DC motors)
pinMode(MOT_B_FWD, OUTPUT); // pin 12, motor B (usually IN1 with DC motors)
pinMode(MOT_B_REV, OUTPUT); // pin 13, motor B (usually IN2 with DC motors)
pinMode(MOT_A_ENB, OUTPUT); // pin 4, motor A enable
pinMode(MOT_B_ENB, OUTPUT); // pin 5, motro B enable
digitalWrite(MOT_A_ENB, HIGH); // HIGH might be OFF
digitalWrite(MOT_B_ENB, HIGH); // HIGH might be OFF
Serial.println("########################################");
Serial.println("# #");
Serial.println("# Arduino Run #");
Serial.println("# #");
Serial.println("########################################");
Serial.println(" --> Initial Pins");
}
void loop() {
val_Right = digitalRead(Line_Detect_Right); // changed to digitalRead()
val_Left = digitalRead(Line_Detect_Left); // changed to digitalRead()
Serial.print("Center = ");
Serial.print(val_Center);
Serial.print(" --> R = ");
Serial.print(val_Right);
Serial.print(" --> L = ");
Serial.print(val_Left);
if (val_Right == 1 && val_Left == 1) { // BOTH sensors detect black line
Serial.print(" - Detect = L & R");
Stop();
delay(100);
}
else if (val_Right == 0 && val_Left == 0) { // BOTH sensors DO NOT detect a line
Serial.print(" - Detect = CENTER");
Forward();
delay(100);
}
else if (val_Right == 1 && val_Left == 0) { // RIGHT sensor detects black line
Serial.print(" - Detect = Right");
Turn_Right();
delay(100);
}
else if (val_Right == 0 && val_Left == 1) { // LEFT sensor detects black line
Serial.print(" - Detect = Left");
Turn_Left();
delay(100);
}
Serial.println("\n");
delay(160);
}
void Stop() {
Serial.print(" --> Direction: STOP");
digitalWrite(MOT_A_FWD, LOW); // motor A
digitalWrite(MOT_A_REV, LOW);
digitalWrite(MOT_B_FWD, LOW); // motor B is 180 degrees from motor A
digitalWrite(MOT_B_REV, LOW);
digitalWrite(MOT_A_ENB, LOW); // DISABLE motor A
digitalWrite(MOT_B_ENB, LOW); // DISABLE motor B
}
void Forward() {
Serial.print(" --> Direction: Forward");
digitalWrite(MOT_A_FWD, HIGH); // motor A
digitalWrite(MOT_A_REV, LOW);
digitalWrite(MOT_B_FWD, HIGH); // motor B is 180 degrees from motor A
digitalWrite(MOT_B_REV, LOW);
analogWrite(MOT_A_ENB, SPEED); // PWM/speed for motor A
analogWrite(MOT_B_ENB, SPEED); // PWM/speed for motor B
}
void Backward() {
Serial.print(" --> Direction: Backward");
digitalWrite(MOT_A_FWD, HIGH);
digitalWrite(MOT_A_REV, LOW);
digitalWrite(MOT_B_FWD, LOW);
digitalWrite(MOT_B_REV, HIGH);
analogWrite(MOT_A_ENB, SPEED);
analogWrite(MOT_B_ENB, SPEED);
}
void Turn_Right() {
Serial.print(" --> Direction: Turn Left");
digitalWrite(MOT_A_FWD, LOW); // motor A fowrward
digitalWrite(MOT_A_REV, HIGH);
digitalWrite(MOT_B_FWD, HIGH); // motor B reverse
digitalWrite(MOT_B_REV, LOW);
analogWrite(MOT_A_ENB, SPEED);
analogWrite(MOT_B_ENB, SPEED);
}
void Turn_Left() {
Serial.print(" --> Direction: Turn Right");
digitalWrite(MOT_A_FWD, HIGH); // motor A reverse
digitalWrite(MOT_A_REV, LOW);
digitalWrite(MOT_B_FWD, LOW); // motor B forward
digitalWrite(MOT_B_REV, HIGH);
analogWrite(MOT_A_ENB, SPEED);
analogWrite(MOT_B_ENB, SPEED);
}
ENA
ENB
FWD
FWD
REV
REV