You are on page 1of 2

1 /*

2 * Handoko Muji Prabowo


3 * NIM :
4 * Teknik Otomasi
5 */
6
7 #include "BluetoothSerial.h"
8 #include <analogWrite.h>
9
10 BluetoothSerial ESP_BT;
11
12 byte dataDiterima;
13 int duttyCycle = 200;
14
15 //Motor A
16 int motor1Pin1 = 27; //Pin 27
17 int motor1Pin2 = 26; //Pin 26
18 int enable1Pin = 14; //Pin 14 untuk konrol speed PWM
19
20 //Motor B
21 int motor2Pin1 = 25; //Pin 25
22 int motor2Pin2 = 33; //Pin 33
23 int enable2Pin2 = 32; //Pin 32 untuk konrol speed PWM
24
25 void setup() {
26 // put your setup code here, to run once:
27 //Setting PWM
28 analogWriteFrequency(30000); //Setting PWM frek di 3Khz
29 analogWriteResolution(8); // Timer resolusi nya 8 BIT
30 //setting pin sebagai output
31 pinMode(motor1Pin1, OUTPUT);
32 pinMode(motor1Pin2, OUTPUT);
33 pinMode(enable1Pin, OUTPUT);
34 pinMode(motor2Pin1, OUTPUT);
35 pinMode(motor2Pin2, OUTPUT);
36 pinMode(enable2Pin2, OUTPUT);
37
38 //Setting Pin Output LOW
39 digitalWrite(motor1Pin1, LOW);
40 digitalWrite(motor1Pin2, LOW);
41 digitalWrite(motor2Pin1, LOW);
42 digitalWrite(motor2Pin2, LOW);
43
44 //Siapkan Koneksi Bluetooth Serial
45 Serial.begin(9600);
46 ESP_BT.begin("ESP32_KONTROL_MOTOR"); // Nama Bluetooth
47 Serial.println("Perangkat Bluetooth siap di sambungkan");
48
49 //Testing
50 Serial.print("Testing DC Motor...");
51 }
52
53 void loop() {
54 // put your main code here, to run repeatedly:
55 if(ESP_BT.available()){ // Cek bila ada koneksi bluetooth
56 dataDiterima = ESP_BT.read(); // Data yang di kirim kan dari hp akan di tampung
di variabel dataDiterima
57 Serial.print("Diterima:"); Serial.println(dataDiterima);
58
59 if(dataDiterima == 48){ // bila data dari hp sebuah bilangan 0 maka TRUE
60 //MOTOR MAJU SPEED MAX
61 analogWrite(enable1Pin, 255);
62 analogWrite(enable2Pin2, 255);
63 Serial.println("MOTOR MAJU");
64 digitalWrite(motor1Pin1, LOW);
65 digitalWrite(motor1Pin2, HIGH);
66 digitalWrite(motor2Pin1, LOW);
67 digitalWrite(motor2Pin2, HIGH);
68 delay(2000);
69
70 // Stop DC motor
71 Serial.println("MOTOR STOP");
72 digitalWrite(motor1Pin1, LOW);
73 digitalWrite(motor1Pin2, LOW);
74 digitalWrite(motor2Pin1, LOW);
75 digitalWrite(motor2Pin2, LOW);
76 delay(1000);
77 }
78
79 if(dataDiterima == 49){ // bila data dari hp sebuah bilangan 1 maka TRUE
80 //MOTOR MUNDUR SPEED MAX
81 analogWrite(enable1Pin, 255);
82 analogWrite(enable2Pin2, 255);
83 Serial.println("MOTOR MUNDUR");
84 digitalWrite(motor1Pin1, HIGH);
85 digitalWrite(motor1Pin2, LOW);
86 digitalWrite(motor2Pin1, HIGH);
87 digitalWrite(motor2Pin2, LOW);
88 delay(2000);
89
90 // Stop the DC motor
91 Serial.println("MOTOR STOP");
92 digitalWrite(motor1Pin1, LOW);
93 digitalWrite(motor1Pin2, LOW);
94 digitalWrite(motor2Pin1, LOW);
95 digitalWrite(motor2Pin2, LOW);
96 delay(1000);
97 }
98
99 if(dataDiterima == 50){ // bila data dari hp sebuah bilangan 2 maka TRUE
100 digitalWrite(motor1Pin2, LOW);
101 digitalWrite(motor2Pin1, HIGH);
102 digitalWrite(motor1Pin1, HIGH);
103 digitalWrite(motor2Pin2, LOW);
104 while(duttyCycle <= 255){ // bila duttycycle = 255 maka loop berakhir
105 analogWrite(enable1Pin, duttyCycle); // kontrol speed perlahan - perlahan
dari 200-255 untuk enable1Pin
106 analogWrite(enable2Pin2, duttyCycle); // // kontrol speed perlahan -
perlahan dari 200-255 untuk enable2Pin2
107 Serial.print("Maju dengan duty cycle: ");
108 Serial.println(duttyCycle);
109 duttyCycle = duttyCycle + 5; // proses increment
110 delay(500);
111 }
112 duttyCycle = 200; // setelah loop maka variable di reset ke 200
113 }
114 }
115 }
116

You might also like