#define DV_PIN 7
#define Senser 6
unsigned int myinput = 0;
const int passwordLength = 3;
int correctPassword[passwordLength] = { 1, 2, 4 };
int enteredPassword[passwordLength];
int inputIndex = 0;
unsigned int Sensor;
int tigger = 0;
void setup() {
DDRB |= 0b00111000;
DDRC &= 0b11000000;
DDRD = 0;
pinMode(DV_PIN, INPUT);
pinMode(Sensor, INPUT);
}
void IR_Reflect_Sensor() {
PORTB &= ~0b00100000;
if (digitalRead(Senser)) {
if (Sensor == 0) {
PORTB ^= 0x20;
}
}
delay(100);
}
int index = 0;
void CHECKPASSWORD() {
int customKey = myinput;
if (customKey) {
enteredPassword[index] = customKey;
index++;
if (index == passwordLength) {
delay(1000);
if (checkEnteredPassword()) {
PORTB ^= 0b001000;
} else {
PORTB ^= 0b010000;
delay(100);
}
delay(2000);
index = 0;
memset(enteredPassword, 0, sizeof(enteredPassword));
}
}
}
bool checkEnteredPassword() {
for (int i = 0; i < passwordLength; i++) {
if (enteredPassword[i] != correctPassword[i]) {
return false;
}
}
return true;
}
void KEYPAD() {
if (digitalRead(PORTC & 0b01111111)) {
myinput = PINC & 0b00111111;
if (myinput == 1 || myinput == 2 || myinput == 4 || myinput == 8 || myinput == 16 || myinput == 32) {
delay(100);
}
}
}
void loop() {
IR_Reflect_Sensor();
PORTB |= 0b00011000;
if (digitalRead(Senser) == LOW) {
if (digitalRead(DV_PIN) == LOW) {
KEYPAD();
}
CHECKPASSWORD();
}
delay(100);
}