// Define the pins for the sensors and devices
#define RED_LED 3 // Red LED pin
#define YELLOW_LED 4 // Yellow LED pin
#define GREEN_LED 5 // Green LED pin
#define BUZZER 6 // Buzzer pin
#define TRIG_PIN 7 // Ultrasonic sensor trigger pin
#define ECHO_PIN 8 // Ultrasonic sensor echo pin
#define RS_PIN 9 // LCD RS pin
#define EN_PIN 10 // LCD EN pin
#define D4_PIN 11 // LCD D4 pin
#define D5_PIN 12 // LCD D5 pin
#define D6_PIN 13 // LCD D6 pin
#define D7_PIN 14 // LCD D7 pin
#define POT_PIN A2 // Potentiometer pin
#define TEMP_PIN A3 // Temperature sensor pin
#define KEYPAD_ROWS 4 // Number of keypad rows
#define KEYPAD_COLS 4 // Number of keypad columns
// Include the libraries for LCD and keypad
#include <LiquidCrystal.h>
#include <Keypad.h>
// Create an LCD object with the pin numbers
LiquidCrystal lcd(RS_PIN, EN_PIN, D4_PIN, D5_PIN, D6_PIN, D7_PIN);
// Define the keypad keys
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Define the keypad row and column pins
byte rowPins[KEYPAD_ROWS] = {15, 16, 17, 18}; // Connect to the row pinouts of the keypad
byte colPins[KEYPAD_COLS] = {19, 20, 21, 22}; // Connect to the column pinouts of the keypad
// Create a keypad object with the keys and pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
// Define some constants and variables
#define DISTANCE_THRESHOLD 50 // Threshold value for distance detection
#define PASSWORD_LENGTH 4 // Length of the password
#define PASSWORD "1234" // The password to access the system
char input[PASSWORD_LENGTH + 1]; // Store the user input
int index = 0; // Store the input index
bool locked = true; // Store the lock state
bool alarm = false; // Store the alarm state
float temperature; // Store the temperature value
void setup() {
// Initialize the serial monitor
Serial.begin(9600);
// Set the sensor and device pins as input or output
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(POT_PIN, INPUT);
pinMode(TEMP_PIN, INPUT);
// Initialize the LCD with the number of columns and rows
lcd.begin(16, 2);
// Print a welcome message on the LCD
lcd.print("Smart Home System");
lcd.setCursor(0, 1);
lcd.print("Enter Password:");
}
void loop() {
// Check the sensors and devices
checkUltrasonic();
checkPotentiometer();
checkTemperature();
checkKeypad();
// Update the LCD display
updateLCD();
}
// Check the ultrasonic sensor and trigger the alarm if the distance is below the threshold
void checkUltrasonic() {
long duration; // Store the duration of the pulse
int distance; // Store the distance from the sensor
digitalWrite(TRIG_PIN, LOW); // Send a low pulse to the trigger pin
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(TRIG_PIN, HIGH); // Send a high pulse to the trigger pin
delayMicroseconds(10); // Wait for 10 microseconds
digitalWrite(TRIG_PIN, LOW); // Send a low pulse to the trigger pin
duration = pulseIn(ECHO_PIN, HIGH); // Read the duration of the echo pin
distance = duration * 0.034 / 2; // Calculate the distance in cm
Serial.print("Distance: ");
Serial.println(distance);
if (distance < DISTANCE_THRESHOLD) { // If the distance is below the threshold
alarm = true; // Set the alarm state to true
Serial.println("Object detected!");
}
}
// Check the potentiometer and adjust the brightness of the LEDs
void checkPotentiometer() {
int potValue = analogRead(POT_PIN); // Read the potentiometer value
Serial.print("Potentiometer: ");
Serial.println(potValue);
int brightness = map(potValue, 0, 1023, 0, 255); // Map the potentiometer value to the LED brightness
analogWrite(RED_LED, brightness); // Write the brightness to the red LED
analogWrite(YELLOW_LED, brightness); // Write the brightness to the yellow LED
analogWrite(GREEN_LED, brightness); // Write the brightness to the green LED
}
// Check the temperature sensor and display the temperature on the LCD
void checkTemperature() {
int tempValue = analogRead(TEMP_PIN); // Read the temperature sensor value
Serial.print("Temperature: ");
Serial.println(tempValue);
temperature = tempValue * 0.48828125; // Convert the temperature sensor value to Celsius
}
// Check the keypad and get the user input
void checkKeypad() {
char key = keypad.getKey(); // Get the pressed key
if (key) { // If a key is pressed
Serial.print("Key: ");
Serial.println(key);
if (key == '#') { // If the key is '#'
input[index] = '\0'; // Terminate the input string
Serial.print("Input: ");
Serial.println(input);
if (strcmp(input, PASSWORD) == 0) { // If the input matches the password
locked = false; // Set the lock state to false
Serial.println("Password correct!");
} else { // If the input does not match the password
locked = true; // Set the lock state to true
Serial.println("Password incorrect!");
}
index = 0; // Reset the input index
} else if (key == '*') { // If the key is '*'
index = 0; // Reset the input index
input[index] = '\0'; // Terminate the input string
Serial.println("Input cleared!");
} else if (index < PASSWORD_LENGTH) { // If the input is not full
input[index] = key; // Store the key in the input array
index++; // Increment the input index
}
}
}
// Update the LCD display according to the lock and alarm states
void updateLCD() {
lcd.clear(); // Clear the LCD screen
if (locked) { // If the system is locked
lcd.print("System Locked"); // Print a message on the LCD
lcd.setCursor(0, 1); // Move the cursor to the second row
lcd.print("Enter Password:"); // Print a message on the LCD
lcd.print(input); // Print the user input on the LCD
} else { // If the system is unlocked
lcd.print("System Unlocked"); // Print a message on the LCD
lcd.setCursor(0, 1); // Move the cursor to the second row
lcd.print("Temp: "); // Print a message on the LCD
lcd.print(temperature); // Print the temperature on the LCD
lcd.print(" C"); // Print the unit on the LCD
}
if (alarm) { // If the alarm is on
digitalWrite(BUZZER, HIGH); // Turn on the buzzer
lcd.setCursor(12, 0); // Move the cursor to the first row
lcd.print("ALARM!"); // Print a message on the LCD
} else { // If the alarm is off
digitalWrite(BUZZER, LOW); // Turn off the buzzer
}
}