-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bluetooth_Car.ino
136 lines (112 loc) · 2.16 KB
/
Bluetooth_Car.ino
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Control car using bluetooth
*/
#include <SoftwareSerial.h>
#define RX 10
#define TX 11
#define IN1 3
#define IN2 5
#define IN3 6
#define IN4 9
#define MAX_SPEED 100
#define MIN_SPEED 0
void forward();
void backward();
void left();
void right();
void stop();
void motor_1_stop() {
analogWrite(IN1, 0);
analogWrite(IN2, 0);
}
void motor_2_stop() {
analogWrite(IN3, LOW);
analogWrite(IN4, LOW);
}
void motor_1_for(int speed) {
analogWrite(IN1, speed);
analogWrite(IN2, 0);
}
void motor_1_back(int speed) {
analogWrite(IN1, 0);
analogWrite(IN2, speed);
}
void motor_2_for(int speed) {
analogWrite(IN3, speed);
analogWrite(IN4, 0);
}
void motor_2_back(int speed) {
analogWrite(IN3, 0);
analogWrite(IN4, speed);
}
SoftwareSerial mySerial(RX, TX); // RX, TX
void setup() {
Serial.begin(9600);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
while (!Serial) {
// wait for serial port to connect. Needed for native USB port only
}
mySerial.begin(9600);
mySerial.println("Bluetooth Hi");
Serial.write("Hello\n");
}
char state = 0;
void loop() {
if (mySerial.available()) {
state = mySerial.read();
Serial.write(state + '\n');
}
int delayTime = 500;
switch (state){
case '1':
Serial.write("UP\n");
forward();
break;
case '2':
Serial.write("DOWN\n");
backward();
break;
case '4':
Serial.write("LEFT\n");
left();
break;
case '3pioj':
Serial.write("RIGHT\n");
right();
break;
default:
Serial.write("STOP\n");
stop();
break;
}
delay(50);
}
//*******************************************************
void forward()
{
motor_1_for(MAX_SPEED);
motor_2_for(MAX_SPEED);
}
void backward()
{
motor_1_back(MAX_SPEED);
motor_2_back(MAX_SPEED);
}
void left()
{
motor_1_for(MAX_SPEED);
motor_2_back(MAX_SPEED);
}
void right()
{
motor_2_for(MAX_SPEED);
motor_1_back(MAX_SPEED);
}
void stop()
{
motor_1_stop();
motor_2_stop();
}