1/first , you need to insstall the motor driver for L293D motor driver
2
3#include <AFMotor.h>
4#include <SoftwareSerial.h>
5
6SoftwareSerial HC05(9, 10);
7
8
9AF_DCMotor motor1(1);
10AF_DCMotor motor2(2);
11AF_DCMotor motor3(3);
12AF_DCMotor motor4(4);
13
14char command;
15
16void setup()
17{
18 HC05.begin(9600);
19}
20
21void loop() {
22 if (HC05.available() > 0) {
23 command = HC05.read();
24
25 Stop();
26
27 switch (command) {
28 case 'F':
29 forward();
30 break;
31 case 'B':
32 back();
33 break;
34 case 'L':
35 left();
36 break;
37 case 'R':
38 right();
39 break;
40 }
41 }
42}
43
44void forward()
45{
46 motor1.setSpeed(255);
47 motor1.run(FORWARD);
48 motor2.setSpeed(255);
49 motor2.run(FORWARD);
50 motor3.setSpeed(255);
51 motor3.run(FORWARD);
52 motor4.setSpeed(255);
53 motor4.run(FORWARD);
54}
55
56void back()
57{
58 motor1.setSpeed(255);
59 motor1.run(BACKWARD);
60 motor2.setSpeed(255);
61 motor2.run(BACKWARD);
62 motor3.setSpeed(255);
63 motor3.run(BACKWARD);
64 motor4.setSpeed(255);
65 motor4.run(BACKWARD);
66}
67
68void left()
69{
70 motor1.setSpeed(255);
71 motor1.run(FORWARD);
72 motor2.setSpeed(50);
73 motor2.run(FORWARD);
74 motor3.setSpeed(255);
75 motor3.run(FORWARD);
76 motor4.setSpeed(50);
77 motor4.run(FORWARD);
78}
79
80void right()
81{
82 motor1.setSpeed(50);
83 motor1.run(FORWARD);
84 motor2.setSpeed(255);
85 motor2.run(FORWARD);
86 motor3.setSpeed(50);
87 motor3.run(FORWARD);
88 motor4.setSpeed(255);
89 motor4.run(FORWARD);
90}
91
92void Stop()
93{
94 motor1.setSpeed(0);
95 motor1.run(RELEASE);
96 motor2.setSpeed(0);
97 motor2.run(RELEASE);
98 motor3.setSpeed(0);
99 motor3.run(RELEASE);
100 motor4.setSpeed(0);
101 motor4.run(RELEASE);
102}