CCCPaste
/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: FakeBomb
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2023-12-03 01:29:44
	- Source Code generated by: Francesco Alessandro

********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Il programma inizia visualizzando una barra di */
	/* caricamento sull'LCD. Successivamente deve partire */
	/* un timer di 10 minuti e sull'LCD dev'essere */
	/* visualizzato "INSERIRE PASSWORD" e nel frattempo */
	/* dev'essere riprodotta la "traccia1.mp3" dal */
	/* DFPlayerMini. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* Attraverso il keypad dev'essere monitorata */
	/* l'introduzione di un codice composto da 4 numeri e */
	/* finalizzato con carattere #. */
/****** SYSTEM REQUIREMENT 3 *****/
	/* Se i 4 numeri sono uguali alla password "5421" */
	/* allora deve accendersi il led verde e sull'LCD */
	/* deve comparire la scritta "CODICE CORRETTO, BOMBA */
	/* DISINNESCATA". Se sbagliato "CODICE SBAGLIATO, */
	/* RIPROVA". */
/****** SYSTEM REQUIREMENT 4 *****/
	/* Durante il countdown deve lampeggiare il led BLU. */
	/* Se il countdown raggiunge 10 secondi allora la */
	/* traccia audio "traccia2.mp3" e "traccia3.mp3" */
	/* devono essere riprodotte. */
/****** SYSTEM REQUIREMENT 5 *****/
	/* Se il countdown termina e non viene inserita la */
	/* password giusta allora il led rosso deve */
	/* accendersi ad intermittenza e devono essere */
	/* riprodotte le tracce mp3 "traccia4.mp3", */
	/* "traccia5.mp3" e "traccia6.mp3". */
/****** END SYSTEM REQUIREMENTS *****/

/********* User code review feedback **********
#### Feedback 1 ####
- il countdown deve partire subito all'avvio e non essere dipenden
te dall'inserimento della password
********* User code review feedback **********/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include 
#include 
#include  

/****** SYSTEM REQUIREMENTS *****/
const unsigned long TIMER_DURATION = 20000; //10 * 60 * 1000; // 10 minutes in milliseconds
const char PASSWORD[5] = "5421";
const char CORRECT_CODE_MESSAGE[] = "CODICE CORRETTO, BOMBA DISINNESCATA";
const char WRONG_CODE_MESSAGE[] = "CODICE SBAGLIATO, RIPROVA";
const char INSERT_PASSWORD_MESSAGE[] = "INSERIRE PASSWORD";
const char COUNTDOWN_MESSAGE[] = "COUNTDOWN: ";
const char MP3_TRACK1_FOLDER[] = "traccia1.mp3";
const char MP3_TRACK2_FOLDER[] = "traccia2.mp3";
const char MP3_TRACK3_FOLDER[] = "traccia3.mp3";
const char MP3_TRACK4_FOLDER[] = "traccia4.mp3";
const char MP3_TRACK5_FOLDER[] = "traccia5.mp3";
const char MP3_TRACK6_FOLDER[] = "traccia6.mp3";

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t pad_Keypad4x4_R1_PIN_D2   = 2;
const uint8_t pad_Keypad4x4_R2_PIN_D3   = 3;
const uint8_t pad_Keypad4x4_R3_PIN_D4   = 4;
const uint8_t pad_Keypad4x4_R4_PIN_D5   = 5;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t pad_Keypad4x4_C1_PIN_D6   = 6;
const uint8_t pad_Keypad4x4_C2_PIN_D7   = 7;
const uint8_t pad_Keypad4x4_C3_PIN_D8   = 8;
const uint8_t pad_Keypad4x4_C4_PIN_D9   = 9;
const uint8_t VERDE_LED_PIN_D11         = 11;
const uint8_t ROSSO_LED_PIN_D12         = 12;
const uint8_t BLU_LED_PIN_D10           = 10;

/***** DEFINITION OF Software Serial *****/
const uint8_t mp3_DFPlayerMini_Serial_PIN_SERIAL_TX_A0 = A0;
const uint8_t mp3_DFPlayerMini_Serial_PIN_SERIAL_RX_A1 = A1;
SoftwareSerial mp3_DFPlayerMini_Serial(mp3_DFPlayerMini_Serial_PIN_SERIAL_RX_A1, mp3_DFPlayerMini_Serial_PIN_SERIAL_TX_A0);

