Hello,
Im learning Arduino and trying a FUNBOX(escape room) project with a display, a remote to start the countdown, lcd and keypad to stop the countdown.
But, Im stuck on password check, when i type in the wrong password there is a delay on the countdown display.
And i need stop the countdown if insert correct password to after reset it if i need with remote.
thanks
#include "TM1637Display.h"
#include "IRremote.h"
#include "LiquidCrystal.h"
#include "Keypad.h"
#include "Password.h"
#define numberofseconds(_time_) ((_time_ / 1000) % 60)
#define numberofminutes(_time_) (((_time_ / 1000) /60) % 60)
const uint8_t ZEROS[] = {0b00111111, 0b00111111, 0b00111111, 0b00111111 };
const uint8_t OFF[] = {0, 0, 0, 0};
// in the libary, the byte order is .GFEDCBA
// .GFEDCBA
const uint8_t PLAY[] = {B01110011, B00111000, B01110111, B01101110};
const int RECV_PIN = 10;
IRrecv irrecv(RECV_PIN);
decode_results results;
//clock, data
TM1637Display display(6, 7);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// 1000ms in one sec, 1000x60x60 = 3600000ms = 1hour 300000 = 5min
const unsigned long timeLimit = 900000;
unsigned long timeStart;
bool bcountdownDone;
const byte ROWS = 4;
const byte COLS = 4;
byte maxPasswordLength = 6;
byte currentPasswordLength = 0;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {A0, A1, A2, 9};
byte colPins[COLS] = {A3, A4, A5, 8};
int count=0;
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
Password password = Password( "1234" );
Password password2 = Password( "1235" );
Password password3 = Password( "1236" );
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
display.setBrightness(0x0d);
display.setSegments(OFF);
display.setSegments(PLAY);
timeStart = millis(); //used to time display of PLAY
irrecv.enableIRIn();
irrecv.blink13(true);
int value = (1234 / 100)%10;
Serial.println( value );
keypad.setDebounceTime(50);
displayCodeEntryScreen();
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}//setup
#define ST_SHOWPLAY 0
#define ST_COUNT 1
#define ST_FLASH_ZEROS 2
void countdown()
{
static byte
stateCountdown = ST_SHOWPLAY;
//the vars used by countdown state
static bool
bcountdownDone = false;
int
seconds,
minutes;
static int
lastseconds = -1;
unsigned long
timeRemaining,
timeElapsed;
switch( stateCountdown )
{
case ST_SHOWPLAY:
//PLAY is showing from setup...time to start countdown?
if(irrecv.decode(&results))
{
irrecv.resume();
if( results.value == 0xFF42BD )
{
stateCountdown = ST_COUNT;
timeStart = millis();
}
}//if
break;
case ST_COUNT:
timeElapsed = millis() - timeStart;
timeRemaining = timeLimit - timeElapsed;
seconds = numberofseconds(timeRemaining);
minutes = numberofminutes(timeRemaining);
if( seconds != lastseconds )
{
lastseconds = seconds;
display.showNumberDecEx(seconds, 0, true, 2, 2);
display.showNumberDecEx(minutes, 0b01000000, true, 2, 0);
}//if
if( seconds == 0 && minutes == 0 )
{
stateCountdown = ST_FLASH_ZEROS;
}//if
break;
case ST_FLASH_ZEROS:
FlashZeros();
break;
}//switch
}//countdown
void loop()
{
countdown();
if(irrecv.decode(&results)){
irrecv.resume();
if( results.value == 0xFF52AD )
{
software_Reset();
}
}
keypad.getKey();
}//loop
void FlashZeros( void )
{
static bool
bState = true; //reflect that countdown left digits at 0000 to begin
static unsigned long
timeFlash = 0;
if( millis() - timeFlash > 300 )
{
timeFlash = millis();
if( bState )
{
display.setSegments(OFF);
bState = false;
}//if
else
{
display.setSegments(ZEROS);
bState = true;
}//else
}//if
}//FlashZeros
void displayCodeEntryScreen()
{
password.reset();
lcd.clear();
lcd.print("Senha:");
lcd.setCursor(0,1);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
//setup and turn off both LEDs
}
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
lcd.print(eKey);
switch (eKey){
case '#':
checkPassword();
break;
case '*':
displayCodeEntryScreen(); break;
default:
password.append(eKey);
}
switch (keypad.getState()){
case PRESSED:
switch (eKey){
case 'D': displayCodeEntryScreen();
}
}
}
}
void checkPassword(){
if (password.evaluate()){
lcd.clear();
delay(30);
lcd.setCursor(0, 0);
lcd.print("Boa!");
lcd.setCursor(4, 1);
lcd.print("Welcome");
delay(2500);
displayCodeEntryScreen();
}
else{
loop();
lcd.clear();
delay(10);
lcd.setCursor(0, 0);
lcd.print("Errooooooou!");
delay(2500);
lcd.clear();
displayCodeEntryScreen();
}
}
void software_Reset(){
asm volatile (" jmp 0");
}