#include <Adafruit_SSD1306.h>
#include <Wire.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
int passcode = 123; // Set the passcode
int detected = 0; // Flag for microphone detection
int input = 0; // Variable for storing microphone input
int auth = 0; // Flag for authentication
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(20, 10);
display.println("Speak now");
display.display();
}
void loop() {
if (digitalRead(A0) == HIGH) { // If microphone detects sound
detected = 1; // Set the flag
delay(500);
}
if (detected) { // If sound is detected
input = analogRead(A0); // Read the microphone input
if (input > 200) { // Check if input is above threshold
auth = 1; // Set authentication flag
detected = 0; // Reset microphone flag
}
}
if (auth) { // If authentication is successful
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(30, 10);
display.println("ACCESS");
display.setCursor(0, 40);
display.println("GRANTED");
display.display();
delay(5000);
auth = 0; // Reset authentication flag
input = 0; // Reset input variable
}
if (!auth && detected == 0) { // If authentication fails
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.println("ACCESS DENIED");
display.setCursor(30, 40);
display.println(":(");
display.display();
delay(5000);
input = 0; // Reset input variable
}
}