/***** DEFINITION OF I2C PINS *****/
const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_A4         = A4;
const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_A5         = A5;
const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS      = 0x27;

const uint8_t KEYPAD4X4_ROWS = 4; //four rows
const uint8_t KEYPAD4X4_COLS = 4; //four columns
char hexaKeys_Keypad4x4[KEYPAD4X4_ROWS][KEYPAD4X4_COLS] = {
    {'1','2','3','A'},
    {'4','5','6','B'},
    {'7','8','9','C'},
    {'*','0','#','D'},
};

/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
DFRobotDFPlayerMini mp3Player;
LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 20, 4);
Keypad customKeypad = Keypad(makeKeymap(hexaKeys_Keypad4x4), 
                             (byte[]) {pad_Keypad4x4_R1_PIN_D2, pad_Keypad4x4_R2_PIN_D3, pad_Keypad4x4_R3_PIN_D4, pad_Keypad4x4_R4_PIN_D5}, 
                             (byte[]) {pad_Keypad4x4_C1_PIN_D6, pad_Keypad4x4_C2_PIN_D7, pad_Keypad4x4_C3_PIN_D8, pad_Keypad4x4_C4_PIN_D9}, 
                             KEYPAD4X4_ROWS, KEYPAD4X4_COLS);

bool isPasswordCorrect = false; // Set password as correct initially
bool isCountdownStarted = true; // Start countdown immediately upon startup
unsigned long countdownStartTime = 0;

void setup()
{
    Serial.begin(9600);
    delay(1000);
    Serial.println(TIMER_DURATION);
    // put your setup code here, to run once:
    pinMode(pad_Keypad4x4_R1_PIN_D2, INPUT_PULLUP);
    pinMode(pad_Keypad4x4_R2_PIN_D3, INPUT_PULLUP);
    pinMode(pad_Keypad4x4_R3_PIN_D4, INPUT_PULLUP);
    pinMode(pad_Keypad4x4_R4_PIN_D5, INPUT_PULLUP);

    pinMode(pad_Keypad4x4_C1_PIN_D6, OUTPUT);
    pinMode(pad_Keypad4x4_C2_PIN_D7, OUTPUT);
    pinMode(pad_Keypad4x4_C3_PIN_D8, OUTPUT);
    pinMode(pad_Keypad4x4_C4_PIN_D9, OUTPUT);
    pinMode(VERDE_LED_PIN_D11, OUTPUT);
    pinMode(ROSSO_LED_PIN_D12, OUTPUT);
    pinMode(BLU_LED_PIN_D10, OUTPUT);

    mp3_DFPlayerMini_Serial.begin(9600);
    mp3Player.begin(mp3_DFPlayerMini_Serial);

    lcd.init();
    lcd.backlight();
    lcd.setCursor(0, 0);
    lcd.print("Caricamento...");

    delay(2000); // Delay for the loading screen

    digitalWrite(BLU_LED_PIN_D10, HIGH); // Turn on BLU LED

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(INSERT_PASSWORD_MESSAGE);

    mp3Player.volume(10); // Set volume to a reasonable level
    mp3Player.playFolder(1, 1); // Play "traccia1.mp3" from folder 1
}

