Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit delay removal - 6 November 2019
- Plus some other stuff... this looks like an escape room thing.
- */
- #include <Keypad.h> // Arduino IDE library.
- #include <LiquidCrystal_I2C.h> // https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads/
- #include <Wire.h> // Arduino IDE library.
- // To set the total time you want, change the values of the hours, minutes and seconds below:
- int counter_seconds = 20; // count seconds -------------- I renamed these variables because they are not understandable.
- int counter_minutes = 10; // count minutes
- int counter_hours = 00; // count hours
- // When operating the countdown timer, it is easiest to convert the time to [seconds].
- long total_seconds = 0; // This is used to hold the total seconds of time left.
- unsigned long one_second_start_time = 0;
- unsigned long one_second_current_time = 0;
- bool timer_is_running = true;
- //char* password = "1234"; //create a password <--------------------- this doesn't work in the Arduino IDE. Not everything in C/C++ works in the Arduino IDE.
- char password[] = {'1', '2', '3', '4'}; //create a password
- int pozisyon = 0; //keypad position
- // With this sketch, every time you enter a correct password character, this value above will increment.
- // And there are 4 characters in the password.
- // So when this value gets to 5, that means that the [whatever] can be unlocked.
- int keypad_check_interval = 500; // This is the time in milliseconds to check the keypad buttons for being pressed.
- bool keypad_enabled = true; // Used to block keypresses happening too fast.
- unsigned long keypad_check_begin_time = 0;
- unsigned long keypad_check_current_time = 0;
- char temporary_char = 0;
- bool invalid_key_entered = false;
- int invalid_key_message_timer = 1000; // This is the milliseconds time to show the "invalid key" message.
- unsigned long unvalid_key_message_begin_time = 0;
- unsigned long unvalid_key_message_current_time = 0;
- const byte rows = 4; //number of the keypad's rows and columns
- const byte cols = 4;
- char keyMap [rows] [cols] = { //define the cymbols on the buttons of the keypad
- {'1', '2', '3', 'A'},
- {'4', '5', '6', 'B'},
- {'7', '8', '9', 'C'},
- {'*', '0', '#', 'D'}
- };
- byte rowPins [rows] = {1, 2, 3, 4}; //pins of the keypad
- byte colPins [cols] = {5, 6, 7, 8};
- Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, rows, cols);
- LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // This is a 20x4 LCD I am assuming.
- void setup() {
- Serial.begin(9600);
- lcd.begin(20, 4);
- setLocked(true); //state of the password
- lcd_clear_all_lines();
- show_locked_message();
- set_total_countdown_seconds();
- convert_total_seconds_to_h_m_s();
- display_remaining_time_on_lcd();
- Serial.println("Exiting setup()");
- }
- void loop() {
- if (pozisyon < 5) {
- // If pozisyon is less than 5, then the correct password has not been entered.
- if (keypad_enabled == true) {
- check_keypad_for_new_key_press();
- }
- else {
- check_to_enable_keypad();
- }
- }
- else {
- // If pozisyon == 5 then that means the correct password has been entered.
- setLocked(false);
- timer_is_running = false;
- show_unlocked_message();
- }
- check_countdown_timer();
- check_invalid_key_message();
- /*
- Old code is all below -------------------------------------------------------------------------------------------
- char whichKey = myKeypad.getKey(); //define which key is pressed with getKey
- lcd.setCursor(0, 0);
- lcd.print("EST. TIME TO BREACH:");
- lcd.setCursor(0, 2);
- lcd.print("IMPUT 7 DIGIT RESET");
- lcd.setCursor(3, 3);
- lcd.print("CODE TO KEYPAD");
- if (whichKey == '*' || whichKey == '#' || whichKey == 'A' || //define invalid keys
- whichKey == 'B' || whichKey == 'C' || whichKey == 'D') {
- pozisyon = 0;
- setLocked(true);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(" Invalid Key");
- delay(1000);
- lcd.clear();
- }
- if (whichKey == password[pozisyon]) {
- pozisyon ++;
- }
- lcd.setCursor(8, 1); // this is all the timer
- lcd.print(":");
- lcd.setCursor(11, 1);
- lcd.print(":");
- counter_seconds--;
- delay(1000); //THIS IS THE ONE I HAVE TO GET RID OF
- if (counter_seconds < 0)
- {
- counter_minutes--;
- counter_seconds = 59;
- }
- if (counter_minutes < 0)
- {
- counter_hours--;
- counter_minutes = 59;
- }
- if (counter_hours < 0) {
- counter_hours = 23;
- counter_minutes = 59;
- counter_seconds = 59;
- }
- if (counter_minutes > 9)
- {
- lcd.setCursor(9, 1);
- lcd.print(counter_minutes);
- }
- else
- {
- lcd.setCursor(9, 1);
- lcd.print("0");
- lcd.setCursor(10, 1);
- lcd.print(counter_minutes);
- lcd.setCursor(11, 1);
- lcd.print(":");
- }
- if (counter_seconds > 9)
- {
- lcd.setCursor(12, 1);
- lcd.print(counter_seconds);
- }
- else
- {
- lcd.setCursor(12, 1);
- lcd.print("0");
- lcd.setCursor(13, 1);
- lcd.print(counter_seconds);
- lcd.setCursor(14, 1);
- lcd.print(" ");
- }
- if (counter_hours > 9)
- {
- lcd.setCursor(6, 1);
- lcd.print (counter_hours);
- }
- else
- {
- lcd.setCursor(6, 1);
- lcd.print("0");
- lcd.setCursor(7, 1);
- lcd.print(counter_hours);
- lcd.setCursor(8, 1);
- lcd.print(":");
- }
- //finnal bit here, once the reset code is entered then this should display.
- if (pozisyon == 4) {
- setLocked(false); // when correct code entered
- lcd.clear();
- lcd.print(" ");
- delay(5000); //make them sit in silence for 5 seconds
- lcd.clear();
- lcd.setCursor(2, 1);
- lcd.print("BREACH CONTAINED"); //give confirmation of job well done
- delay(20000); //this should really be untill code is reset but for now its fine
- }
- delay(100);
- */
- } // end of main loop()
- void set_total_countdown_seconds() {
- total_seconds = counter_seconds + (counter_minutes * 60) + (counter_hours * 3600);
- }
- void convert_total_seconds_to_h_m_s() {
- counter_seconds = total_seconds;
- counter_hours = 0;
- counter_minutes = 0;
- while (counter_seconds >= 3600) {
- counter_seconds = counter_seconds - 3600;
- counter_hours = counter_hours + 1;
- }
- while (counter_seconds >= 60) {
- counter_seconds = counter_seconds - 60;
- counter_minutes = counter_minutes + 1;
- }
- }
- void show_locked_message() {
- lcd.setCursor(0, 0);
- lcd.print("EST. TIME TO BREACH:");
- // Note: this does not update the time.
- lcd.setCursor(0, 2);
- lcd.print("IMPUT 4 DIGIT RESET");
- lcd.setCursor(3, 3);
- lcd.print("CODE TO KEYPAD");
- }
- void show_unlocked_message() {
- lcd.clear();
- lcd.setCursor(2, 1);
- lcd.print("BREACH CONTAINED"); //give confirmation of job well done
- delay(20000); //this should really be untill code is reset but for now its fine
- }
- void check_countdown_timer() {
- if (timer_is_running == true) {
- if (total_seconds > 0) {
- // If there is still time left:
- one_second_current_time = millis();
- if (one_second_current_time >= one_second_start_time) {
- if (one_second_current_time >= (one_second_start_time + 1000)) {
- total_seconds--;
- convert_total_seconds_to_h_m_s();
- display_remaining_time_on_lcd();
- one_second_start_time = millis();
- }
- }
- else {
- one_second_start_time = millis();
- }
- }
- else {
- // This happens if you run out of time:
- show_time_expired_message();
- }
- }
- }
- void display_remaining_time_on_lcd() {
- // The time is printed on the second line down (#1).
- // hours --------------------------------
- lcd_clear_line2();
- if (counter_hours > 9)
- {
- lcd.setCursor(6, 1);
- lcd.print (counter_hours);
- }
- else
- {
- lcd.setCursor(6, 1);
- lcd.print("0");
- // lcd.setCursor(7, 1);
- lcd.print(counter_hours);
- }
- // ------------------------------------------------
- //lcd.setCursor(8, 1); // do not need
- lcd.print(":");
- // minutes -------------------------------------------
- if (counter_minutes > 9)
- {
- lcd.setCursor(9, 1);
- lcd.print(counter_minutes);
- }
- else
- {
- lcd.setCursor(9, 1);
- lcd.print("0");
- // lcd.setCursor(10, 1);
- lcd.print(counter_minutes);
- }
- // -------------------------------------------------
- //lcd.setCursor(11, 1); // do not need
- lcd.print(":");
- // seconds --------------------------------------------
- if (counter_seconds > 9)
- {
- lcd.setCursor(12, 1);
- lcd.print(counter_seconds);
- }
- else
- {
- lcd.setCursor(12, 1);
- lcd.print("0");
- // lcd.setCursor(13, 1);
- lcd.print(counter_seconds);
- }
- }
- void show_time_expired_message() {
- timer_is_running = false;
- lcd.clear();
- lcd.setCursor(2, 1);
- lcd.print("YOU DED BITCH"); //give confirmation of all dead
- delay(20000); //this should really be untill code is reset but for now its fine
- }
- void check_keypad_for_new_key_press() {
- temporary_char = myKeypad.getKey(); // getKey returns false if no key pressed.
- Serial.print("check_keypad_for_new_key_press() keypad entry = ");
- Serial.println(temporary_char);
- if (check_if_char_is_a_digit(temporary_char)) {
- // If the character is a digit:
- if (temporary_char == password[pozisyon]) {
- pozisyon++;
- Serial.print("check_keypad_for_new_key_press() correct entry: pozisyon = ");
- Serial.println(pozisyon);
- }
- else {
- Serial.print("check_keypad_for_new_key_press() incorrect entry: pozisyon = ");
- Serial.println(pozisyon);
- }
- temporary_char = 0;
- }
- else {
- show_invalid_key_message();
- }
- }
- bool check_if_char_is_a_digit(char thisChar) {
- bool testResult = false;
- if (thisChar > 47 && thisChar < 58) { // ASCII 47 == zero and ASCII 57 = nine.
- // This character is a digit.
- Serial.println("check_if_char_is_a_digit(char thisChar) digit test = true");
- testResult = true;
- }
- else {
- Serial.println("check_if_char_is_a_digit(char thisChar) digit test = false");
- }
- return testResult;
- }
- void check_to_enable_keypad() {
- // If the keypad is disabled by the software timer, this will re-enable it after keypad_check_interval has passed.
- keypad_check_current_time = millis();
- if (keypad_check_current_time >= keypad_check_begin_time) {
- if (keypad_check_current_time >= (keypad_check_begin_time + keypad_check_interval)) {
- keypad_enabled = true;
- }
- }
- else {
- keypad_check_begin_time = millis();
- }
- }
- void check_invalid_key_message() {
- if (invalid_key_entered == true) {
- unvalid_key_message_current_time = millis();
- if (unvalid_key_message_current_time >= unvalid_key_message_begin_time) {
- if (unvalid_key_message_current_time >= (unvalid_key_message_begin_time + invalid_key_message_timer)) {
- invalid_key_entered = false;
- show_normal_top_line();
- }
- }
- else {
- unvalid_key_message_begin_time = millis();
- }
- }
- }
- void show_normal_top_line() {
- lcd_clear_line1();
- lcd.print("EST. TIME TO BREACH:");
- }
- void show_invalid_key_message() {
- unvalid_key_message_begin_time = millis();
- invalid_key_entered = true;
- lcd_clear_line1();
- lcd.print(" Invalid Key");
- }
- void setLocked(bool thisValue) {
- // This is a do-nothing function, that was missing from the code provided?
- // ,,,,,,,, This is to lock or unlock the [whatever]
- if (thisValue == false) {
- // unlock the whatever here
- }
- else {
- // lock the whatever here
- }
- }
- void lcd_clear_all_lines() {
- lcd_clear_line1();
- lcd_clear_line2();
- lcd_clear_line3();
- lcd_clear_line4();
- }
- void lcd_clear_line1() {
- lcd.setCursor(0, 0);
- lcd.print(" ");
- lcd.setCursor(0, 0);
- }
- void lcd_clear_line2() {
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- }
- void lcd_clear_line3() {
- lcd.setCursor(20, 0);
- lcd.print(" ");
- lcd.setCursor(20, 0);
- }
- void lcd_clear_line4() {
- lcd.setCursor(20, 1);
- lcd.print(" ");
- lcd.setCursor(20, 1);
- }
Add Comment
Please, Sign In to add comment