In the previous article, we built a robot car that changes the route to avoid an obstacle. In this article, we have improved its functions. As for the robot car, it is capable of performing all the functions and is an improvement over previous projects. We have added special functions to it.
1.Bluetooth control
2.Line following
3.Obstacle avoidance ability
Arduino Uno robot car |
- If you refer to our previous articles, you can get an idea of these functions. Let's go to the procedure.
- You need these components to build this Arduino robot car.
Apparatus
1.Arduino Uno board
Arduino Uno board |
2.Motor drive(L298N)
Motor drive |
3.Bluetooth module (HC06)
Bluetooth module |
4.IR sensor
IR sensor |
5.Ultra sonic sensor
Ultrasonic sensor |
6.Servo motor
Servo motor |
7.Jumper cables
Jumper cable |
8. breadboard
breadboard |
9.Dot board
Dot board |
10.Four phone battery
phone battery |
10.Toy car wheel with gear
Connection diagram
Arduino code
#include<Servo.h>
Servo name;
//m1
int enA = 10;
int in1 = 12;
int in2 = 8;
//m2
int enB = 5;
int in3 = 7;
int in4 = 6;
char val;
#define t 11 //trig-white
#define e 13 //echo-black
long duration, cm ;
void setup() {
Serial.begin(9600);
name.attach(9);
name.write(90);
delay(300);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(t, OUTPUT );
pinMode(e, INPUT );
digitalWrite(t, LOW);
delayMicroseconds(5);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
}
/*
mpower(motor number,rotation,speed);
motor number
1-1st motor
2-2nd motor
rotation
1-forword
0-stop
-1-backword
speed
0-255 PWNvalue
*/
void mpower(int motor, int rotation, int spd) {
int pwm;
int pA;
int pB;
if (motor == 1) {
pwm = enA;
pA = in1;
pB = in2;
}
else if (motor == 2) {
pwm = enB;
pA = in3;
pB = in4;
}
else {
return;
}
if (rotation == 0) {
digitalWrite(pA, LOW);
digitalWrite(pB, LOW);
}
else if (rotation == 1) {
digitalWrite(pA, HIGH);
digitalWrite(pB, LOW);
}
else if (rotation == -1) {
digitalWrite(pA, LOW);
digitalWrite(pB, HIGH);
}
analogWrite(pwm, spd);
}
int speedval = 0; //speed of car(max 40)
int maxspeed = 40;
int normalspeed = 127;//less than 125(255/2)
int min_dis = 15;
int mode = 0;
//0-run
//1-scan
//2-rotate
int f_max = 0;
int f_max_ang = 0;
void loop() {
int lol;
if (Serial.available()) {
val = Serial.read();
Serial.println(val);
if (val == '1') {
mpower(1, 1, 100);
mpower(2, -1, 100);
}
if (val == '3') {
mpower(1, -1, 100);
mpower(2, 1, 100);
}
if (val == '4') {
mpower(1, 1, 255);
mpower(2, 1, 255);
}
if (val == '2') {
mpower(1, -1, 255);
mpower(2, -1, 255);
}
if (val == 'x') {
mpower(1, -1, 0);
mpower(2, -1, 0);
}
}
if (val == 'a') {
mpower(1, 1, 255);
mpower(2, -1, 255);
}
if (val == 'b') {
mpower(1, 1, 200);
mpower(2, -1, 200);
}
if (val == 'c') {
mpower(1, 1, 150);
mpower(2, -1, 150);
}
if (val == 'n') {
if (digitalRead(3) == HIGH) {
if (digitalRead(2) == LOW) {
if (digitalRead(4) == LOW) {
//go straight
mpower(1, -1, 255);
mpower(2, 1, 255);
lol=='1';
}
else {
//turn right
mpower(1, 1, 255);
mpower(2, 1, 255);
lol=='2';
}
}
else {
if (digitalRead(4) == LOW) {
//turn left
mpower(1, -1, 255);
mpower(2, -1, 255);
lol=='3';
}
else {
//stop
mpower(1, 1, 0);
mpower(2, -1, 0);
lol=='4';
}
}
}
else {
if (digitalRead(2) == LOW) {
if (digitalRead(4) == LOW) {
//stop
if(lol=='1'){
mpower(1, -1, 255);
mpower(2, 1, 255);
}
else if(lol=='2') {
mpower(1, -1, 255);
mpower(2, -1, 255);
}
else if(lol=='3'){
mpower(1, 1, 255);
mpower(2, 1, 255);
}
else if(lol=='4'){
mpower(1, -1, 0);
mpower(2, 1, 0);}
else if(lol=='6'){
mpower(1, -1, 255);
mpower(2, -1, 255);
}
else if(lol=='7'){
mpower(1, 1, 255);
mpower(2, 1, 255);
}
else if(lol=='8'){
mpower(1, -1, 0);
mpower(2, -1, 0);
}
//lol=='5';
}
else {
//turn right
mpower(1, 1, 255);
mpower(2, 1, 255);
lol=='6';
}
}
else {
if (digitalRead(4) == LOW) {
//turn left
mpower(1, -1, 255);
mpower(2, - 1, 255);
lol=='7';
}
else {
//stop
mpower(1, -1, 0);
mpower(2, -1, 0);
lol=='8';
}
}
}
}
if(val=='z'){
cm = getDistance();
if (mode == 0) {
if (cm > min_dis || cm == 0 ) {
speedval++;
if (speedval > maxspeed) {
speedval = maxspeed;
}
mpower(2, -1, 255);
mpower(1, 1, 255);
delay(100);
}
else {
mpower(1, -1, 255);
mpower(2, 1, 255);
delay(speedval * 6);
speedval = 0;
if (cm > 1 || speedval == 5) {
mode = 1;
}
}
Serial.print("CM");
Serial.print(cm);
Serial.print(",Speed-");
Serial.print(speedval);
Serial.print(",Mode-");
Serial.print(mode);
Serial.println();
}
else if (mode == 1) {
f_max = 0;
name.write(0);
delay(200);
for (int a = 0; a <= 180; a += 10) {
name.write(a);
delay(20);
cm = getDistance();
if (cm > f_max) {
f_max = cm;
f_max_ang = a;
}
Serial.print(",D=");
Serial.print(cm);
}
Serial.print("f_max_ang=");
Serial.print(f_max_ang);
Serial.print("MAX:");
Serial.println(f_max);
name.write(90);
delay(200);
mode = 2;
}
else if (mode == 2) {
if (f_max_ang < 90) {
Serial.println("Rotatiog LEFT");
mpower(1, -1, 255);
mpower(2, -1, 255);
delay(f_max_ang * 10);
}
else {
Serial.println("Rotatiog RIGH");
mpower(1, 1, 255);
mpower(2, 1, 255);
delay((f_max_ang-90) * 10);
}
mpower(1, 0, 0);
mpower(2, 0, 0);
mode = 0;
}
mpower(1, 0, 0);
mpower(2, 0, 0);
//mpower(1, 1, 50);
//mpower(2, -1, 100);
}
}
long getDistance() {
digitalWrite(t, HIGH);
delayMicroseconds(10);
digitalWrite(t, LOW);
duration = pulseIn(e, HIGH, 100000);
return (duration / 2) / 29.1;
}
1 Comments
which app is used to control this car?
ReplyDelete