The display screen need to show a one on the next position, i mean it should be like safe code. For instance 4321 in same row, not one by one . I need some suggestion on what i suppose to do or you could send me examples.
#include <DHT11.h>
#include <Display.h>
#include <TM1637Display.h>
#include "pitches.h"
enum {ST_LOCKED, ST_LOCKED_INPUTTING, ST_UNLOCKED, ST_LOCKED2} currentState = ST_LOCKED;
// enum just allocates integers from the left so ST_LOCKED=0, ST_LOCKED_INPUTTINGW =1 etc
// just makes it easier to have human relatable names in the switch..case later
unsigned long previousMillis;
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
int Interval = 500;
bool LedState = LOW;
byte LedPin = 13;
unsigned long startTime;
unsigned long period = 500;
boolean flashing = false;
byte count = 0;
byte previousButtonState;
bool messageDisplayedOnce = false;
byte redLed = 4;
byte greenLed = 5;
byte yellowLed = 7;
byte inputButton = 9;
byte confirmButton = 8;
const int Buzzer = 3;
const int NTC_IN = A1;
bool inputButtonState; // current state of the input button
bool lastInputButtonState; // previous state of the input button
bool confirmButtonState; // current state of the confirm button
bool lastConfirmButtonState; // previous state of the confirm button
byte correctDigits[] = {4, 3, 2, 1};
byte enteredDigits[] = {1, 1, 1, 1};
byte digitIndex = 0;
bool entryIsCorrectSoFar = true; //at the first mistake, make it false and keep it false
unsigned long redAndGreenBothGoOn;
unsigned long greenBlipAt;
bool greenBlip = false;
unsigned long redBlipAt;
bool redBlip = false;
unsigned long redFlashAt;
bool redFlash = false;
void setup()
{
Display.clear();
Serial.begin(9600);
Serial.println("....4 digit vault code...");
pinMode(LED_BUILTIN, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(inputButton, INPUT_PULLUP);
pinMode(confirmButton, INPUT_PULLUP);
inputButtonState = digitalRead(inputButton);
lastInputButtonState = inputButtonState;
confirmButtonState = digitalRead(confirmButton);
lastConfirmButtonState = confirmButtonState;
Serial.print(inputButtonState);
Serial.print(lastInputButtonState);
Serial.print(confirmButtonState);
Serial.println(lastConfirmButtonState);
}
void loop()
{
Life();
States();
}
void Life()
{
if (millis() - previousMillis >= Interval)
{
LedState = !LedState;
digitalWrite(LedPin, LedState);
previousMillis = millis();;
}
}
void States()
{
switch (currentState)
{
case ST_LOCKED:
if (!messageDisplayedOnce)
{
Serial.println("\n *** The vault is locked ***");
Serial.println("Press Input to start entering the code...");
messageDisplayedOnce = true;
digitalWrite(greenLed, LOW);
if (!redFlash) {
digitalWrite(redLed, LOW);
}
}
if (!flashing)
{
byte currentButtonState = digitalRead(confirmButton);
if (currentButtonState != previousButtonState && currentButtonState == LOW)
{
flashing = true;
count = 0;
startTime = millis();
}
previousButtonState = currentButtonState;
}
else
{
unsigned long currentTime = millis();
if (currentTime - startTime >= period)
{
tone(Buzzer, 1000, 400);
digitalWrite(redLed, !digitalRead(redLed));
startTime = currentTime;
count++;
if (count == 6)
{
flashing = false;
digitalWrite(redLed, LOW);
}
}
}
if (checkInputNewPress())
{
currentState = ST_LOCKED_INPUTTING;
messageDisplayedOnce = false;
//redAndGreenBothGoOn = millis();
//digitalWrite(redLed, HIGH);
//digitalWrite(greenLed, HIGH);
}
break;
case ST_LOCKED_INPUTTING:
if (!messageDisplayedOnce)
{
Serial.print("Click Input to increment digit ");
Serial.print(digitIndex);
Display.show(digitIndex);
Serial.print(", whose current value is ");
Serial.print(enteredDigits[digitIndex]);
Display.show(enteredDigits[digitIndex]);
Serial.println(". Click the CONFIRM button when you done");
messageDisplayedOnce = true;
}
//if (millis() - redAndGreenBothGoOn >= 2000)
{
// digitalWrite(redLed, LOW);
//digitalWrite(greenLed, LOW);
}
if (checkInputNewPress())
{
messageDisplayedOnce = false;
enteredDigits[digitIndex]++;
if (enteredDigits[digitIndex] == 5) enteredDigits[digitIndex] = 4; //1
//greenBlipAt = millis();
//greenBlip = true;
//digitalWrite(greenLed, HIGH);
}
//if (millis() - greenBlipAt >= 100 && greenBlip)
{
// digitalWrite(greenLed, LOW);
//greenBlip = false;
}
if (checkConfirmNewPress())
{
entryIsCorrectSoFar = true;
for (int i = 0; i < 4; i++) {
if (enteredDigits[i] != correctDigits[i])
{
entryIsCorrectSoFar = false;
}
}
digitIndex++;
messageDisplayedOnce = false;
//redBlipAt = millis();
//redBlip = true;
//digitalWrite(redLed, HIGH);
}
{
}
//if (millis() - greenBlipAt >= 100 && greenBlip)
{
//digitalWrite(greenLed, LOW);
//greenBlip = false;
}
if (digitIndex == 4)
{
digitIndex = 0;
for (int i = 0; i < 4; i++) enteredDigits[i] = 1;
if (entryIsCorrectSoFar == true) currentState = ST_UNLOCKED;
else
{
currentState = ST_LOCKED;
redBlip = false;
redFlashAt = millis();
redFlash = true;
digitalWrite(redLed, HIGH);
Serial.println("You made at least one mistake, sorry");
entryIsCorrectSoFar = true;
}
}
break;
case ST_UNLOCKED:
if (!messageDisplayedOnce)
{
Serial.println("\n *** Vault is unlocked ***");
Serial.println("Press Confirm to lock the vault"); //not part of the brief but no point
// trying to unlock it again if its not locked
messageDisplayedOnce = true;
greenBlipAt = millis();
greenBlip = true;
digitalWrite(greenLed, HIGH);
tone(Buzzer, NOTE_B3, 400);
}
if (millis() - greenBlipAt >= 400 && greenBlip)
{
digitalWrite(greenLed, LOW);
greenBlip = false;
}
if (checkConfirmNewPress())
{
// Serial.println("Locking the vault");
currentState = ST_LOCKED;
messageDisplayedOnce = false;
}
break;
}
}
bool checkInputNewPress()
{
bool result = false;
inputButtonState = digitalRead(inputButton);
if (inputButtonState != lastInputButtonState)
{
if (inputButtonState == LOW)
{
result = true;
}
delay(50);
}
else //no change
{
result = false;
}
lastInputButtonState = inputButtonState;
return result;
}
bool checkConfirmNewPress()
{
bool result = false;
confirmButtonState = digitalRead(confirmButton);
if (confirmButtonState != lastConfirmButtonState)
{
if (confirmButtonState == LOW)
{
result = true;
}
delay(50);
}
else
{
result = false;
}
lastConfirmButtonState = confirmButtonState;
return result;
}