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