#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "pitches.h"
#define LED_gb A4
#define LED_r A5
#define BEEPER 13
#define DEFUSE_BTN 12
const uint8_t ROWS = 4;
const uint8_t COLS = 3;
char keys[ROWS][COLS] = {
{ '1', '2', '3'},
{ '4', '5', '6'},
{ '7', '8', '9'},
{ '*', '0', '#'}
};
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
uint8_t colPins[COLS] = { 5, 4, 3 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String lock = "7355608";
String code = "";
bool plant = false, explosion = false, defusing = false;
float start, blik;
int seconds = 0, power, beep, n = 1, defuse = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
pinMode(A5, OUTPUT);
digitalWrite(A5, LOW);
digitalWrite(DEFUSE_BTN, HIGH);
lcd.begin(16, 2);
lcd.print("System Ready");
lcd.clear();
delay(2000);
lcd.print("Start Planting");
}
void loop() {
if (digitalRead(DEFUSE_BTN) == HIGH) {
defusing = 0;
}
char key = keypad.getKey();
if (plant == 0) {
if (key != NO_KEY) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Entered Code: ");
lcd.setCursor(0, 1);
lcd.print(code + key);
tone(BEEPER, 1000);
delay(10);
noTone(BEEPER);
code += key;
if (code.length() > 6) {
if (code != lock) {
lcd.setCursor(0, 1);
lcd.print("WRONG CODE ");
delay(2000);
lcd.clear();
lcd.print("Start Planting");
code = "";
} else {
plant = 1;
start = millis();
blik = millis();
lcd.setCursor(0, 1);
lcd.print("PLANTED ");
delay(2000);
lcd.clear();
}
}
}
} else {
if (explosion == 0) {
if (!defusing) {
if ((millis() - blik) > beep) {
blik = millis();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Explosion Alert");
lcd.setCursor(0, 1);
lcd.print("Time Left: ");
lcd.print(34 - seconds);
delay(500); //Change for IRL
ledControl(false);
}
}
if ((millis() - start) > 999) {
seconds++;
if (digitalRead(DEFUSE_BTN) == LOW) {
defusing = 1;
lcd.setCursor(0, 0);
lcd.print("Defusing... ");
lcd.setCursor(0, 1);
lcd.print("Time Left: ");
lcd.print(34 - seconds);
delay(100); //Change for IRL
ledControl(true);
if (digitalRead(DEFUSE_BTN) == LOW)
defuse += 2;
if (defuse == 10) {
plant = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("BOMB DEFUSED");
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(BEEPER, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(BEEPER);
}
lcd.setCursor(0, 1);
lcd.print("System Ready");
delay(2000);
lcd.clear();
lcd.print("Start Planting");
defuse = 0;
seconds = 0;
code = "";
defusing = 0;
}
}
start = millis();
}
beep = map(seconds, 0, 34, 1000, 100);
power = map(beep, 1000, 100, 0, 250);
if (seconds == 34) {
explosion = 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("EXPLOSION ALERT ");
lcd.setCursor(0, 1);
lcd.print("Evacuate area!");
tone(BEEPER, 10);
delay(5000);
noTone(BEEPER);
lcd.clear();
lcd.print("System Ready");
delay(2000);
lcd.clear();
lcd.print("Start Planting");
explosion = 0;
plant = 0;
seconds = 0;
code = "";
defusing = 0;
defuse = 0;
}
}
}
}
void ledControl (bool defusing) {
int hertz = 0;
if (defusing) {
hertz = 3213;
} else {
hertz = 2093;
}
digitalWrite(LED_r, HIGH);
if (seconds > 28)
digitalWrite(LED_gb, HIGH);
tone(BEEPER, hertz);
delay(10);
noTone(BEEPER);
for (float i = 255; i >= power; i--) {
analogWrite(LED_r, i);
if (seconds > 28)
analogWrite(LED_gb, i);
delay(3);
}
digitalWrite(LED_r, LOW);
digitalWrite(LED_gb, LOW);
}