Advertisement
pleasedontcode

fakeB

Dec 2nd, 2023 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 11.17 KB | Source Code | 0 0
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: FakeBomb
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-03 01:29:44
  15.     - Source Code generated by: Francesco Alessandro
  16.  
  17. ********* Pleasedontcode.com **********/
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Il programma inizia visualizzando una barra di */
  21.     /* caricamento sull'LCD. Successivamente deve partire */
  22.     /* un timer di 10 minuti e sull'LCD dev'essere */
  23.     /* visualizzato "INSERIRE PASSWORD" e nel frattempo */
  24.     /* dev'essere riprodotta la "traccia1.mp3" dal */
  25.     /* DFPlayerMini. */
  26. /****** SYSTEM REQUIREMENT 2 *****/
  27.     /* Attraverso il keypad dev'essere monitorata */
  28.     /* l'introduzione di un codice composto da 4 numeri e */
  29.     /* finalizzato con carattere #. */
  30. /****** SYSTEM REQUIREMENT 3 *****/
  31.     /* Se i 4 numeri sono uguali alla password "5421" */
  32.     /* allora deve accendersi il led verde e sull'LCD */
  33.     /* deve comparire la scritta "CODICE CORRETTO, BOMBA */
  34.     /* DISINNESCATA". Se sbagliato "CODICE SBAGLIATO, */
  35.     /* RIPROVA". */
  36. /****** SYSTEM REQUIREMENT 4 *****/
  37.     /* Durante il countdown deve lampeggiare il led BLU. */
  38.     /* Se il countdown raggiunge 10 secondi allora la */
  39.     /* traccia audio "traccia2.mp3" e "traccia3.mp3" */
  40.     /* devono essere riprodotte. */
  41. /****** SYSTEM REQUIREMENT 5 *****/
  42.     /* Se il countdown termina e non viene inserita la */
  43.     /* password giusta allora il led rosso deve */
  44.     /* accendersi ad intermittenza e devono essere */
  45.     /* riprodotte le tracce mp3 "traccia4.mp3", */
  46.     /* "traccia5.mp3" e "traccia6.mp3". */
  47. /****** END SYSTEM REQUIREMENTS *****/
  48.  
  49. /********* User code review feedback **********
  50. #### Feedback 1 ####
  51. - il countdown deve partire subito all'avvio e non essere dipenden
  52. te dall'inserimento della password
  53. ********* User code review feedback **********/
  54.  
  55. /****** DEFINITION OF LIBRARIES *****/
  56. #include <SoftwareSerial.h>
  57. #include <Wire.h>
  58. #include <DFRobotDFPlayerMini.h>
  59. #include <LiquidCrystal_I2C.h>
  60. #include <Keypad.h>
  61.  
  62. /****** SYSTEM REQUIREMENTS *****/
  63. const unsigned long TIMER_DURATION = 20000; //10 * 60 * 1000; // 10 minutes in milliseconds
  64. const char PASSWORD[5] = "5421";
  65. const char CORRECT_CODE_MESSAGE[] = "CODICE CORRETTO, BOMBA DISINNESCATA";
  66. const char WRONG_CODE_MESSAGE[] = "CODICE SBAGLIATO, RIPROVA";
  67. const char INSERT_PASSWORD_MESSAGE[] = "INSERIRE PASSWORD";
  68. const char COUNTDOWN_MESSAGE[] = "COUNTDOWN: ";
  69. const char MP3_TRACK1_FOLDER[] = "traccia1.mp3";
  70. const char MP3_TRACK2_FOLDER[] = "traccia2.mp3";
  71. const char MP3_TRACK3_FOLDER[] = "traccia3.mp3";
  72. const char MP3_TRACK4_FOLDER[] = "traccia4.mp3";
  73. const char MP3_TRACK5_FOLDER[] = "traccia5.mp3";
  74. const char MP3_TRACK6_FOLDER[] = "traccia6.mp3";
  75.  
  76. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  77. const uint8_t pad_Keypad4x4_R1_PIN_D2   = 2;
  78. const uint8_t pad_Keypad4x4_R2_PIN_D3   = 3;
  79. const uint8_t pad_Keypad4x4_R3_PIN_D4   = 4;
  80. const uint8_t pad_Keypad4x4_R4_PIN_D5   = 5;
  81.  
  82. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  83. const uint8_t pad_Keypad4x4_C1_PIN_D6   = 6;
  84. const uint8_t pad_Keypad4x4_C2_PIN_D7   = 7;
  85. const uint8_t pad_Keypad4x4_C3_PIN_D8   = 8;
  86. const uint8_t pad_Keypad4x4_C4_PIN_D9   = 9;
  87. const uint8_t VERDE_LED_PIN_D11         = 11;
  88. const uint8_t ROSSO_LED_PIN_D12         = 12;
  89. const uint8_t BLU_LED_PIN_D10           = 10;
  90.  
  91. /***** DEFINITION OF Software Serial *****/
  92. const uint8_t mp3_DFPlayerMini_Serial_PIN_SERIAL_TX_A0 = A0;
  93. const uint8_t mp3_DFPlayerMini_Serial_PIN_SERIAL_RX_A1 = A1;
  94. SoftwareSerial mp3_DFPlayerMini_Serial(mp3_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, mp3_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);
  95.  
  96. /***** DEFINITION OF I2C PINS *****/
  97. const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_A4         = A4;
  98. const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_A5         = A5;
  99. const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS      = 0x27;
  100.  
  101. const uint8_t KEYPAD4X4_ROWS = 4; //four rows
  102. const uint8_t KEYPAD4X4_COLS = 4; //four columns
  103. char hexaKeys_Keypad4x4[KEYPAD4X4_ROWS][KEYPAD4X4_COLS] = {
  104.     {'1','2','3','A'},
  105.     {'4','5','6','B'},
  106.     {'7','8','9','C'},
  107.     {'*','0','#','D'},
  108. };
  109.  
  110. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  111. DFRobotDFPlayerMini mp3Player;
  112. LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 20, 4);
  113. Keypad customKeypad = Keypad(makeKeymap(hexaKeys_Keypad4x4),
  114.                              (byte[]) {pad_Keypad4x4_R1_PIN_D2, pad_Keypad4x4_R2_PIN_D3, pad_Keypad4x4_R3_PIN_D4, pad_Keypad4x4_R4_PIN_D5},
  115.                              (byte[]) {pad_Keypad4x4_C1_PIN_D6, pad_Keypad4x4_C2_PIN_D7, pad_Keypad4x4_C3_PIN_D8, pad_Keypad4x4_C4_PIN_D9},
  116.                              KEYPAD4X4_ROWS, KEYPAD4X4_COLS);
  117.  
  118. bool isPasswordCorrect = false; // Set password as correct initially
  119. bool isCountdownStarted = true; // Start countdown immediately upon startup
  120. unsigned long countdownStartTime = 0;
  121.  
  122. void setup()
  123. {
  124.     Serial.begin(9600);
  125.     delay(1000);
  126.     Serial.println(TIMER_DURATION);
  127.     // put your setup code here, to run once:
  128.     pinMode(pad_Keypad4x4_R1_PIN_D2, INPUT_PULLUP);
  129.     pinMode(pad_Keypad4x4_R2_PIN_D3, INPUT_PULLUP);
  130.     pinMode(pad_Keypad4x4_R3_PIN_D4, INPUT_PULLUP);
  131.     pinMode(pad_Keypad4x4_R4_PIN_D5, INPUT_PULLUP);
  132.  
  133.     pinMode(pad_Keypad4x4_C1_PIN_D6, OUTPUT);
  134.     pinMode(pad_Keypad4x4_C2_PIN_D7, OUTPUT);
  135.     pinMode(pad_Keypad4x4_C3_PIN_D8, OUTPUT);
  136.     pinMode(pad_Keypad4x4_C4_PIN_D9, OUTPUT);
  137.     pinMode(VERDE_LED_PIN_D11, OUTPUT);
  138.     pinMode(ROSSO_LED_PIN_D12, OUTPUT);
  139.     pinMode(BLU_LED_PIN_D10, OUTPUT);
  140.  
  141.     mp3_DFPlayerMini_Serial.begin(9600);
  142.     mp3Player.begin(mp3_DFPlayerMini_Serial);
  143.  
  144.     lcd.init();
  145.     lcd.backlight();
  146.     lcd.setCursor(0, 0);
  147.     lcd.print("Caricamento...");
  148.  
  149.     delay(2000); // Delay for the loading screen
  150.  
  151.     digitalWrite(BLU_LED_PIN_D10, HIGH); // Turn on BLU LED
  152.  
  153.     lcd.clear();
  154.     lcd.setCursor(0, 0);
  155.     lcd.print(INSERT_PASSWORD_MESSAGE);
  156.  
  157.     mp3Player.volume(10); // Set volume to a reasonable level
  158.     mp3Player.playFolder(1, 1); // Play "traccia1.mp3" from folder 1
  159. }
  160.  
  161. void loop()
  162. {
  163.     // put your main code here, to run repeatedly:
  164.     char customKey = customKeypad.getKey();
  165.    
  166.     if (customKey)
  167.     {
  168.         Serial.println(customKey);
  169.         if (customKey == '#')
  170.         {
  171.             if (isPasswordCorrect)
  172.             {
  173.                 digitalWrite(VERDE_LED_PIN_D11, HIGH); // Turn on VERDE LED
  174.                 lcd.clear();
  175.                 lcd.setCursor(0, 0);
  176.                 lcd.print(CORRECT_CODE_MESSAGE);
  177.                 while(1);
  178.             }
  179.             else
  180.             {
  181.                 digitalWrite(ROSSO_LED_PIN_D12, HIGH); // Turn on ROSSO LED
  182.                 lcd.clear();
  183.                 lcd.setCursor(0, 0);
  184.                 lcd.print(WRONG_CODE_MESSAGE);
  185.                 delay(1000);
  186.                 digitalWrite(ROSSO_LED_PIN_D12, LOW); // Turn off ROSSO LED
  187.                 mp3Player.enableLoopAll();
  188.                 mp3Player.playFolder(2, 1); // Play "traccia4.mp3" from folder 2
  189.                 mp3Player.playFolder(2, 2); // Play "traccia5.mp3" from folder 2
  190.                 mp3Player.playFolder(2, 3); // Play "traccia6.mp3" from folder 2
  191.                 while(1);
  192.             }
  193.         }
  194.         else
  195.         {
  196.             isPasswordCorrect = checkPassword(customKey); // Check if the entered password is correct
  197.         }
  198.     }
  199.  
  200.     if (!isCountdownStarted && isPasswordCorrect)
  201.     {
  202.         startCountdown(); // Start the countdown if the password is correct
  203.     }
  204.  
  205.     if (isCountdownStarted)
  206.     {
  207.         unsigned long currentTime = millis();
  208.         unsigned long elapsedTime = currentTime - countdownStartTime;
  209.         if (elapsedTime >= TIMER_DURATION)
  210.         {
  211.             countdownFinished(); // Countdown has finished, handle the situation
  212.         }
  213.         else
  214.         {
  215.             updateCountdownToLCD(elapsedTime); // Update the countdown on the LCD
  216.         }
  217.  
  218.         if (elapsedTime >= (TIMER_DURATION - 10000))
  219.         {
  220.             playAdditionalTracks(); // Play additional tracks when 10 seconds left in the countdown
  221.         }
  222.     }
  223. }
  224.  
  225. bool checkPassword(char customKey)
  226. {
  227.     static char enteredPassword[5] = "";
  228.     static uint8_t passwordIndex = 0;
  229.  
  230.     if (customKey >= '0' && customKey <= '9')
  231.     {
  232.         enteredPassword[passwordIndex++] = customKey;
  233.         lcd.setCursor(passwordIndex - 1, 0);
  234.         lcd.print("*");
  235.  
  236.         if (passwordIndex == 4)
  237.         {
  238.             enteredPassword[passwordIndex] = '\0'; // Null-terminate the entered password string
  239.             passwordIndex = 0;                      // Reset the password index for future use
  240.            
  241.             Serial.println(enteredPassword);
  242.             Serial.println(PASSWORD);
  243.             if (strcmp(enteredPassword, PASSWORD) == 0)
  244.             {  
  245.                 Serial.println("PSW GIUSTA");
  246.                 return true; // Password is correct
  247.             }
  248.             else
  249.             {
  250.                 lcd.clear();
  251.                 lcd.setCursor(0, 0);
  252.                 lcd.print(WRONG_CODE_MESSAGE);
  253.                 delay(1000);
  254.                 lcd.clear();
  255.                 lcd.setCursor(0, 0);
  256.                 lcd.print(INSERT_PASSWORD_MESSAGE);
  257.             }
  258.         }
  259.     }
  260.  
  261.     return false;
  262. }
  263.  
  264. void startCountdown()
  265. {
  266.     isCountdownStarted = true;
  267.     countdownStartTime = millis();
  268. }
  269.  
  270. void updateCountdownToLCD(unsigned long elapsedTime)
  271. {
  272.     unsigned long remainingTime = TIMER_DURATION - elapsedTime;
  273.     unsigned long minutes = remainingTime / 60000;
  274.     unsigned long seconds = (remainingTime % 60000) / 1000;
  275.  
  276.     lcd.setCursor(0, 1);
  277.     lcd.print(COUNTDOWN_MESSAGE);
  278.  
  279.     if (minutes < 10)
  280.     {
  281.         lcd.print("0");
  282.     }
  283.     lcd.print(minutes);
  284.     lcd.print(":");
  285.     if (seconds < 10)
  286.     {
  287.         lcd.print("0");
  288.     }
  289.     lcd.print(seconds);
  290.  
  291.     // Blink the BLU LED
  292.     unsigned long blinkInterval = 500;
  293.     if ((elapsedTime / blinkInterval) % 2 == 0)
  294.     {
  295.         digitalWrite(BLU_LED_PIN_D10, HIGH);
  296.     }
  297.     else
  298.     {
  299.         digitalWrite(BLU_LED_PIN_D10, LOW);
  300.     }
  301. }
  302.  
  303. void playAdditionalTracks()
  304. {
  305.     mp3Player.playFolder(2, 4); // Play "traccia2.mp3" from folder 2
  306.     mp3Player.playFolder(2, 5); // Play "traccia3.mp3" from folder 2
  307. }
  308.  
  309. void countdownFinished()
  310. {
  311.     isCountdownStarted = false;
  312.     isPasswordCorrect = false;
  313.     countdownStartTime = 0;
  314.  
  315.     digitalWrite(ROSSO_LED_PIN_D12, HIGH); // Turn on ROSSO LED
  316.  
  317.     lcd.clear();
  318.     lcd.setCursor(0, 0);
  319.     lcd.print(WRONG_CODE_MESSAGE);
  320.     delay(1000);
  321.  
  322.     digitalWrite(ROSSO_LED_PIN_D12, LOW); // Turn off ROSSO LED
  323.  
  324.     mp3Player.enableLoopAll();
  325.     mp3Player.playFolder(2, 4); // Play "traccia2.mp3" from folder 2
  326.     mp3Player.playFolder(2, 5); // Play "traccia3.mp3" from folder 2
  327.     mp3Player.playFolder(2, 6); // Play "traccia6.mp3" from folder 2
  328. }
  329.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement