skizziks_53

Reddit code timer sketch v 1.0

Nov 7th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.07 KB | None | 0 0
  1. /*
  2.   Reddit delay removal - 6 November 2019
  3.  
  4.   Plus some other stuff... this looks like an escape room thing.
  5.  
  6. */
  7.  
  8. #include <Keypad.h> // Arduino IDE library.
  9. #include <LiquidCrystal_I2C.h> // https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads/
  10. #include <Wire.h> // Arduino IDE library.
  11.  
  12. // To set the total time you want, change the values of the hours, minutes and seconds below:
  13. int counter_seconds = 20; // count seconds -------------- I renamed these variables because they are not understandable.
  14. int counter_minutes = 10; // count minutes
  15. int counter_hours = 00; // count hours
  16.  
  17. // When operating the countdown timer, it is easiest to convert the time to [seconds].
  18. long total_seconds = 0; // This is used to hold the total seconds of time left.
  19. unsigned long one_second_start_time = 0;
  20. unsigned long one_second_current_time = 0;
  21. bool timer_is_running = true;
  22.  
  23. //char* password = "1234"; //create a password <--------------------- this doesn't work in the Arduino IDE. Not everything in C/C++ works in the Arduino IDE.
  24. char password[] = {'1', '2', '3', '4'}; //create a password
  25.  
  26. int pozisyon = 0; //keypad position
  27. // With this sketch, every time you enter a correct password character, this value above will increment.
  28. // And there are 4 characters in the password.
  29. // So when this value gets to 5, that means that the [whatever] can be unlocked.
  30.  
  31. int keypad_check_interval = 500; // This is the time in milliseconds to check the keypad buttons for being pressed.
  32. bool keypad_enabled = true; // Used to block keypresses happening too fast.
  33. unsigned long keypad_check_begin_time = 0;
  34. unsigned long keypad_check_current_time = 0;
  35. char temporary_char = 0;
  36.  
  37. bool invalid_key_entered = false;
  38. int invalid_key_message_timer = 1000; // This is the milliseconds time to show the "invalid key" message.
  39. unsigned long unvalid_key_message_begin_time = 0;
  40. unsigned long unvalid_key_message_current_time = 0;
  41.  
  42. const byte rows = 4; //number of the keypad's rows and columns
  43. const byte cols = 4;
  44.  
  45. char keyMap [rows] [cols] = { //define the cymbols on the buttons of the keypad
  46.   {'1', '2', '3', 'A'},
  47.   {'4', '5', '6', 'B'},
  48.   {'7', '8', '9', 'C'},
  49.   {'*', '0', '#', 'D'}
  50. };
  51.  
  52. byte rowPins [rows] = {1, 2, 3, 4}; //pins of the keypad
  53. byte colPins [cols] = {5, 6, 7, 8};
  54.  
  55. Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, rows, cols);
  56.  
  57. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // This is a 20x4 LCD I am assuming.
  58.  
  59. void setup() {
  60.   Serial.begin(9600);
  61.   lcd.begin(20, 4);
  62.   setLocked(true); //state of the password
  63.   lcd_clear_all_lines();
  64.   show_locked_message();
  65.   set_total_countdown_seconds();
  66.   convert_total_seconds_to_h_m_s();
  67.   display_remaining_time_on_lcd();
  68.   Serial.println("Exiting setup()");
  69. }
  70.  
  71.  
  72.  
  73. void loop() {
  74.  
  75.   if (pozisyon < 5) {
  76.     // If pozisyon is less than 5, then the correct password has not been entered.
  77.     if (keypad_enabled == true) {
  78.       check_keypad_for_new_key_press();
  79.     }
  80.     else {
  81.       check_to_enable_keypad();
  82.     }
  83.   }
  84.   else {
  85.     // If pozisyon == 5 then that means the correct password has been entered.
  86.     setLocked(false);
  87.     timer_is_running = false;
  88.     show_unlocked_message();
  89.   }
  90.  
  91.   check_countdown_timer();
  92.  
  93.   check_invalid_key_message();
  94.  
  95.   /*
  96.      Old code is all below -------------------------------------------------------------------------------------------
  97.     char whichKey = myKeypad.getKey(); //define which key is pressed with getKey
  98.     lcd.setCursor(0, 0);
  99.     lcd.print("EST. TIME TO BREACH:");
  100.     lcd.setCursor(0, 2);
  101.     lcd.print("IMPUT 7 DIGIT RESET");
  102.     lcd.setCursor(3, 3);
  103.     lcd.print("CODE TO KEYPAD");
  104.  
  105.     if (whichKey == '*' || whichKey == '#' || whichKey == 'A' || //define invalid keys
  106.       whichKey == 'B' || whichKey == 'C' || whichKey == 'D') {
  107.     pozisyon = 0;
  108.     setLocked(true);
  109.     lcd.clear();
  110.     lcd.setCursor(0, 0);
  111.     lcd.print(" Invalid Key");
  112.     delay(1000);
  113.     lcd.clear();
  114.     }
  115.  
  116.     if (whichKey == password[pozisyon]) {
  117.     pozisyon ++;
  118.     }
  119.  
  120.     lcd.setCursor(8, 1); // this is all the timer
  121.     lcd.print(":");
  122.     lcd.setCursor(11, 1);
  123.     lcd.print(":");
  124.     counter_seconds--;
  125.     delay(1000); //THIS IS THE ONE I HAVE TO GET RID OF
  126.     if (counter_seconds < 0)
  127.     {
  128.     counter_minutes--;
  129.     counter_seconds = 59;
  130.     }
  131.  
  132.     if (counter_minutes < 0)
  133.     {
  134.     counter_hours--;
  135.     counter_minutes = 59;
  136.     }
  137.  
  138.     if (counter_hours < 0) {
  139.     counter_hours = 23;
  140.     counter_minutes = 59;
  141.     counter_seconds = 59;
  142.     }
  143.  
  144.     if (counter_minutes > 9)
  145.     {
  146.     lcd.setCursor(9, 1);
  147.     lcd.print(counter_minutes);
  148.     }
  149.     else
  150.     {
  151.     lcd.setCursor(9, 1);
  152.     lcd.print("0");
  153.     lcd.setCursor(10, 1);
  154.     lcd.print(counter_minutes);
  155.     lcd.setCursor(11, 1);
  156.     lcd.print(":");
  157.     }
  158.  
  159.     if (counter_seconds > 9)
  160.     {
  161.     lcd.setCursor(12, 1);
  162.     lcd.print(counter_seconds);
  163.     }
  164.     else
  165.     {
  166.     lcd.setCursor(12, 1);
  167.     lcd.print("0");
  168.     lcd.setCursor(13, 1);
  169.     lcd.print(counter_seconds);
  170.     lcd.setCursor(14, 1);
  171.     lcd.print(" ");
  172.     }
  173.  
  174.     if (counter_hours > 9)
  175.     {
  176.     lcd.setCursor(6, 1);
  177.     lcd.print (counter_hours);
  178.     }
  179.     else
  180.     {
  181.     lcd.setCursor(6, 1);
  182.     lcd.print("0");
  183.     lcd.setCursor(7, 1);
  184.     lcd.print(counter_hours);
  185.     lcd.setCursor(8, 1);
  186.     lcd.print(":");
  187.     }
  188.  
  189.     //finnal bit here, once the reset code is entered then this should display.
  190.  
  191.     if (pozisyon == 4) {
  192.     setLocked(false); // when correct code entered
  193.     lcd.clear();
  194.     lcd.print(" ");
  195.     delay(5000); //make them sit in silence for 5 seconds
  196.     lcd.clear();
  197.     lcd.setCursor(2, 1);
  198.     lcd.print("BREACH CONTAINED"); //give confirmation of job well done
  199.     delay(20000); //this should really be untill code is reset but for now its fine
  200.     }
  201.  
  202.     delay(100);
  203.   */
  204. } // end of main loop()
  205.  
  206.  
  207.  
  208.  
  209. void set_total_countdown_seconds() {
  210.   total_seconds = counter_seconds + (counter_minutes * 60) + (counter_hours * 3600);
  211. }
  212.  
  213. void convert_total_seconds_to_h_m_s() {
  214.   counter_seconds = total_seconds;
  215.   counter_hours = 0;
  216.   counter_minutes = 0;
  217.   while (counter_seconds >= 3600) {
  218.     counter_seconds = counter_seconds - 3600;
  219.     counter_hours = counter_hours + 1;
  220.   }
  221.   while (counter_seconds >= 60) {
  222.     counter_seconds = counter_seconds - 60;
  223.     counter_minutes = counter_minutes + 1;
  224.   }
  225. }
  226.  
  227.  
  228. void show_locked_message() {
  229.   lcd.setCursor(0, 0);
  230.   lcd.print("EST. TIME TO BREACH:");
  231.   // Note: this does not update the time.
  232.   lcd.setCursor(0, 2);
  233.   lcd.print("IMPUT 4 DIGIT RESET");
  234.   lcd.setCursor(3, 3);
  235.   lcd.print("CODE TO KEYPAD");
  236. }
  237.  
  238. void show_unlocked_message() {
  239.   lcd.clear();
  240.   lcd.setCursor(2, 1);
  241.   lcd.print("BREACH CONTAINED"); //give confirmation of job well done
  242.   delay(20000); //this should really be untill code is reset but for now its fine
  243. }
  244.  
  245. void check_countdown_timer() {
  246.   if (timer_is_running == true) {
  247.     if (total_seconds > 0) {
  248.       // If there is still time left:
  249.       one_second_current_time = millis();
  250.       if (one_second_current_time >= one_second_start_time) {
  251.         if (one_second_current_time >= (one_second_start_time + 1000)) {
  252.           total_seconds--;
  253.           convert_total_seconds_to_h_m_s();
  254.           display_remaining_time_on_lcd();
  255.           one_second_start_time = millis();
  256.         }
  257.       }
  258.       else {
  259.         one_second_start_time = millis();
  260.       }
  261.     }
  262.     else {
  263.       // This happens if you run out of time:
  264.       show_time_expired_message();
  265.     }
  266.   }
  267. }
  268.  
  269. void display_remaining_time_on_lcd() {
  270.   // The time is printed on the second line down (#1).
  271.   // hours --------------------------------
  272.   lcd_clear_line2();
  273.   if (counter_hours > 9)
  274.   {
  275.     lcd.setCursor(6, 1);
  276.     lcd.print (counter_hours);
  277.   }
  278.   else
  279.   {
  280.     lcd.setCursor(6, 1);
  281.     lcd.print("0");
  282.     // lcd.setCursor(7, 1);
  283.     lcd.print(counter_hours);
  284.   }
  285.   // ------------------------------------------------
  286.   //lcd.setCursor(8, 1); // do not need
  287.   lcd.print(":");
  288.   // minutes -------------------------------------------
  289.   if (counter_minutes > 9)
  290.   {
  291.     lcd.setCursor(9, 1);
  292.     lcd.print(counter_minutes);
  293.   }
  294.   else
  295.   {
  296.     lcd.setCursor(9, 1);
  297.     lcd.print("0");
  298.     // lcd.setCursor(10, 1);
  299.     lcd.print(counter_minutes);
  300.   }
  301.   // -------------------------------------------------
  302.   //lcd.setCursor(11, 1); // do not need
  303.   lcd.print(":");
  304.   // seconds --------------------------------------------
  305.   if (counter_seconds > 9)
  306.   {
  307.     lcd.setCursor(12, 1);
  308.     lcd.print(counter_seconds);
  309.   }
  310.   else
  311.   {
  312.     lcd.setCursor(12, 1);
  313.     lcd.print("0");
  314.     // lcd.setCursor(13, 1);
  315.     lcd.print(counter_seconds);
  316.   }
  317. }
  318.  
  319.  
  320. void show_time_expired_message() {
  321.   timer_is_running = false;
  322.   lcd.clear();
  323.   lcd.setCursor(2, 1);
  324.   lcd.print("YOU DED BITCH"); //give confirmation of all dead
  325.   delay(20000); //this should really be untill code is reset but for now its fine
  326. }
  327.  
  328. void check_keypad_for_new_key_press() {
  329.   temporary_char = myKeypad.getKey(); // getKey returns false if no key pressed.
  330.   Serial.print("check_keypad_for_new_key_press() keypad entry = ");
  331.   Serial.println(temporary_char);
  332.   if (check_if_char_is_a_digit(temporary_char)) {
  333.     // If the character is a digit:
  334.     if (temporary_char == password[pozisyon]) {
  335.       pozisyon++;
  336.       Serial.print("check_keypad_for_new_key_press() correct entry: pozisyon = ");
  337.       Serial.println(pozisyon);
  338.     }
  339.     else {
  340.       Serial.print("check_keypad_for_new_key_press() incorrect entry: pozisyon = ");
  341.       Serial.println(pozisyon);
  342.     }
  343.     temporary_char = 0;
  344.   }
  345.   else {
  346.     show_invalid_key_message();
  347.   }
  348. }
  349.  
  350. bool check_if_char_is_a_digit(char thisChar) {
  351.   bool testResult = false;
  352.   if (thisChar > 47 && thisChar < 58) { // ASCII 47 == zero and ASCII 57 = nine.
  353.     // This character is a digit.
  354.     Serial.println("check_if_char_is_a_digit(char thisChar) digit test = true");
  355.     testResult = true;
  356.   }
  357.   else {
  358.     Serial.println("check_if_char_is_a_digit(char thisChar) digit test = false");
  359.   }
  360.   return testResult;
  361. }
  362.  
  363. void check_to_enable_keypad() {
  364.   // If the keypad is disabled by the software timer, this will re-enable it after keypad_check_interval has passed.
  365.   keypad_check_current_time = millis();
  366.   if (keypad_check_current_time >= keypad_check_begin_time) {
  367.     if (keypad_check_current_time >= (keypad_check_begin_time + keypad_check_interval)) {
  368.       keypad_enabled = true;
  369.     }
  370.   }
  371.   else {
  372.     keypad_check_begin_time = millis();
  373.   }
  374. }
  375.  
  376. void check_invalid_key_message() {
  377.   if (invalid_key_entered == true) {
  378.     unvalid_key_message_current_time = millis();
  379.     if (unvalid_key_message_current_time >= unvalid_key_message_begin_time) {
  380.       if (unvalid_key_message_current_time >= (unvalid_key_message_begin_time + invalid_key_message_timer)) {
  381.         invalid_key_entered = false;
  382.         show_normal_top_line();
  383.       }
  384.     }
  385.     else {
  386.       unvalid_key_message_begin_time = millis();
  387.     }
  388.   }
  389. }
  390.  
  391. void show_normal_top_line() {
  392.   lcd_clear_line1();
  393.   lcd.print("EST. TIME TO BREACH:");
  394. }
  395.  
  396. void show_invalid_key_message() {
  397.   unvalid_key_message_begin_time = millis();
  398.   invalid_key_entered = true;
  399.   lcd_clear_line1();
  400.   lcd.print(" Invalid Key");
  401. }
  402.  
  403.  
  404. void setLocked(bool thisValue) {
  405.   // This is a do-nothing function, that was missing from the code provided?
  406.   // ,,,,,,,, This is to lock or unlock the [whatever]
  407.   if (thisValue == false) {
  408.     // unlock the whatever here
  409.   }
  410.   else {
  411.     // lock the whatever here
  412.   }
  413. }
  414. void lcd_clear_all_lines() {
  415.   lcd_clear_line1();
  416.   lcd_clear_line2();
  417.   lcd_clear_line3();
  418.   lcd_clear_line4();
  419. }
  420.  
  421. void lcd_clear_line1() {
  422.   lcd.setCursor(0, 0);
  423.   lcd.print("                    ");
  424.   lcd.setCursor(0, 0);
  425. }
  426.  
  427. void lcd_clear_line2() {
  428.   lcd.setCursor(0, 1);
  429.   lcd.print("                    ");
  430.   lcd.setCursor(0, 1);
  431. }
  432.  
  433. void lcd_clear_line3() {
  434.   lcd.setCursor(20, 0);
  435.   lcd.print("                    ");
  436.   lcd.setCursor(20, 0);
  437. }
  438.  
  439. void lcd_clear_line4() {
  440.   lcd.setCursor(20, 1);
  441.   lcd.print("                    ");
  442.   lcd.setCursor(20, 1);
  443. }
Add Comment
Please, Sign In to add comment