Hello. i have a doubt on the given code
#include <AFMotor.h>
#include <Servo.h>
#include <SoftwareSerial.h> // TX RX software library for bluetooth
//define
#define leftir A5
#define rightir A4
//define
//
#define trigPin2 9
#define echoPin2 10
//
AF_DCMotor motor1(1); // Motor 1 connected to M1 port of L293D
AF_DCMotor motor2(2); // Motor 2 connected to M2 port of L293D
AF_DCMotor motor3(3); // Motor 3 connected to M3 port of L293D
AF_DCMotor motor4(4); // Motor 4 connected to M4 port of L293D
void setup() {
//ir
pinMode(leftir,INPUT);
pinMode(rightir,INPUT);
//ir
//ultrasonic sensor
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
//ultrasonic sensor
//
Serial.begin(9600); // Initialize serial communication
motor1.setSpeed(255); // Set motor speed (0-255)
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
}
void loop() {
if (Serial.available() > 0) {
char command = Serial.read();
executeCommand(command);
}
//ultrasonc sensor
int duration, distance; // The Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
//new ultrasonic
digitalWrite(trigPin2, HIGH); // The output pulse with 1ms width on trigPin
delay(1);
digitalWrite(trigPin2, LOW);
duration = pulseIn(echoPin2, HIGH); // Measures the pulse input in the echo pin
distance = (duration/2) / 29.1; // Distance is half the duration devided by 29.1 (from datasheet)
// if the distance is less than 0.5 meter and more than 0 (0 or less means over range)
if (distance <= 10 && distance >= 0) {
stopMoving();
Serial.println("EOAB");
} else {
// Nothing
}
// Waits 50 milliseconds
delay(50);
//new ultra
//ultrasonic sensor
}
void executeCommand(char command) {
switch (command) {
case 'F':
moveForward();
break;
case 'B':
moveBackward();
break;
case 'L':
turnLeft();
break;
case 'R':
turnRight();
break;
case 'S':
stopMoving();
break;
case 'Z':
linefollowing();
break;
}
}
void moveForward() {
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void moveBackward() {
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
void turnLeft() {
// Increase speed for the forward motors, decrease for the backward ones
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void turnRight() {
// Increase speed for the forward motors, decrease for the backward ones
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
void stopMoving() {
motor1.setSpeed(0);
motor2.setSpeed(0);
motor3.setSpeed(0);
motor4.setSpeed(0);
}
//line detection forward
void linefollowing() {
int leftSensor = digitalRead(leftir);
int rightSensor = digitalRead(rightir);
if (leftSensor == LOW && rightSensor == LOW) {
// Both sensors detect the line, move forward
motor1.setSpeed(150);
motor2.setSpeed(150);
motor3.setSpeed(150);
motor4.setSpeed(150);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
else if (leftSensor == LOW && rightSensor == HIGH) {
// Left sensor detects the line, turn left
motor1.setSpeed(200);
motor2.setSpeed(200);
motor3.setSpeed(100);
motor4.setSpeed(100);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
else if (leftSensor == HIGH && rightSensor == LOW) {
// Right sensor detects the line, turn right
motor1.setSpeed(100);
motor2.setSpeed(100);
motor3.setSpeed(200);
motor4.setSpeed(200);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
else {
// No line detected, stop
stopMoving();
}
}
How to add line changing function to the car when it is in line following mode ?
Is the current code for line following is ok? How to add line changing function to the car when the car recieve letter 'X' and 'C' to changing line to Left and Righ lane Respectivily
can you help to do it and say new things that i can implement in this project other than line following using same setup.
This project also contain an esp32cam for video streaming via wifi to the mobile app. Can i use any thing to make the car better?