//////////////////////////////////////////////////////////////
// Counter Strike Bomb //
// by RoboTecs //
//////////////////////////////////////////////////////////////
// Contact info: //
// https://www.facebook.com/robotecsRyT/ //
// https://www.youtube.com/c/RoboTecs //
// [email protected] //
//////////////////////////////////////////////////////////////
#include <SoftwareSerial.h>
#include <Keypad.h>
//#include <LiquidCrystal.h> //PANTALLA 16X2
#include <LiquidCrystal_I2C.h> //PANTALLA 20X4
#include <DFRobotDFPlayerMini.h>
SoftwareSerial mySoftwareSerial(12, 10); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
int Scount = 15; // Conteo regresivo de Segundos
int Mcount = 00; // Conteo regresivo de Minutos
int Hcount = 00; // Conteo regresivo de Horas
long secMillis = 0; // Guarda ultimo time para sumar segundos, referencia para llegando a fin de conteo
long interval = 1000; // Intervalo de tiempo para los segundos
char password[4]; // Longitud del PW: 4 caracteres
int currentLength = 0; //Lleva la longitud del numero que se está escribiendo actualmente
char entered[4]; //número ingresado
char HHkey[1]; //Almacena temporalmente la hora en vector de dos posiciones
char MMkey[1]; //Almacena temporalmente los minutos en vector de dos posiciones
char SSkey[1]; //Almacena temporalmente los segundos en vector de dos posiciones
unsigned long duracion = 100; //Controla el tiempo que dura el tono de avance de los segundos
/* Keypad setup */
const byte ROWS = 4; //4 wiersze
const byte COLS = 4; //4 kolumny
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {A3, A2, A1, A0};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
/* Keypad setup */
//LiquidCrystal lcd(A1, A2, A3, A4, A5, 11); // Rs, E, D4, D5, D6, D7; //PANTALLA 16X2
LiquidCrystal_I2C lcd(0x27, 20, 4); //PANTALLA 20X4
const int Buzzer = 8;
const int tonos[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494};
const int countTonos = 10;
void sonido() {
for (int iTono = 0; iTono < countTonos; iTono++)
{
tone(Buzzer, tonos[iTono]);
delay(50);
}
noTone(Buzzer);
}
void setup() {
lcd.init(); // instrucciones para mensajes LCD
lcd.backlight(); //instrucciones para encender la lámpara LCD posterior
pinMode(Buzzer, OUTPUT);
pinMode(13, OUTPUT); // LED que pulsa con cada segundo
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
tone(Buzzer, 100);
delay(500); // wait for a second
tone(Buzzer, 600);
delay(500); // wait for a second
noTone(Buzzer);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
Serial.begin(9600);
lcd.begin(20, 4);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Robotecs'Counter");
lcd.setCursor (0, 1);
lcd.print("Strike Bomb");
delay(3000);
sonido();
/*
mySoftwareSerial.begin(9600);
myDFPlayer.begin(mySoftwareSerial);
myDFPlayer.setTimeOut(500);
myDFPlayer.volume(30);
myDFPlayer.EQ(DFPLAYER_EQ_BASS);
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
myDFPlayer.disableLoopAll();
*/
//**********************hh
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Countdown");
lcd.setCursor (0, 1);
lcd.print("Hours: 00");
lcd.setCursor (9, 1);
while (currentLength < 2)
{
lcd.setCursor(currentLength + 7, 1); //mueva el cursos a medida que se digita
lcd.cursor();
char key = keypad.getKey(); //guarde el valor de la tecla digitada en la variable key
key == NO_KEY;
if (key != NO_KEY)
{
if ((key != '*') && (key != '#'))
{
lcd.print(key);
Serial.print("Opción: * ó #");
Serial.println(key);
HHkey[currentLength] = key;
currentLength++;
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
tone(Buzzer, 250, 100);
delay(200);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
}
}
}
if (currentLength == 2)
{
delay(500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Hours: ");
lcd.setCursor(6, 1);
lcd.print(HHkey[0]);
Serial.print(HHkey[0]);
lcd.print(HHkey[1]);
Serial.print(HHkey[1]);
Serial.println();
Serial.println(HHkey);
Hcount = 10 * (int(HHkey[0]) - 48) + (int(HHkey[1] - 48));
Serial.println(Hcount);
delay(2000);
lcd.clear();
currentLength = 0;
}
//**********************
//**********************
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Countdown");
lcd.setCursor (0, 1);
lcd.print("Minutes: 00");
lcd.setCursor (11, 1);
while (currentLength < 2)
{
lcd.setCursor(currentLength + 9, 1);
lcd.cursor();
char key = keypad.getKey();
key == NO_KEY;
if (key != NO_KEY)
{
if ((key != '*') && (key != '#'))
{
lcd.print(key);
MMkey[currentLength] = key;
currentLength++;
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
tone(Buzzer, 250, 100);
delay(200);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
}
}
}
if (currentLength == 2)
{
delay(500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Minutes: ");
lcd.setCursor(6, 1);
lcd.print(MMkey[0]);
lcd.print(MMkey[1]);
Mcount = 10 * (int(MMkey[0]) - 48) + (int(MMkey[1] - 48));
delay(2000);
lcd.clear();
currentLength = 0;
}
//******************
//**********************ss
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Countdown");
lcd.setCursor (0, 1);
lcd.print("Seconds: 00");
lcd.setCursor (11, 1);
while (currentLength < 2)
{
lcd.setCursor(currentLength + 9, 1);
lcd.cursor();
char key = keypad.getKey();
key == NO_KEY;
if (key != NO_KEY)
{
if ((key != '*') && (key != '#'))
{
lcd.print(key);
SSkey[currentLength] = key;
currentLength++;
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
tone(Buzzer, 250, 100);
delay(200);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
}
}
}
if (currentLength == 2)
{
delay(500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Seconds: ");
lcd.setCursor(6, 1);
lcd.print(SSkey[0]);
lcd.print(SSkey[1]);
Scount = 10 * (int(SSkey[0]) - 48) + (int(SSkey[1] - 48));
delay(2000);
lcd.clear();
currentLength = 0;
}
//******************
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter defusing ");
lcd.setCursor(0, 1);
lcd.print("Code: ");
while (currentLength < 4)
{
lcd.setCursor(currentLength + 6, 1);
lcd.cursor();
char key = keypad.getKey();
key == NO_KEY;
if (key != NO_KEY)
{
if ((key != '*') && (key != '#'))
{
lcd.print(key);
password[currentLength] = key;
currentLength++;
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
tone(Buzzer, 250, 100);
delay(200);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
}
}
}
if (currentLength == 4)
{
delay(500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("You've Entered: ");
lcd.setCursor(6, 1);
lcd.print(password[0]);
lcd.print(password[1]);
lcd.print(password[2]);
lcd.print(password[3]);
myDFPlayer.play(0002); //planted
delay(2000);
lcd.clear();
currentLength = 0;
}
}
void loop()
{
timer(); //conteo regresivo
char key2 = keypad.getKey(); // get the key
if (key2 == '*') // en el caso de querer desarmar la bomba
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
while (currentLength < 4)
{
timer();
char key2 = keypad.getKey();
if (key2 == '#')
{
currentLength = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Code: "));
}
else if (key2 != NO_KEY)
{
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
lcd.print(key2);
entered[currentLength] = key2;
currentLength++;
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
tone(Buzzer, 250, 100);
delay(200);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
lcd.noCursor();
lcd.setCursor(currentLength + 6, 0);
lcd.print("*");
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
}
}
if (currentLength == 4)
{
if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3])
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Bomb Defused"); // Bomba desarmada
currentLength = 0;
myDFPlayer.play(0001); //defused
delay(2000);
lcd.setCursor(0, 1);
lcd.print("Reset the Bomb");
delay(1000000);
}
else
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Wrong Password!");
if (Hcount > 0)
{
Hcount = 0; //Hcount = Hcount - 1;
}
if (Mcount > 0)
{
Mcount = 0; //Mcount = Mcount - 59;
}
if (Scount > 0)
{
Scount = 5; //Scount = Scount - 59;
}
delay(1500);
currentLength = 0;
}
}
}
}
void timer()
{
if (Hcount <= 0)
{
if ( Mcount < 0 )
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("The Bomb Has ");
lcd.setCursor (0, 1);
lcd.print("Exploded!");
while (Mcount < 0)
{
tone(Buzzer, 650);
digitalWrite(13, HIGH); //LED ON
delay(1000);
myDFPlayer.play(0003); // ¡Terrorist win!
delay(2000);
}
}
}
lcd.setCursor (0, 1);
lcd.print ("Timer:");
Serial.println("Timer: ");
if (Hcount >= 10)
{
lcd.setCursor (7, 1);
lcd.print (Hcount);
Serial.println(Hcount);
}
if (Hcount < 10)
{
lcd.setCursor (7, 1);
lcd.write ('0');
lcd.setCursor (8, 1);
lcd.print (Hcount);
}
lcd.print (":");
if (Mcount >= 10)
{
lcd.setCursor (10, 1);
lcd.print (Mcount);
}
if (Mcount < 10)
{
lcd.setCursor (10, 1);
lcd.write ('0');
lcd.setCursor (11, 1);
lcd.print (Mcount);
}
lcd.print (":");
if (Scount >= 10)
{
lcd.setCursor (13, 1);
lcd.print (Scount);
}
if (Scount < 10)
{
lcd.setCursor (13, 1);
lcd.write ('0');
lcd.setCursor (14, 1);
lcd.print (Scount);
}
if (Hcount < 0)
{
Hcount = 0;
}
if (Mcount < 0)
{
Hcount --;
Mcount = 59;
}
if (Scount < 1) // Si es 60 ejecutar
{
Mcount --; // sume 1 a Mcount
Scount = 59; // reiniciar Scount
}
if (Scount > 0) // Hacer esto 59 veces
{
unsigned long currentMillis = millis();
if (currentMillis - secMillis > interval)
{
secMillis = currentMillis;
Scount --;
if ((Scount < 40) && (Mcount == 0) && (Hcount == 0)) {
duracion = (((900 / -39) * Scount + 1023));
tone(Buzzer, 650, duracion);
digitalWrite(13, HIGH); //LED ON
delay(duracion);
if (Scount == 5) {
myDFPlayer.volume(20);
myDFPlayer.play(0004); //¡The bomb is ticking down!
delay(1000);
myDFPlayer.volume(30);
}
}
else if ((Scount >= 40) || (Mcount > 0) || (Hcount > 0)) {
tone(Buzzer, 650, 100); // Milis: 100 -
digitalWrite(13, HIGH); //LED ON
delay(30);
}
delay(50);
noTone(Buzzer);
digitalWrite(13, LOW); //LED OFF
}
}
}