Devices & Components
SmartElex Bharat AI Innovators Kit Powered by Arduino
Software & Tools
Arduino Cloud Editor
Arduino Cloud Agent
Project description
Code
CloudKey: The IoT Smart Lock
cpp
1C#include "thingProperties.h" 2#include <Servo.h> 3 4// Rotary Encoder pins 5#define CLK 4 6#define DT 5 7#define SW 6 8 9// Ultrasonic pins 10#define TRIG 2 11#define ECHO 3 12 13Servo lockServo; 14 15int counter = 0; 16int lastStateCLK; 17String enteredCode = ""; 18String correctCode = "1"; // Set your password here 19bool personDetected = false; 20bool lastPersonDetected = false; // to track state change 21 22void setup() { 23 Serial.begin(9600); 24 delay(1500); 25 26 // Initialize Arduino Cloud properties 27 initProperties(); 28 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 29 30 pinMode(CLK, INPUT); 31 pinMode(DT, INPUT); 32 pinMode(SW, INPUT_PULLUP); 33 34 pinMode(TRIG, OUTPUT); 35 pinMode(ECHO, INPUT); 36 37 lockServo.attach(9); // Servo pin 38 39 lastStateCLK = digitalRead(CLK); 40 41 lock_control = false; // default locked 42 lockServo.write(0); // lock position 43} 44 45void loop() { 46 ArduinoCloud.update(); 47 48 // Rotary Encoder handling 49 int currentStateCLK = digitalRead(CLK); 50 if (currentStateCLK != lastStateCLK && currentStateCLK == 1) { 51 if (digitalRead(DT) != currentStateCLK) { 52 counter++; 53 } else { 54 counter--; 55 } 56 Serial.print("Code: "); 57 Serial.println(counter); 58 } 59 lastStateCLK = currentStateCLK; 60 61 // Button press to submit code 62 if (digitalRead(SW) == LOW) { 63 enteredCode += String(counter); 64 Serial.print("Entered Code: "); 65 Serial.println(enteredCode); 66 delay(300); 67 counter = 0; // reset for next digit 68 69 if (enteredCode.length() >= correctCode.length()) { 70 if (enteredCode == correctCode) { 71 Serial.println("Correct Code! Unlocking..."); 72 lock_control = true; 73 lockServo.write(90); 74 } else { 75 Serial.println("Wrong Code! Locking..."); 76 lock_control = false; 77 lockServo.write(0); 78 } 79 enteredCode = ""; // reset 80 } 81 } 82 83 // Ultrasonic person detection 84 long duration, distance; 85 digitalWrite(TRIG, LOW); 86 delayMicroseconds(2); 87 digitalWrite(TRIG, HIGH); 88 delayMicroseconds(10); 89 digitalWrite(TRIG, LOW); 90 duration = pulseIn(ECHO, HIGH); 91 distance = duration * 0.034 / 2; 92 93 personDetected = (distance > 0 && distance < 100); 94 95 // Only print when detection state changes to true 96 if (personDetected && !lastPersonDetected) { 97 Serial.println("Person Detected!"); 98 } 99 100 lastPersonDetected = personDetected; 101 102 // Cloud-controlled lock override 103 if (lock_control) { 104 lockServo.write(90); 105 } else { 106 lockServo.write(0); 107 } 108} 109 110// Cloud variable change function 111void onLockControlChange() { 112 if (lock_control) { 113 Serial.println("Cloud: Unlock"); 114 lockServo.write(90); 115 } else { 116 Serial.println("Cloud: Lock"); 117 lockServo.write(0); 118 } 119}
Downloadable files
CloudKey: The IoT Smart Lock
Circuit Smart Lock 1.jpg

Comments
Only logged in users can leave comments