void loop()
{
    // put your main code here, to run repeatedly:
    char customKey = customKeypad.getKey();
    
    if (customKey)
    {
        Serial.println(customKey);
        if (customKey == '#')
        {
            if (isPasswordCorrect)
            {
                digitalWrite(VERDE_LED_PIN_D11, HIGH); // Turn on VERDE LED
                lcd.clear();
                lcd.setCursor(0, 0);
                lcd.print(CORRECT_CODE_MESSAGE);
                while(1);
            }
            else
            {
                digitalWrite(ROSSO_LED_PIN_D12, HIGH); // Turn on ROSSO LED
                lcd.clear();
                lcd.setCursor(0, 0);
                lcd.print(WRONG_CODE_MESSAGE);
                delay(1000);
                digitalWrite(ROSSO_LED_PIN_D12, LOW); // Turn off ROSSO LED
                mp3Player.enableLoopAll();
                mp3Player.playFolder(2, 1); // Play "traccia4.mp3" from folder 2
                mp3Player.playFolder(2, 2); // Play "traccia5.mp3" from folder 2
                mp3Player.playFolder(2, 3); // Play "traccia6.mp3" from folder 2
                while(1);
            }
        }
        else
        {
            isPasswordCorrect = checkPassword(customKey); // Check if the entered password is correct
        }
    }

    if (!isCountdownStarted && isPasswordCorrect)
    {
        startCountdown(); // Start the countdown if the password is correct
    }

    if (isCountdownStarted)
    {
        unsigned long currentTime = millis();
        unsigned long elapsedTime = currentTime - countdownStartTime;
        if (elapsedTime >= TIMER_DURATION)
        {
            countdownFinished(); // Countdown has finished, handle the situation
        }
        else
        {
            updateCountdownToLCD(elapsedTime); // Update the countdown on the LCD
        }

        if (elapsedTime >= (TIMER_DURATION - 10000))
        {
            playAdditionalTracks(); // Play additional tracks when 10 seconds left in the countdown
        }
    }
}

bool checkPassword(char customKey)
{
    static char enteredPassword[5] = "";
    static uint8_t passwordIndex = 0;

    if (customKey >= '0' && customKey <= '9')
    {
        enteredPassword[passwordIndex++] = customKey;
        lcd.setCursor(passwordIndex - 1, 0);
        lcd.print("*");

        if (passwordIndex == 4)
        {
            enteredPassword[passwordIndex] = '\0'; // Null-terminate the entered password string
            passwordIndex = 0;                      // Reset the password index for future use
            
            Serial.println(enteredPassword);
            Serial.println(PASSWORD);
            if (strcmp(enteredPassword, PASSWORD) == 0)
            {   
                Serial.println("PSW GIUSTA");
                return true; // Password is correct
            }
            else
            {
                lcd.clear();
                lcd.setCursor(0, 0);
                lcd.print(WRONG_CODE_MESSAGE);
                delay(1000);
                lcd.clear();
                lcd.setCursor(0, 0);
                lcd.print(INSERT_PASSWORD_MESSAGE);
            }
        }
    }

    return false;
}

void startCountdown()
{
    isCountdownStarted = true;
    countdownStartTime = millis();
}

void updateCountdownToLCD(unsigned long elapsedTime)
{
    unsigned long remainingTime = TIMER_DURATION - elapsedTime;
    unsigned long minutes = remainingTime / 60000;
    unsigned long seconds = (remainingTime % 60000) / 1000;

    lcd.setCursor(0, 1);
    lcd.print(COUNTDOWN_MESSAGE);

    if (minutes < 10)
    {
        lcd.print("0");
    }
    lcd.print(minutes);
    lcd.print(":");
    if (seconds < 10)
    {
        lcd.print("0");
    }
    lcd.print(seconds);

    // Blink the BLU LED
    unsigned long blinkInterval = 500;
    if ((elapsedTime / blinkInterval) % 2 == 0)
    {
        digitalWrite(BLU_LED_PIN_D10, HIGH);
    }
    else
    {
        digitalWrite(BLU_LED_PIN_D10, LOW);
    }
}

void playAdditionalTracks()
{
    mp3Player.playFolder(2, 4); // Play "traccia2.mp3" from folder 2
    mp3Player.playFolder(2, 5); // Play "traccia3.mp3" from folder 2
}

void countdownFinished()
{
    isCountdownStarted = false;
    isPasswordCorrect = false;
    countdownStartTime = 0;

    digitalWrite(ROSSO_LED_PIN_D12, HIGH); // Turn on ROSSO LED

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(WRONG_CODE_MESSAGE);
    delay(1000);

    digitalWrite(ROSSO_LED_PIN_D12, LOW); // Turn off ROSSO LED

    mp3Player.enableLoopAll();
    mp3Player.playFolder(2, 4); // Play "traccia2.mp3" from folder 2
    mp3Player.playFolder(2, 5); // Play "traccia3.mp3" from folder 2
    mp3Player.playFolder(2, 6); // Play "traccia6.mp3" from folder 2
}