I just wanted help with my arduino car project
Materials I have :
- Arduino Uno
- L293D Motor Driver
- 4 motors with wheels
- 4 servo motors
- 2 IR sensors
- Ultrasonic Sensor
- HC-05 Bluetooth module
with these items i want to create a robot car which can move around which i can control using my phone and voice controlled and obstacle avoiding and line following
As of right now i have created it to move around using my phone and it is voice controlled aswell , i want help with adding the obstacle avoidence and line following to my code and how to connect the sensors required to do it
this is my code
#include "AFMotor.h"
const int MOTOR_1 = 1;
const int MOTOR_2 = 2;
const int MOTOR_3 = 3;
const int MOTOR_4 = 4;
AF_DCMotor motor1(MOTOR_1, MOTOR12_64KHZ); // create motor object, 64KHz pwm
AF_DCMotor motor2(MOTOR_2, MOTOR12_64KHZ); // create motor object, 64KHz pwm
AF_DCMotor motor3(MOTOR_3, MOTOR12_64KHZ); // create motor object, 64KHz pwm
AF_DCMotor motor4(MOTOR_4, MOTOR12_64KHZ); // create motor object, 64KHz pwm
int state;
int Speed = 130;
void setup() {
motor1.setSpeed(Speed); // set the motor speed to 0-255
motor2.setSpeed(Speed);
motor3.setSpeed(Speed);
motor4.setSpeed(Speed);
Serial.begin(9600);
delay(500);
}
void loop(){
if(Serial.available() > 0){ //if some date is sent, reads it and saves in state
state = Serial.read();
if(state > 10){Speed = state;}
}
motor1.setSpeed(Speed); // set the motor speed to 0-255
motor2.setSpeed(Speed);
motor3.setSpeed(Speed);
motor4.setSpeed(Speed);
//===============================================================================
// Key Control Command
//===============================================================================
if(state == 1 || state == 'F'){forword(); } // if the state is '1' the DC motor will go forward
else if(state == 2 || state == 'B'){backword();} // if the state is '2' the motor will Reverse
else if(state == 3 || state == 'L'){turnLeft();} // if the state is '3' the motor will turn left
else if(state == 4 || state == 'R'){turnRight();} // if the state is '4' the motor will turn right
else if(state == 5 || state == 'S'){Stop(); } // if the state is '5' the motor will Stop
else if(state == 8){forwordleft(); } // if the state is '8' the motor will forword then left
else if(state == 9){forwordright(); } // if the state is '9' the motor will forword then right
else if(state == 10){backwordleft(); } // if the state is '9' the motor will backword then left
else if(state == 11){backwordright(); } // if the state is '9' the motor will backword then right
/////////////////////////////////////END\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//===============================================================================
// Voice Control Command
//===============================================================================
else if(state == 6){turnLeft(); delay(400); state = 5;}
else if(state == 7){turnRight(); delay(400); state = 5;}
/////////////////////////////////////END\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
delay(80);
}
void forword(){
motor1.run(FORWARD); //forward
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void backword(){
motor1.run(BACKWARD); // backword
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
void turnRight(){
motor1.run(FORWARD); // right
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
void turnLeft(){
motor1.run(BACKWARD); // left
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void forwordleft(){
motor1.run(RELEASE); // forwordleft
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(RELEASE);
}
void forwordright(){
motor1.run(FORWARD); // forwordright
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(FORWARD);
}
void backwordleft(){
motor1.run(BACKWARD); // backwordleft
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(BACKWARD);
}
void backwordright(){
motor1.run(RELEASE); // backwordright
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(RELEASE);
}
void Stop(){
motor1.run(RELEASE); // stopped
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
to this code i wanna add the obstacle avoidance using the ultrasonic sensor and line following with IR sensors can anyone please help me