-
Notifications
You must be signed in to change notification settings - Fork 2
/
02_RC_ESP32_Controller_WiFi_L298N.ino
165 lines (122 loc) · 2.91 KB
/
02_RC_ESP32_Controller_WiFi_L298N.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Projek Robot Car Control WiFi
// By mirzaam_
// With Module L298N
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "r6OxEolMa5TH1oarrGr5TXBeaC3TsaFi";
char ssid[] = "MariAtun";
char pass[] = "29031999";
// Make Variable From Module L298N
const int enA = 27, enB = 14;
const int in1 = 16, in2 = 17, in3 = 25, in4 = 26;
const int freq = 500;
const int channel1 = 0;
const int channel2 = 1;
const int resolution = 10;
int speedRobot = 300;
const int LED_BUILTIN = 2;
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
ledcSetup( channel1 , freq , resolution );
ledcSetup( channel2 , freq , resolution );
ledcAttachPin( enA , channel1 );
ledcAttachPin( enB , channel2 );
}
void loop() {
Blynk.run();
}
//----------------------------
BLYNK_WRITE(V1) {
int x = param[0].asInt();
int y = param[1].asInt();
// Maju
if(x==0 && y==1){
forward();
}
// Mundur
else if(x==0 && y==-1){
backward();
}
// Ke Kanan
else if(x==1 && y==0){
right();
}
// Ke Kiri
else if(x==-1 && y==0){
left();
}
// Stop
else if(x==0 && y==0){
stop();
}
}
//----------------------------
//----------------------------
BLYNK_WRITE(V2)
{
int speedRobot = param.asInt();
// speed = 300 - 900;
Serial.print("Speed Motor: ");
Serial.println(speedRobot);
ledcWrite(channel1 , speedRobot);
ledcWrite(channel2 , speedRobot);
}
//----------------------------
//Maju
void forward() {
Serial.println("Jalan");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
//Mundur
void backward() {
Serial.println("Mundur");
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
//Berhenti
void stop() {
Serial.println("Berhenti");
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
//Belok Ke Kanan
void right() {
Serial.println("Belok Ke Kanan");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
//Belok Ke Kiri
void left() {
Serial.println("Belok Ke Kiri");
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
//Putar Balik
void turn() {
Serial.println("Putar Balik");
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}