/*
Comandos BT
-------------
A B C - Cambia entre los modos de medición
I - Inicia la medición
S - Para la medición
U - Envia las ultimas 5 mediciones a la app
*/
//Librerias
#include <TM1637Display.h> //https://docs.arduino.cc/libraries/tm1637/
#include <BLESerial.h> //https://docs.arduino.cc/libraries/serial_ble/
//Config. Pines
#define BOTONINICIO 3
#define BOTONMODO 4
#define DISPLAYCLK 9
#define DISPLAYDIO 8
//Config. Bluetooth BLE
String device_name = "CronometroESP32";
BLESerial<> SerialBLE;
//Config. Display
TM1637Display display(DISPLAYCLK, DISPLAYDIO);
const uint8_t SEG_T30[] = {
SEG_D | SEG_E | SEG_F | SEG_G, // T
SEG_G, // -
SEG_A | SEG_B | SEG_C | SEG_D | SEG_G, // 3
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F // 0
};
const uint8_t SEG_T60[] = {
SEG_D | SEG_E | SEG_F | SEG_G, // T
SEG_G, // -
SEG_A | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G, // 6
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F // 0
};
const uint8_t SEG_CRON[] = {
SEG_A | SEG_D | SEG_E | SEG_F, // C
SEG_A | SEG_B | SEG_E | SEG_F, // R
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O
SEG_A | SEG_B | SEG_C | SEG_E | SEG_F // N
};
const uint8_t SEG_FIN[] = {
SEG_A | SEG_E | SEG_F | SEG_G, // F
SEG_E | SEG_F, // I
SEG_A | SEG_B | SEG_C | SEG_E | SEG_F, // N
SEG_G // -
};
//Variables
int historialmodo[] = { 0, 0, 0, 0, 0 };
int historialtiempos[] = { 0, 0, 0, 0, 0 };
long tiempoinicio = 0;
long tiempo = 0;
long tiempofin = 0;
int limitetiempo = 0;
int contador = 0;
int modo = 1;
int modoanterior = 0;
bool inicio = false;
bool parar = false;
//Funciones
void enviardatos() {
for (int i = 0; i <= 4; i++) {
SerialBLE.print(historialmodo[i]);
SerialBLE.flush();
SerialBLE.write(',');
SerialBLE.flush();
SerialBLE.print(historialtiempos[i]);
SerialBLE.flush();
if (i < 4) {
SerialBLE.write(',');
SerialBLE.flush();
}
}
SerialBLE.write('/');
SerialBLE.flush();
delay(10);
}
void ControlBT(void *e) {
for (;;) {
if (SerialBLE.available() > 0) {
char lecturabt = SerialBLE.read();
//Serial.println(lecturabt);
switch (lecturabt) {
case 'A':
if (inicio == false) {
modo = 1;
}
break;
case 'B':
if (inicio == false) {
modo = 2;
}
break;
case 'C':
if (inicio == false) {
modo = 3;
}
break;
case 'I':
if (inicio == false) {
inicio = true;
}
break;
case 'S':
if (inicio == true) {
parar = true;
}
break;
case 'U':
if (inicio == false) {
enviardatos();
}
break;
}
}
delay(20);
}
}
void setup() {
//Serial.begin(115200);
SerialBLE.begin(device_name);
xTaskCreate(ControlBT, "ControlBT", 2048, NULL, 2, NULL);
display.setBrightness(7, true);
display.clear();
pinMode(BOTONINICIO, INPUT_PULLUP);
pinMode(BOTONMODO, INPUT_PULLUP);
}
void loop() {
while (inicio == false) {
yield();
if (digitalRead(BOTONMODO) == 0) {
modo++;
if (modo >= 4) {
modo = 1;
}
delay(300);
}
if (modo != modoanterior) {
display.clear();
switch (modo) {
case 1:
display.setSegments(SEG_CRON);
limitetiempo = 90000;
break;
case 2:
display.setSegments(SEG_T30);
limitetiempo = 30000;
break;
case 3:
display.setSegments(SEG_T60);
limitetiempo = 60000;
break;
}
modoanterior = modo;
}
if (digitalRead(BOTONINICIO) == 0) {
delay(300);
inicio = true;
break;
}
}
display.clear();
display.showNumberDec(3);
delay(1000);
display.showNumberDec(2);
delay(1000);
display.showNumberDec(1);
delay(1000);
tiempo = 0;
contador = 0;
tiempoinicio = millis();
tiempofin = (tiempoinicio + limitetiempo);
while (inicio == true) {
yield();
tiempo = millis();
//if (tiempo <= tiempofin) {
switch (modo) {
case 1:
contador = ((tiempo - tiempoinicio) / 10);
break;
default:
contador = ((limitetiempo - (tiempo - tiempoinicio)) / 10);
break;
}
display.showNumberDecEx(contador, 0x40, true, 4, 0);
//}
if (digitalRead(BOTONINICIO) == 0) {
parar = true;
}
if (parar == true || tiempo > tiempofin) {
inicio = false;
parar = false;
display.clear();
display.setSegments(SEG_FIN);
delay(750);
display.clear();
display.showNumberDecEx(contador, 0x40, true, 4, 0);
delay(500);
for (int i = 3; i >= 0; i--) {
historialmodo[(i + 1)] = historialmodo[i];
historialtiempos[(i + 1)] = historialtiempos[i];
}
historialmodo[0] = modo;
historialtiempos[0] = contador;
break;
}
}
}