Created
June 26, 2021 04:15
-
-
Save chepecarlos/49f5298533b0c26b10fc25b07d158cd1 to your computer and use it in GitHub Desktop.
Codigo de ayuda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "BluetoothSerial.h" | |
#include "Wire.h" | |
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) | |
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it | |
#endif | |
BluetoothSerial SerialBT; | |
const int MPU_ADDR = 0x68; | |
#define Apagado 0 | |
#define Encendido 1 | |
#define Interminente 2 | |
struct GradosLivertad { | |
int16_t x; | |
int16_t y; | |
int16_t z; | |
}; | |
struct SuperLed { | |
String Nombre; | |
int Pin; | |
int Estado; | |
boolean Activo; | |
float Tiempo; | |
}; | |
SuperLed Leds[3] = { | |
{"rojo", 5, Apagado, false, 0}, | |
{"azul", 18, Apagado, false, 0}, | |
{"verde", 13, Apagado, false, 0} | |
}; | |
int CantidadLed = 3; | |
float IntervaloLed = 200; | |
GradosLivertad Acelerometo = {0, 0, 0}; | |
GradosLivertad Giroscopio = {0, 0, 0}; | |
int Temperatura = 0; | |
void setup() { | |
Serial.begin(115200); | |
//BTSerial.begin(9600); | |
SerialBT.begin("ESP32test"); //Bluetooth device name | |
Serial.println("The device started, now you can pair it with bluetooth!"); | |
for (int i = 0; i < CantidadLed; i++) { | |
pinMode(Leds[i].Pin, OUTPUT); | |
} | |
Wire.begin(); | |
Wire.beginTransmission(MPU_ADDR); | |
Wire.write(0x6B); | |
Wire.write(0); | |
Wire.endTransmission(true); | |
} | |
void loop() { | |
ActualizarAcelerometro(); | |
if (SerialBT.available()) { | |
DecodificarSerial(); | |
} | |
for (int i = 0; i < CantidadLed; i++) { | |
ActualizarLed(Leds[i]); | |
} | |
ActualizarLedAcalerrometro(); | |
} | |
void ActualizarLed(SuperLed &Led) { | |
switch (Led.Estado) { | |
case Apagado: | |
digitalWrite(Led.Pin, 0); | |
break; | |
case Encendido: | |
digitalWrite(Led.Pin, 1); | |
break; | |
case Interminente: | |
float TiempoActual = millis(); | |
if (TiempoActual - Led.Tiempo > IntervaloLed) { | |
Led.Tiempo = TiempoActual; | |
Led.Activo = !Led.Activo; | |
if (Led.Activo) { | |
digitalWrite(Led.Pin, 1); | |
} else { | |
digitalWrite(Led.Pin, 0); | |
} | |
} | |
break; | |
} | |
} | |
void DecodificarSerial() { | |
// rojo/a \n | |
// rojo es led | |
// a mensaje [a|e|i](Apagado, Encendido, Interminente) | |
String Mensaje = SerialBT.readStringUntil('\n'); | |
Serial.print("Mensaje : "); | |
Serial.println(Mensaje); | |
int PosicionPleca = Mensaje.indexOf('/'); | |
int PosicionSaltoLinea = Mensaje.length(); | |
String Dato = Mensaje.substring(0, PosicionPleca); | |
String Valor = Mensaje.substring(PosicionPleca + 1, PosicionSaltoLinea); | |
int EstadoActualLed = ObtenerEstadoLed(Valor); | |
for (int i = 0; i < CantidadLed; i++) { | |
if (Dato.equals(Leds[i].Nombre)) { | |
Leds[i].Estado = EstadoActualLed; | |
Serial.print("Led : "); | |
Serial.print(Dato); | |
Serial.print(" Estado : "); | |
Serial.println(EstadoActualLed); | |
return; | |
} | |
} | |
Serial.println("Error mensaje"); | |
} | |
int ObtenerEstadoLed(String Valor) { | |
if (Valor.equals("a")) { | |
return Apagado; | |
} else if (Valor.equals("e")) { | |
return Encendido; | |
} else if (Valor.equals("i")) { | |
return Interminente; | |
} | |
return -1; | |
} | |
void ActualizarAcelerometro() { | |
Wire.beginTransmission(MPU_ADDR); | |
Wire.write(0x3B); | |
Wire.endTransmission(false); | |
Wire.requestFrom(MPU_ADDR, 14, true); | |
Acelerometo.x = Wire.read() << 8 | Wire.read(); | |
Acelerometo.y = Wire.read() << 8 | Wire.read(); | |
Acelerometo.z = Wire.read() << 8 | Wire.read(); | |
Temperatura = Wire.read() << 8 | Wire.read(); | |
Giroscopio.x = Wire.read() << 8 | Wire.read(); | |
Giroscopio.y = Wire.read() << 8 | Wire.read(); | |
Giroscopio.z = Wire.read() << 8 | Wire.read(); | |
} | |
void ActualizarLedAcalerrometro() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment