Hello everyone, I am working on a project where I have to create a safe, and simulate the door of these safe with the light sensor (when the sensor recognize the light the door is open, otherwise close). Now, I am missing the last part: when the code is correct and you open the door, and then close the door automatically, the safe turn back in block and you have to retype the password. I am trying different ways but I dunno why does not work:
void SensorDoor() // this is the sensor that recognize the door open or close
the thought is simple: when the condition are true back to the mode 0
THE FUNCTION:
void SensorDoor() // this is the sensor that recognize the door open or cloose
{
int doorSensorValue = analogRead(DOOR_SENSOR);
doorSensorValue = map (doorSensorValue, 0, 1023, 0, 200);
if (checkPassword != PASSWORD && doorSensorValue > 100)
{
while (alarmRunning < 7)
{
digitalWrite(FAILED_LED, HIGH);
delay(200);
digitalWrite(FAILED_LED, LOW);
delay(200);
tone(ALARM_SOUND, 440, 1000);
alarmRunning ++;
Display.show("----");
}
}
else if (checkPassword == PASSWORD && doorSensorValue < 100)
{
mode = 0;
}
}
THE ENTIRE CODE
#include <Display.h>
const int LEFT_BUTTON_PIN = 9;
const int RIGHT_BUTTON_PIN = 8;
const int OPEN_LED = 5; // if the password match with the default password a led turn on, as a proof of the correct password
const int FAILED_LED = 4 ; // if the password is wrong, the red led switch on
const int ALARM_SOUND = 3; // if the door is opened with the security code still active, the alarm sounds
const int DOOR_SENSOR = 16;
const int press_Button = LOW;
const int not_PressButton = HIGH;
const String digitSpace1 = "000"; // these number are the spaces for the display and allow to type from left to right
const String digitSpace2 = "00";
const String digitSpace3 = "0";
const String PASSWORD = "1234"; // this is the default password
String checkPassword = ""; // check password, show if the 4 input match the dafult password
int alarmRunning = 0;
int mode = 0; // these are the mode for entering the four digit
String inputValue_1 = ""; // this is the number that will be stored typed from the visitor (1-5)
String inputValue_2 = "";
String inputValue_3 = "";
String inputValue_4 = "";
int numberValue = 0;
int previousLeft = LOW;
int previousRight = LOW;
void setup() {
pinMode (LEFT_BUTTON_PIN, INPUT_PULLUP);
pinMode (RIGHT_BUTTON_PIN, INPUT_PULLUP);
pinMode(OPEN_LED, OUTPUT);
pinMode(FAILED_LED, OUTPUT);
pinMode (ALARM_SOUND, OUTPUT);
pinMode (DOOR_SENSOR, INPUT);
Serial.begin(9600);
Display.show("0000");
}
void GetInput();
void ConfirmInput();
void SensorDoor();
void loop() {
GetInput();
ConfirmInput();
SensorDoor();
}
void GetInput() // this is the function to get all the for digit
{
int leftButton = digitalRead(LEFT_BUTTON_PIN);
if(leftButton != previousLeft)
{
if (leftButton == press_Button)
{
numberValue = numberValue % 5 + 1 ; // this is the loop for the five digits
delay(200);
if (mode == 0 )
{
inputValue_1 = numberValue;
Display.show(inputValue_1 + digitSpace1);
}
else if (mode == 1 )
{
inputValue_2 = numberValue;
Display.show(inputValue_1 + inputValue_2 + digitSpace2);
}
else if (mode == 2 )
{
inputValue_3 = numberValue;
Display.show(inputValue_1 + inputValue_2 + inputValue_3 + digitSpace3);
}
else if (mode == 3 )
{
inputValue_4 = numberValue;
Display.show(inputValue_1 + inputValue_2 + inputValue_3 + inputValue_4 );
}
}
previousLeft = leftButton;
}
}
void ConfirmInput () // this is the mode that store and conferm the digit
{
int rightButton = digitalRead (RIGHT_BUTTON_PIN);
if(rightButton != previousRight)
{
if (rightButton == press_Button)
{
mode = (mode+1) % 5;
numberValue = 0;
}
else if (mode == 4)
{
checkPassword = inputValue_1 + inputValue_2 + inputValue_3 + inputValue_4;
if (checkPassword == PASSWORD)
{
digitalWrite(OPEN_LED, HIGH);
Display.off();
}
else if(checkPassword != PASSWORD)
{
digitalWrite(FAILED_LED, HIGH);
delay(1000);
Display.clear();
Display.show("0000");
delay(1000);
digitalWrite(FAILED_LED, LOW);
mode = 0;
}
}
previousRight = rightButton;
}
}
void SensorDoor() // this is the sensor that recognize the door open or cloose
{
int doorSensorValue = analogRead(DOOR_SENSOR);
doorSensorValue = map (doorSensorValue, 0, 1023, 0, 200);
if (checkPassword != PASSWORD && doorSensorValue > 100)
{
while (alarmRunning < 7)
{
digitalWrite(FAILED_LED, HIGH);
delay(200);
digitalWrite(FAILED_LED, LOW);
delay(200);
tone(ALARM_SOUND, 440, 1000);
alarmRunning ++;
Display.show("----");
}
}
else if (checkPassword == PASSWORD && doorSensorValue < 100)
{
mode = 0;
}
}