-
Notifications
You must be signed in to change notification settings - Fork 2
/
Bluetooth Control Car
96 lines (89 loc) · 2.42 KB
/
Bluetooth Control Car
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
int motor1Pin1 = 5;
int motor1Pin2 = 9;
int enable1Pin = 6;
int motor2Pin1 = 8;
int motor2Pin2 = 10;
int enable2Pin = 11;
int state;
int flag=0;
int stateStop=0;
void setup() {
// sets the pins as outputs:
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(enable2Pin, OUTPUT);
digitalWrite(enable1Pin, HIGH);
digitalWrite(enable2Pin, HIGH);
Serial.begin(9600);
}
void loop() {
if(Serial.available() > 0){
state = Serial.read();
flag=0;
}
// if the state is '1' the DC motor will go forward
if (state == '1') {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
if(flag == 0){
Serial.println("Go Forward!");
flag=1;
}
}
// if the state is '2' the motor will turn RIGHT
else if (state == '2') {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
if(flag == 0){
Serial.println("Turn RIGHT");
flag=1;
}
delay(1500);
state=3;
stateStop=1;
}
// if the state is '3' the motor will Stop
else if (state == '3' || stateStop == 1) {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
if(flag == 0){
Serial.println("STOP!");
flag=1;
}
stateStop=0;
}
// if the state is '4' the motor will turn LEFT
else if (state == '4') {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
if(flag == 0){
Serial.println("Turn LEFT");
flag=1;
}
delay(1500);
state=3;
stateStop=1;
}
// if the state is '5' the motor will Reverse
else if (state == '5') {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
if(flag == 0){
Serial.println("Reverse!");
flag=1;
}
}
}