-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bluetooth Bot
107 lines (92 loc) · 2.22 KB
/
Bluetooth Bot
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;
char receivedChar;// received value will be stored as CHAR in this variable
const int MR1 = 12; //ESP32 pins (MR=Right Motor) (ML=Left Motor) (1=Forward) (2=Backward)
const int MR2 = 14;
const int EN1 = 13;
const int ML1 = 27;
const int ML2 = 26;
const int EN2 = 25;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32"); //You can change your Bluetooth device name
pinMode(MR1, OUTPUT);
pinMode(MR2, OUTPUT);
pinMode(EN1, OUTPUT);
pinMode(EN2, OUTPUT);
pinMode(ML1, OUTPUT);
pinMode(ML2, OUTPUT);
}
void Forward(){
//RIGHT MOTOR
digitalWrite(MR1,HIGH);//MOVE FRONT
digitalWrite(MR2,LOW); //MOVE BACK
digitalWrite(EN1,HIGH);
//LEFT MOTOR
digitalWrite(ML1,LOW);//MOVE BACK
digitalWrite(ML2,HIGH);//MOVE FRONT
digitalWrite(EN2,HIGH);
}
void Backward(){
digitalWrite(MR1,LOW);
digitalWrite(MR2,HIGH);
digitalWrite(EN1,HIGH);
digitalWrite(ML1,HIGH);
digitalWrite(ML2,LOW);
digitalWrite(EN2,HIGH);
}
void Left(){
digitalWrite(MR1,HIGH);
digitalWrite(MR2,LOW);
digitalWrite(EN1,HIGH);
digitalWrite(ML1,HIGH);
digitalWrite(ML2,LOW);
digitalWrite(EN2,HIGH);
}
void Right(){
digitalWrite(MR1,LOW);
digitalWrite(MR2,HIGH);
digitalWrite(EN1,HIGH);
digitalWrite(ML1,LOW);
digitalWrite(ML2,HIGH);
digitalWrite(EN2,HIGH);
}
void Stop(){
digitalWrite(MR1,LOW);
digitalWrite(MR2,LOW);
digitalWrite(EN1,LOW);
digitalWrite(ML1,LOW);
digitalWrite(ML2,LOW);
digitalWrite(EN2,LOW);
}
void loop() {
receivedChar =(char)SerialBT.read();
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.print ("Received:");//print on serial monitor
Serial.println(receivedChar);//print on serial monitor
if(receivedChar == 'F')
{
Forward();
}
if(receivedChar == 'G')
{
Backward();
}
if(receivedChar == 'L')
{
Left();
}
if(receivedChar == 'R')
{
Right();
}
if(receivedChar == 'S')
{
Stop();
}
}
delay(20);
}