#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#define TRIG_PIN 11
#define ECHO_PIN 10
#define PIR_SENSOR_PIN 13
#define BUZZER_PIN 9
#define SERVO_PIN 12
float new_delay;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servoMotor;
Servo swingServo; // Define servo for swinging
const int ENA = 3;
const int IN1 = 4;
const int IN2 = 5;
const int ROW_NUM = 4;
const int COLUMN_NUM = 4;
bool intruderAlertActive = false;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pin_rows[ROW_NUM] = {45, 43, 41, 39};
byte pin_column[COLUMN_NUM] = {37, 35, 33, 31};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
int motorSpeed = 0;
int mode = 0;
String angleInput = "";
int distanceUnit = 0; // 0: cm, 1: inches, 2: yards, 3: feet
String welcomeMessage = "Welcome to my project";
int scrollSpeed = 200; // Adjust the speed of scrolling
bool pirMotionDetected = false;
void setup() {
lcd.begin(16, 2);
lcd.init();
lcd.backlight();
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(PIR_SENSOR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
servoMotor.attach(SERVO_PIN);
servoMotor.write(0); // Set servo motor to 0 degrees
for(int i = 0; i < ROW_NUM; i++) {
pinMode(pin_rows[i], INPUT_PULLUP);
}
for(int j = 0; j < COLUMN_NUM; j++) {
pinMode(pin_column[j], OUTPUT);
digitalWrite(pin_column[j], HIGH);
}
// Welcome Screen
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Hello!");
delay(2000);
startupSound(); // Play startup sound
String welcomeMessage = "TronixModulev1.0";
for (int i = 0; i <= welcomeMessage.length() - 16; i++) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(welcomeMessage.substring(i, i + 16));
lcd.setCursor(5, 1); // Set cursor to the second line
lcd.print("Welcome!");
delay(2000);
}
delay(2000); // Display welcome message for a few seconds
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Select Mode:");
lcd.setCursor(3, 1);
lcd.print("A B C D");
}
void startupSound() {
tone(BUZZER_PIN, 523, 250); // C5 note for 0.25 seconds (C is the 5th note on a piano)
delay(250);
tone(BUZZER_PIN, 587, 250); // D5 note for 0.25 seconds
delay(250);
tone(BUZZER_PIN, 659, 250); // E5 note for 0.25 seconds
delay(250);
tone(BUZZER_PIN, 698, 250); // F5 note for 0.25 seconds
delay(250);
tone(BUZZER_PIN, 784, 250); // G5 note for 0.25 seconds
delay(250);
tone(BUZZER_PIN, 880, 250); // A5 note for 0.25 seconds
delay(250);
noTone(BUZZER_PIN); // Turn off the buzzer
}
void loop() {
char key = keypad.getKey();
if (key) {
switch(key) {
case 'A':
mode = 1;
lcd.clear();
lcd.print("Angle");
lcd.setCursor(5,1);
lcd.print("Measurement");
delay(1000);
playSwitchSoundA();
break;
case 'B':
mode = 2;
lcd.clear();
lcd.print("Distance");
lcd.setCursor(5,1);
lcd.print("Measurement");
delay(1000);
playSwitchSoundB();
break;
case 'C':
mode = 3;
lcd.clear();
lcd.print("PIR Sensor");
lcd.setCursor(0,1);
lcd.print("Motion Detection");
delay(1000);
playSwitchSoundC();
break;
case 'D':
mode = 4;
lcd.clear();
lcd.setCursor(3,0);
lcd.print("DC Motor");
lcd.setCursor(5,1);
lcd.print("Mode");
delay(1000);
playSwitchSoundD();
break;
case '*':
mode = 5;
lcd.clear();
lcd.setCursor(2,0);
lcd.print("IntruderSOS");
lcd.setCursor(5,1);
lcd.print("Mode");
delay(1000);
playSwitchSoundE();
break;
case '#': // Enter in Mode A
if (angleInput.length() > 0) {
int angle = angleInput.toInt();
if (angle >= 0 && angle <= 180) {
servoMotor.write(angle);
tone(BUZZER_PIN, 400, 150); // Play a 500Hz sound for 150ms
delay(150);
noTone(BUZZER_PIN);
}
angleInput = "";
}
break;
case '1':
if (mode == 2) distanceUnit = 1; // Change unit to inches in Mode B
else if (mode == 1 && angleInput.length() < 3) angleInput += key; // Append key to angle input in Mode A
tone(BUZZER_PIN, 500, 200); // Play a 1000Hz sound for 200ms
delay(150); // Add a small delay for better user experience
noTone(BUZZER_PIN); // Turn off the sound
break;
case '2':
if (mode == 2) distanceUnit = 2; // Change unit to yards in Mode B
else if (mode == 1 && angleInput.length() < 3) angleInput += key; // Append key to angle input in Mode A
tone(BUZZER_PIN, 600, 200); // Play a 1000Hz sound for 200ms
delay(150); // Add a small delay for better user experience
noTone(BUZZER_PIN); // Turn off the sound
break;
case '3':
if (mode == 2) distanceUnit = 3; // Change unit to feet in Mode B
else if (mode == 1 && angleInput.length() < 3) angleInput += key; // Append key to angle input in Mode A
tone(BUZZER_PIN, 700, 200); // Play a 1000Hz sound for 200ms
delay(150); // Add a small delay for better user experience
noTone(BUZZER_PIN); // Turn off the sound
break;
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (mode == 1 && angleInput.length() < 3) {
angleInput += key; // Append key to angle input in Mode A
}
break;
case '0':
if (mode == 2) distanceUnit = 0; // Change unit to feet in Mode B
else if (mode == 1 && angleInput.length() < 3) angleInput += key; // Append key to angle input in Mode A
tone(BUZZER_PIN, 800, 200); // Play a 1000Hz sound for 200ms
delay(150); // Add a small delay for better user experience
noTone(BUZZER_PIN); // Turn off the sound
break;
default:
break;
}
}
switch (mode) {
case 1: // Control Servo Motor
controlServo();
break;
case 2: // Measure Distance
measureDistance();
break;
case 3: // Detect Hand Motion using IR Sensor
detectHandMotion();
break;
case 4: // Electric Fan mode
electricFanMode();
break;
case 5: // Application
sosAlarm();
break;
default:
break;
}
}
void playSwitchSoundA() {
tone(BUZZER_PIN, 1000, 200); // Play a 1000Hz sound for 200ms
delay(150); // Add a small delay for better user experience
noTone(BUZZER_PIN); // Turn off the sound
}
void playSwitchSoundB() {
tone(BUZZER_PIN, 1500, 200); // Play a 1500Hz sound for 200ms
delay(150);
noTone(BUZZER_PIN);
}
void playSwitchSoundC() {
tone(BUZZER_PIN, 2000, 200); // Play a 2000Hz sound for 200ms
delay(150);
noTone(BUZZER_PIN);
}
void playSwitchSoundD() {
tone(BUZZER_PIN, 3000, 200); // Play a 2000Hz sound for 200ms
delay(150);
noTone(BUZZER_PIN);
}
void playSwitchSoundE() {
tone(BUZZER_PIN, 1000, 200); // Play a 1000Hz sound for 200ms
delay(200);
tone(BUZZER_PIN, 1500, 200); // Play a 1500Hz sound for 200ms
delay(250);
tone(BUZZER_PIN, 2000, 200); // Play a 2000Hz sound for 200ms
delay(300);
noTone(BUZZER_PIN);
}
void controlServo() {
lcd.clear();
lcd.print("Mode: Servo");
lcd.setCursor(0, 1);
lcd.print("Angle: ");
lcd.print(angleInput); // Display angle input in Mode A
lcd.print((char)223);
delay(150);
}
void measureDistance() {
long duration;
int distance;
lcd.clear();
lcd.print("Mode: Distance");
lcd.setCursor(0, 1);
lcd.print("d: ");
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration/2) * 0.0343;
switch (distanceUnit) {
case 1: // inches
distance *= 0.394; // Convert cm to inches
lcd.print(distance);
lcd.print(" inches");
break;
case 2: // yards
distance *= 0.0113; // Convert cm to yards
lcd.print(distance);
lcd.print(" yards");
break;
case 3: // feet
distance *= 0.0328; // Convert cm to feet
lcd.print(distance);
lcd.print(" feet");
break;
default: // cm
lcd.print(distance);
lcd.print(" cm");
break;
}
delay(500);
}
void detectHandMotion() {
lcd.clear();
lcd.print("Mode: PIR Sensor");
lcd.setCursor(0, 1);
lcd.print("S: ");
int irValue = digitalRead(PIR_SENSOR_PIN);
if (irValue == HIGH) {
lcd.print("Detected");
// Play notes: Do, Re, Mi, Fa, So, La, Ti in low pitch
int notes[] = {261, 293, 329, 349, 392, 440, 493, 440, 392, 349, 329, 293, 261}; // Frequencies in Hz
for(int i = 0; i < 13; i++) {
tone(BUZZER_PIN, notes[i], 500); // Play the note for 300ms
delay(250); // Add a small delay for better user experience
noTone(BUZZER_PIN); // Turn off the sound
}
} else {
lcd.print("Not Detected");
noTone(BUZZER_PIN); // Turn off the buzzer
}
delay(500);
}
void sosAlarm() {
long duration, distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration/2) / 29.1;
new_delay= (distance *3) +30;
pirMotionDetected = digitalRead(PIR_SENSOR_PIN) == HIGH;
if(distance < 100 && pirMotionDetected) {
lcd.clear();
lcd.print("Targets Nearby!!");
lcd.setCursor(0, 1);
lcd.print("S:Intruder Alert!");
delay(2000);
lcd.clear();
lcd.print("Secure and lock");
lcd.setCursor(0, 1);
lcd.print("the systems!");
delay(2000);
for(int i = 0; i < 10; i++) {
tone(BUZZER_PIN, 1000, 200); // S
delay(250);
noTone(BUZZER_PIN);
delay(50);
tone(BUZZER_PIN, 3000, 200); // O
delay(250);
noTone(BUZZER_PIN);
delay(50);
tone(BUZZER_PIN, 1000, 200); // S
delay(250);
noTone(BUZZER_PIN);
delay(50);
}
lcd.clear();
lcd.print("Sending SOS");
for (int count = 0; count < 2; count++) {
for (int angle = 0; angle <= 180; angle += 5) {
servoMotor.write(angle);
tone(BUZZER_PIN, 2000, 50); // Play a 2000Hz sound for 50ms (adjust as needed)
delay(50);
}
for (int angle = 180; angle >= 0; angle -= 5) {
servoMotor.write(angle);
tone(BUZZER_PIN, 2000, 50); // Play a 2000Hz sound for 50ms (adjust as needed)
delay(50);
}
}
delay(1000);
servoMotor.write(90); // Stop the servo at the center position
noTone(BUZZER_PIN); // Turn off the buzzer
}
else if(distance >= 200 && pirMotionDetected) {
lcd.clear();
lcd.print("Activated Sensor.");
lcd.setCursor(0, 1);
lcd.print("S: No Intruder");
servoMotor.write(0); // Reset servo position
noTone(BUZZER_PIN); // Turn off the buzzer
delay(500);
} else {
lcd.clear();
lcd.print("Activated Sensor.");
lcd.setCursor(0, 1);
lcd.print("S: No Intruder");
servoMotor.write(0); // Reset servo position
noTone(BUZZER_PIN); // Turn off the buzzer
delay(500);
}
}
void electricFanMode() {
lcd.clear();
lcd.print("Mode: DC Motor");
lcd.setCursor(0, 1);
lcd.print("RPM: ");
lcd.print(motorSpeed); // Display angle input in Mode A
lcd.print("rpm");
delay(150);
char key = keypad.getKey();
if (key == '4') {
if (motorSpeed > 0) {
motorSpeed -= 10;
setMotorSpeed(motorSpeed);
tone(BUZZER_PIN, 500, 200); // Play a 500Hz sound for 200ms
delay(150); // Add a small delay for better user experience
noTone(BUZZER_PIN); // Turn off the sound
}
} else if (key == '6') {
if (motorSpeed < 255) {
motorSpeed += 10;
setMotorSpeed(motorSpeed);
tone(BUZZER_PIN, 1000, 200); // Play a 1000Hz sound for 200ms
delay(150); // Add a small delay for better user experience
noTone(BUZZER_PIN); // Turn off the sound
}
} else if (key == '0') {
motorSpeed = 0;
setMotorSpeed(motorSpeed);
tone(BUZZER_PIN, 750, 200); // Play a 750Hz sound for 200ms
delay(150); // Add a small delay for better user experience
noTone(BUZZER_PIN); // Turn off the sound
}
delay(200);
}
void setMotorSpeed(int speed) {
analogWrite(ENA, speed);
if (speed > 0) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
} else {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
delay(500);
}