// DEFINE
// Macros para recuperar las fracciones de segundos y minutos de un tiempo
// suministrado en ms
#define numeroDesegundos(_time_) ((_time_ / 1000) % 60)
#define numeroDeminutos(_time_) (((_time_ / 1000) / 60) % 60)
#include <TM1637Display.h>
// CONSTANTES
//const uint8_t OFF[] = {0, 0, 0, 0};
// En esta biblioteca, el orden de los bytes es .GFEDCBA
const uint8_t PLAY[] = {B01110011, B00111000, B01011111, B01101110};
const uint8_t FIN[] = {B01110001, B00110000, B01010100, B00000000};
uint8_t SEG_txtStnby[] = {
SEG_D, // _
SEG_D, // _
SEG_D, // _
SEG_B | SEG_C, // I
SEG_C | SEG_E | SEG_G, // n
SEG_A | SEG_C | SEG_D | SEG_F | SEG_G, // s
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G, // e
SEG_E |SEG_G, // r
SEG_D | SEG_E | SEG_F | SEG_G, // t
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G, // e
SEG_D, // _
SEG_A | SEG_B | SEG_F | SEG_E, // m
SEG_A | SEG_B | SEG_C | SEG_F, // m
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // o
SEG_C | SEG_E | SEG_G, // n
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G, // e
SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d
SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G, // a
};
// GLOBALES
// Crea un objeto de visualización, especificando parámetros (pin de reloj, pin de datos)
TM1637Display display(4, 5);
int buzzer = 11;
int buzz_freq = 1000;
int aviso = 11; //tiempo de aviso sonoro
volatile int activa_contador = 0;
int monedero = 2;
// 1000 ms en 1 segundo, 60 segundos en 1 minuto, 60 minutos en 1 hora. Entonces, 1000x60x60 = 3600000ms = 1 hora
unsigned long limite_de_tiempo = 0;
volatile int tiempo_restante;
void setup(){
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
pinMode(monedero, INPUT_PULLUP);
// interrupcion sobre pin digital 2
attachInterrupt(digitalPinToInterrupt(monedero), monedas, FALLING);
// Establecemos el brillo del display
display.setBrightness(4,true); // Controlamos el brillo que va desde 0 - 7 niveles
// Limpiamos el display
// display.setSegments(OFF);
display.clear();
}
// incrementamos el credito del usuario
void monedas(){
activa_contador = 1;
}
//tiempo_restante = tiempo_restante + 20000;
void displayText() {
display.setSegments(PLAY);
delay(2000);
}
void loop(){
// displayText();
//cuenta_regresiva();
if(activa_contador == 0){
text_stanby();
}
if(activa_contador == 1){
cuenta_regresiva();
}
}
void text_stanby(){
for (int i=0; i<18; i++)
{
display.setSegments(SEG_txtStnby+i);
delay(400);
Serial.println(i);
if (activa_contador == 1) // salimos del for
{
i = 0;
break;
}
} // fin for
} //fin text_stanby
void cuenta_regresiva() {
limite_de_tiempo = 20000;
// Calcula el tiempo restante
tiempo_restante = limite_de_tiempo - millis();
if(tiempo_restante > 0) {
// separamos las partes para mostrar la cuenta regresiva en formato mm:ss
int segundos = numeroDesegundos(tiempo_restante);
int minutos = numeroDeminutos(tiempo_restante);
// Esto muestra los segundos en los dos últimos lugares
display.showNumberDecEx(segundos, 0, true, 2, 2);
// esto separa los pares e impares para mostrar los 2 puntos del display, encendemos en pares y apagamos en impares
if (segundos % 2 == 0){
display.showNumberDecEx(minutos, 0b01000000, true, 2, 0); // enciende los 2 puntos
//iniciamos el aviso sonoro si faltan 10 segundos
if(segundos < aviso && minutos == 0){tone(buzzer, buzz_freq);}
delay(1000);
}
else {
display.showNumberDecEx(minutos, 0b00000000, true, 2, 0); // Apaga los 2 puntos
noTone(buzzer);
delay(1000);
}
// Muestra los minutos en los dos primeros lugares
display.showNumberDec(minutos, true, 2, 0);
//display.showNumberDecEx(minutos, 0x80>>1, true, 2, 0);
//Actualiza el tiempo restante
// tiempo_restante = limite_de_tiempo - millis();
// escribimos fin en pantalla notificando tiempo terminado al usuario
if (segundos == 0 && minutos == 0){
display.setSegments(FIN);
tone(buzzer, buzz_freq);
delay(2000);
noTone(buzzer);
activa_contador = 0;
}
} // fin de if tiempo restante
} //fin void cuenta_regresiva