Project 15: Arduino Line Follower Robot
Main Ideas:
Circuit
- L298 Dual Motor Driver
- 2 DC motors
- Arduino UNO
- 4CH Channel Infrared Tracing Module
- Two 9 Volt battery to provide power to L298 & Arduino.
Code
/* https://arduino4everyones.blogspot.com/ Project 15:
Arduino Line Follower Robot */
//Uno PWM pins 3, 5, 6, 9, 10, and 11
//New Motors Pins //Motor A
Right Side on side of 3 & 4 Sensor #define enA 5 //PWM Orang enable A #define in4 3 //Yellow IN4 #define in3 4 //Green IN3
//Motor B
Left Side on side of 1 & 2 Sensor #define enB 6 //PWM Gray Enable B #define in1 7 // blue IN2 #define in2 8 // maron IN1
int speedMotorA; // control the speed of New motor
-right side int speedMotorB;// control the speed of New motor
-left side
float timeToMoveBack5CM = 300; float timeToTrunBack360 = 1750;
#define Sensor1Pin 9 #define Sensor2Pin 10 #define Sensor3Pin 11 #define Sensor4Pin 12
int Sensor1 = 0; int Sensor2 = 0; int Sensor3 = 0; int Sensor4 = 0;
void setup() { //
initialize serial communication at 9600 bits per second:
Serial.begin(9600); //
Serial.println("start"); // put your
setup code here, to run once:
//Set Motor
A left side pins as output so we can contol the motor pinMode(enB,
OUTPUT); pinMode(in1,
OUTPUT); pinMode(in2,
OUTPUT);
//Motor B
New right side pins as output so we can contol the motor pinMode(enA,
OUTPUT); pinMode(in3,
OUTPUT); pinMode(in4,
OUTPUT);
//set
initial speed for the motors speedMotorA
= 190; // control the speed of motor -left side speedMotorB
= 190; // control the speed of motor -right side
pinMode
(Sensor1Pin, INPUT); pinMode
(Sensor2Pin, INPUT); pinMode
(Sensor3Pin, INPUT); pinMode
(Sensor4Pin, INPUT); //
TunBackword360(); //
stopmove(); }
void loop() { // put your
main code here, to run repeatedly:
//MoveLogic();
//moveForward(); //moveBackword5CM();
//TrunRight5CM();
//moveForwardone();
//moveForwardReduceSpeed();
//moveForward(); //
read_sensors();
//TrunLefteduceSpeed();
//TrunRighteduceSpeed();
//TunBackword360(); moveLogic(); }
void moveLogic() {
read_sensors(); //Read the
Sensor if HIGH (BLACK Line) or LOW (WHITE Line) if (Sensor4
== LOW && Sensor3 == LOW && Sensor2 == LOW && Sensor1
== LOW) {
stopmove();
moveBackword5CM();
Serial.println("stopmove :"); } //Read the
Sensor if HIGH (BLACK Line) or LOW (WHITE Line) if (Sensor4
== HIGH && Sensor3 == HIGH && Sensor2 == HIGH &&
Sensor1 == HIGH) {
TunBackword360();
stopmove();
Serial.println("stopmove :"); }
if (Sensor4
== LOW && Sensor3 == HIGH && Sensor2 == HIGH &&
Sensor1 == LOW) {
moveForwardReduceSpeed();
Serial.println("moveForwardReduceSpeed :"); }
//trun Left //Read the
Sensor if HIGH (BLACK Line) or LOW (WHITE Line) if (Sensor4
== HIGH && Sensor3 == LOW && Sensor2 == LOW &&
Sensor1 == LOW) {
TrunLefteduceSpeed();
Serial.println("TrunLefteduceSpeed :"); } if (Sensor4
== HIGH && Sensor3 == HIGH && Sensor2 == LOW &&
Sensor1 == LOW) {
//TrunLefteduceSpeed();
moveForwardReduceSpeed();
Serial.println("moveForwardReduceSpeed(); :"); }
if (Sensor4
== HIGH && Sensor3 == HIGH && Sensor2 == HIGH &&
Sensor1 == LOW) {
TrunLefteduceSpeed();
Serial.println("TrunLefteduceSpeed :"); } if (Sensor4
== LOW && Sensor3 == HIGH && Sensor2 == LOW &&
Sensor1 == LOW) { //TrunLefteduceSpeed();
moveForwardReduceSpeed();
Serial.println("moveForwardReduceSpeed(); :"); }
//
TrunRighteduceSpeed if (Sensor4
== LOW && Sensor3 == HIGH && Sensor2 == HIGH &&
Sensor1 == HIGH) {
TrunRighteduceSpeed();
Serial.println("TrunRighteduceSpeed :"); } if (Sensor4
== LOW && Sensor3 == LOW && Sensor2 == HIGH &&
Sensor1 == HIGH) { //
TrunRighteduceSpeed();
moveForwardReduceSpeed();
Serial.println(" moveForwardReduceSpeed(); :"); }
if (Sensor4
== LOW && Sensor3 == LOW && Sensor2 == HIGH &&
Sensor1 == LOW) {
//TrunRighteduceSpeed();
moveForwardReduceSpeed();
Serial.println("moveForwardReduceSpeed(); :"); } if (Sensor4
== LOW && Sensor3 == LOW && Sensor2 == LOW && Sensor1
== HIGH) {
TrunRighteduceSpeed();
Serial.println("TrunRighteduceSpeed :"); }
} void read_sensors() //Read the Sensor if HIGH (BLACK Line) or LOW (WHITE
Line) { Sensor1 =
digitalRead(Sensor1Pin); Sensor2 =
digitalRead(Sensor2Pin); Sensor3 =
digitalRead(Sensor3Pin); Sensor4 =
digitalRead(Sensor4Pin);
Serial.print("Sensor1Pin :");
Serial.println(Sensor1);
Serial.print("Sensor2Pin :");
Serial.println(Sensor2);
Serial.print("Sensor3Pin :");
Serial.println(Sensor3); Serial.print("Sensor4Pin
:");
Serial.println(Sensor4);
//delay(1000); }
void ResetSpeedMotors() { speedMotorA
= 220; // control the speed of motor -rigt side speedMotorB
= 220; // control the speed of motor -left side }
void ReduceSpeed() { //set speed speedMotorA
= 120; // control the speed of motor -rigt side speedMotorB
= 120; // control the speed of motor -left side }
void moveForwardone() {
ResetSpeedMotors(); // run motor
B back // analogWrite(enB, speedMotorB); // digitalWrite(in1, HIGH); // digitalWrite(in2, LOW);
// run motor
B forward // analogWrite(enB, speedMotorB); // digitalWrite(in1, LOW); // digitalWrite(in2, HIGH);
// run motor
A back // analogWrite(enA, speedMotorA); // digitalWrite(in3, LOW); // digitalWrite(in4, HIGH);
// run motor
A forward // analogWrite(enA, speedMotorA); // digitalWrite(in3, HIGH); // digitalWrite(in4, LOW);
}
void moveBackword5CM() {
ReduceSpeed(); unsigned
long moveBackStartTime = millis(); // save the time that we started moveback while
((millis() - moveBackStartTime) < (timeToMoveBack5CM) ) // stay in this
loop until timeToMoveBack10CM (1.1 seconds) has elapsed { // run
motor A back analogWrite(enA,
speedMotorA);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// run
motor B back
analogWrite(enB, speedMotorB);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
} }
void TunBackword360() {
ResetSpeedMotors(); unsigned
long moveBackStartTime = millis(); // save the time that we started moveback while
((millis() - moveBackStartTime) < (timeToTrunBack360) ) // stay in this
loop until timeToMoveBack10CM (1.1 seconds) has elapsed {
//Motor B Left Side on side of
1 & 2 Sensor //forward
analogWrite(enB, speedMotorB);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
//Motor A Right Side on side of
3 & 4 Sensor //Back
analogWrite(enA, speedMotorA);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
}
void moveForward() {
ResetSpeedMotors(); //Motor B Left Side on side of 1 & 2 Sensor
analogWrite(enB, speedMotorB);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH); //Motor A Right Side on side of 3 & 4 Sensor
analogWrite(enA, speedMotorA);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW); } void moveForwardReduceSpeed() {
ReduceSpeed(); //Motor B Left Side on side of 1 & 2 Sensor
analogWrite(enB, speedMotorB);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH); //Motor A Right Side on side of 3 & 4 Sensor
analogWrite(enA, speedMotorA);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW); }
void TrunLefteduceSpeed() {
ReduceSpeed(); //Motor B Left Side on side of 1 & 2 Sensor //forward
analogWrite(enB, speedMotorB);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH); //Motor A Right Side on side of 3 & 4 Sensor //Back
analogWrite(enA, speedMotorA);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH); }
void TrunRighteduceSpeed() {
ReduceSpeed(); //Motor B Left Side on side of 1 & 2 Sensor //back
analogWrite(enB, speedMotorB);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW); //Motor A Right Side on side of 3 & 4 Sensor //forward
analogWrite(enA, speedMotorA);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW); } void stopmove() {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(enB, 0);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enA, 0);
delay (100); }
|
No comments:
Post a Comment