#include <Servo.h>
Servo myServo; // Create Servo object
// Pin definitions
const int redLedPin = 11;
const int greenLedPin = 12;
const int buttonPin = 13;
const int servoPin = 10;
// Pins for Seven Segment Display
const int segmentPins[7] = {2, 3, 4, 5, 6, 7, 8};
// Number patterns for Seven Segment Display (common anode)
const int numberPatterns[10][7] = {
{0, 0, 0, 0, 0, 0, 1}, // 0
{1, 0, 0, 1, 1, 1, 1}, // 1
{0, 0, 1, 0, 0, 1, 0}, // 2
{0, 0, 0, 0, 1, 1, 0}, // 3
{1, 0, 0, 1, 1, 0, 0}, // 4
{0, 1, 0, 0, 1, 0, 0}, // 5
{0, 1, 0, 0, 0, 0, 0}, // 6
{0, 0, 0, 1, 1, 1, 1}, // 7
{0, 0, 0, 0, 0, 0, 0}, // 8
{0, 0, 0, 1, 1, 0, 0} // 9
};
// Variables for button and servo state
int buttonState = 0;
int lastButtonState = 0;
bool servoUnlocked = false; // Track if the door can be unlocked
unsigned long countdownStartTime; // Start time for countdown
bool countdownActive = false; // Countdown status
// Correct password and input password
const String correctPassword = "1234";
String inputPassword = ""; // Variable to store the input password
void setup() {
myServo.attach(servoPin); // Attach servo to pin
pinMode(buttonPin, INPUT); // Set button pin as input
pinMode(redLedPin, OUTPUT); // Set red LED as output
pinMode(greenLedPin, OUTPUT); // Set green LED as output
// Set up Seven Segment Display pins
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
digitalWrite(segmentPins[i], HIGH); // Turn off all segments initially (anode)
}
Serial.begin(9600); // Start serial communication
Serial.println("Sistem dimulai. Masukkan password:"); // Feedback awal
myServo.write(0); // Set initial servo position to 0 degrees (locked)
clearDisplay(); // Turn off all segments at setup
digitalWrite(redLedPin, HIGH); // Initial red LED on (door locked)
digitalWrite(greenLedPin, LOW); // Green LED is off initially
}
void loop() {
// Read button status
buttonState = digitalRead(buttonPin);
// Check if button is pressed when servo can be unlocked
if (buttonState == HIGH && lastButtonState == LOW && servoUnlocked) {
myServo.write(90); // Move servo to unlock position (90 degrees)
Serial.println("Pintu terbuka, menunggu 5 detik...");
digitalWrite(greenLedPin, HIGH); // Turn on green LED to indicate unlocked
delay(5000); // Wait for 5 seconds
myServo.write(0); // Move servo back to locked position (0 degrees)
Serial.println("Pintu terkunci kembali.");
digitalWrite(greenLedPin, LOW); // Turn off green LED
servoUnlocked = false; // Reset the unlocked status after action
}
// Store previous button state
lastButtonState = buttonState;
// Check for password input
if (Serial.available() > 0) {
char incomingChar = Serial.read(); // Read character from Serial
// Print debugging info to see received character
Serial.print("Karakter yang diterima: ");
Serial.println(incomingChar);
// Check if Enter (newline) is pressed, marking end of input
if (incomingChar == '\n' || incomingChar == '\r') { // Handle both '\n' and '\r'
Serial.print("Password yang dimasukkan lengkap: ");
Serial.println(inputPassword);
if (inputPassword == correctPassword) {
digitalWrite(greenLedPin, HIGH); // Turn on green LED briefly
Serial.println("Password benar! Tekan tombol untuk membuka kunci.");
servoUnlocked = true; // Allow button to unlock the servo
inputPassword = ""; // Reset input password
delay(1000); // Keep green LED on for 1 second as feedback
digitalWrite(greenLedPin, LOW); // Turn off green LED
} else {
Serial.println("Password salah!");
digitalWrite(redLedPin, HIGH); // Turn on red LED
delay(2000); // Wait 2 seconds
digitalWrite(redLedPin, LOW); // Turn off red LED
inputPassword = ""; // Reset input password
}
} else if (incomingChar >= '0' && incomingChar <= '9') { // Check only for numeric characters
inputPassword += incomingChar; // Append character to password
Serial.print("Password yang dimasukkan: ");
Serial.println(inputPassword); // Display entered password
}
}
// Clear display when no countdown or action is active
if (!servoUnlocked) {
clearDisplay();
}
}
// Function to display number on Seven Segment
void displayNumber(int num) {
if (num >= 0 && num <= 9) { // Display only digits 0-9
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], numberPatterns[num][i]);
}
}
}
// Function to turn off all segments on Seven Segment
void clearDisplay() {
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], HIGH); // Turn off all segments
}
}