Kondisi yang ingin ditampilkan yaitu :
- 0 LED turned OFF
- 1 LED turned ON
- 2 LED turned ON-OFF
Perangkat yang dibutuhkan :
- ESP32 dengan kabel USB
- LED
- Resistor
- 2 buah Kabel Jumper
- 1 buah Motherboard
Gambar Rangkaian :
Code :
#include "BluetoothSerial.h" //Header File for
Serial Bluetooth, will be added by default into Arduino
BluetoothSerial ESP_BT; //Object for Bluetooth
int incoming;
const int pinLED = 14;
void setup() {
Serial.begin(9600);
//Start Serial monitor in 9600
ESP_BT.begin("ESP32_LED"); //Name of your Bluetooth Signal
Serial.println("Bluetooth Device is Ready to Pair");
pinMode (pinLED,
OUTPUT);//Specify that LED pin is output
}
void loop() {
if (ESP_BT.available())
//Check if we receive anything from Bluetooth
{
incoming =
ESP_BT.read(); //Read what we recevive
Serial.print("Received:"); Serial.println(incoming);
if (incoming ==
49)
{
digitalWrite(pinLED, HIGH);
ESP_BT.println("LED turned ON");
}
if (incoming ==
48)
{
digitalWrite(pinLED, LOW);
ESP_BT.println("LED turned OFF");
}
if (incoming ==
50)
{
ESP_BT.println("LED turned ON-OFF");
digitalWrite(pinLED, HIGH);
delay (1000);
digitalWrite(pinLED, LOW);
delay (1000);
digitalWrite(pinLED, HIGH);
delay (1000);
digitalWrite(pinLED, LOW);
delay (1000);
digitalWrite(pinLED,
HIGH);
delay (1000);
digitalWrite(pinLED, LOW);
delay (1000);
}
}
delay(20);
}
Tidak ada komentar:
Posting Komentar