#include <IRremote.h>
#include <Servo.h>
#define PIN_RECEIVER 11 // Signal Pin of IR receiver
#define LED_PIN 13 // LED pin
IRrecv receiver(PIN_RECEIVER);
Servo myservo;
int lockPos = 1; // position for locked state
int idlePos = 50; // initial and idle position
int unlockPos = 110; // position for unlocked state
bool locked = true; // track the lock status
unsigned long unlockTime = 0; // to store the timestamp when the door was unlocked
void setup()
{
pinMode(LED_PIN, OUTPUT); // Set LED pin as OUTPUT
receiver.enableIRIn(); // Start the receiver
myservo.attach(10);
myservo.write(idlePos); // initial position
digitalWrite(LED_PIN, HIGH); // Turn on the LED initially, assuming it's locked
Serial.begin(9600); // Initialize Serial communication
}
void loop()
{
// Checks received an IR signal
if (receiver.decode()) {
translateIR();
receiver.resume();
}
// Check if more than 1 minute has passed since the door was unlocked
if (!locked && (millis() - unlockTime >= 60000)) {
lockDoor();
}
}
void translateIR()
{
Serial.print("Received IR Command: "); // Print the received command
Serial.println(receiver.decodedIRData.command, DEC);
switch (receiver.decodedIRData.command) {
case 69:
lockDoor();
break;
case 70:
unlockDoor();
break;
default:
break;
}
}
void lockDoor() {
myservo.write(lockPos);
digitalWrite(LED_PIN, HIGH);
locked = true;
delay(1000); // Wait for 3 seconds
myservo.write(idlePos); // Move back to idle position
}
void unlockDoor() {
myservo.write(unlockPos);
digitalWrite(LED_PIN, LOW);
locked = false;
unlockTime = millis(); // Set the unlock timestamp
delay(1000); // Wait for 3 seconds
myservo.write(idlePos); // Move back to idle position
}