#include "BluetoothSerial.h"
#include <ESP32Servo.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#define LED 2
BluetoothSerial SerialBT;
//Declaramos la variable que almacenara el valor analigico ingresara del potenciometro
int entradaPotenciometro = 0;
//Variable que movera el servo
int anguloServo = 0;
//Declaramos el servo
Servo servo;
//Declaramos la variable del pin del potenciometro
int pinPotenciometro=15;
//Declaramos la variable del pin del servo
int pinServo=4;
String message = "";
char incoming;
BluetoothSerial BT; // Objeto Bluetooth
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
//Configuramos el pin del potenciometro
pinMode(pinPotenciometro, INPUT);
//Configuramos el servo y su pin
servo.attach(pinServo, 500, 2500);
pinMode (LED, OUTPUT); // Cambie el pin LED a OUTPUT
}
void loop() {
//Recibimos los datos analogicos del potenciometro
entradaPotenciometro = analogRead(pinPotenciometro);
//Obtenemos el anguloServo multiplicando entradaPotenciometro * (180/4095)
// anguloServo = (entradaPotenciometro * 0.04395604395);
Serial.print("entradaPotenciometro:");
Serial.println(entradaPotenciometro);
Serial.print("anguloServo:");
Serial.println(anguloServo);
Serial.println("------------------");
//Movemos el servo este angulo
servo.write(anguloServo);
delay(10); // this speeds up the simulation
if (BT.available()) // Compruebe si recibimos algo de Bluetooth
{
char incoming = BT.read();
if (incoming != '\n'){
message += String(incoming);
}
else{
Serial.println(message);
if (message == "ON"){ // 1 en ASCII
digitalWrite(LED, HIGH); // LED Encendido
//BT.println("LED encendido"); // Envía el mensaje de texto a través de BT Serial
}
if (message == "OFF"){ // 0 en ASCII
digitalWrite(LED, LOW); // LED Apagado
//BT.println("LED apagado"); // Envía el mensaje de texto a través de BT Serial
}
if (message == "t"){ // 0 en ASCII
BT.print(entradaPotenciometro);
}
if (message.startsWith("z")){ // 0 en ASCII
message.remove(0,1);
anguloServo = message.toInt();
}
message = "";
}
delay(20);
}
}