-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathESP32-code.ino
107 lines (83 loc) · 1.55 KB
/
ESP32-code.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
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
int IN1 = 15; //motor1
int IN2 = 2; //motor1
int ENA = 0;
int IN3 = 4; //motor2
int IN4 = 16; //motor2
int ENB = 17;
char bt = 0;
void My_Motor(bool a, bool b,bool c, bool d){
digitalWrite(IN1,a);
digitalWrite(IN2,b);
digitalWrite(IN3,c);
digitalWrite(IN4,d);
}
void motorForward(){
My_Motor(1,0,0,1);
}
void motorBackward(){
My_Motor(0,1,1,0);
}
void motorStop(){
My_Motor(0,0,0,0);
}
void motorRight(){
My_Motor(1,0,1,0);
}
void motorLeft(){
My_Motor(0,1,1,0);
}
void motorSetSpeed(char motor, int speed){
if(motor == 'L'){
analogWrite(ENB, speed);
}
else if(motor == 'R'){
analogWrite(ENA, speed);
}
else if(motor == 'B'){
analogWrite(ENA, speed);
analogWrite(ENB, speed);
}
}
void setup()
{
Serial.begin(115200);
SerialBT.begin("Robot");
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
motorSetSpeed('B', 255);
}
void loop()
{
run();
}
void run(){
if (SerialBT.available() > 0)
{
bt = SerialBT.read();
Serial.print(bt);
if(bt == 'B') //move back
{
motorBackward();
}
else if (bt == 'F') //move forward
{
motorForward();
}
else if (bt == 'L') // move left
{
motorLeft();
}
else if (bt == 'R') //move right
{
motorRight();
}
else if (bt == 'S') //forward right
{
motorStop();
}
}
}