//Biblioteca para habilitar a comunicação SERIAL
#include "BluetoothSerial.h"
//---Mapeamento de Hardware---//
#define led 5
#define btn 4
//Verifica se o bluetooth esta habilitado corretamente
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
//---Variáveis Globais---//
char dado;
bool flagBtn=0;
bool flagLed=0;
//Cria uma instância de BluetoothSerial chamado SerialBT
BluetoothSerial SerialBT;
void setup(){
Serial.begin(19200);
SerialBT.begin("UFFS-CC");
Serial.println("O dispositivo foi iniciado, agora você pode emparelhá-lo com bluetooth!");
pinMode(led, OUTPUT);
pinMode(btn, INPUT);
}
void loop(){
leBotao();
leSerial();
leBluetooth();
delay(20);
}
//==============================================//
void leSerial() {
if(Serial.available()) {
dado=(Serial.read());
SerialBT.write(Serial.read());
escreveBluetooth();
}
}
//==============================================//
void leBluetooth(){
if(SerialBT.available()){
dado=(SerialBT.read());
Serial.write(SerialBT.read());
escreveBluetooth();
}
}
//==============================================//
void leBotao(){
if((digitalRead(btn)==LOW)&&(flagBtn==0)){
flagBtn=1;
flagLed=!flagLed;
if(flagLed==0)
dado='b';
if(flagLed==1)
dado='B';
escreveBluetooth();
}
if((digitalRead(btn)==HIGH)&&(flagBtn==1)){
flagBtn=0;
}
}
//==============================================//
void escreveBluetooth(){
if(dado=='B'){
digitalWrite(led,HIGH);
SerialBT.print("Led ON via botao");
Serial.println("Led ON via botao");
SerialBT.print("*LR255*");
}
if(dado=='b'){
digitalWrite(led,LOW);
SerialBT.print("Led OFF via botao");
Serial.println("Led OFF via botao");
SerialBT.print("*LR50*");
}
if(dado=='l'){
digitalWrite(led,HIGH);
SerialBT.print("Led ON via bluetooth");
Serial.println("Led ON via bluetooth");
SerialBT.print("*LR255*");
}
if(dado=='d'){
digitalWrite(led,LOW);
SerialBT.print("Led OFF via bluetooth");
Serial.println("Led OFF via bluetooth");
SerialBT.print("*LR50*");
}
if(dado=='L'){
digitalWrite(led,HIGH);
SerialBT.print("Led ON via SERIAL");
Serial.println("Led ON via SERIAL");
SerialBT.print("*LR255*");
}
if(dado=='D'){
digitalWrite(led,LOW);
SerialBT.print("Led OFF via SERIAL");
Serial.println("Led OFF via SERIAL");
SerialBT.print("*LR50*");
}
}