/*
Many people struggle to wake up at their desired time, which often leads to a challenging and unproductive day.
This difficulty can affect their mood, energy levels, and overall performance. To address this issue, this alarm system
stimulator offers a practical solution. Unlike traditional alarms that can be snoozed, this system ensures that users
wake up fully by requiring physical movement to turn it off. By incorporating features like motion detection, it encourages
users to get out of bed, making the process of waking up easier and more effective. This approach aims to promote a
more energetic start to the day, enhancing daily productivity.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <Keypad.h>
#define PIR_PIN 11
#define BUZZER_PIN 10
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS1307 rtc;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int alarmHour = 7;
int alarmMinute = 0;
bool alarmSet = false;
unsigned long motionStartTime = 0;
unsigned long alarmStartTime = 0;
void setup()
{
pinMode(PIR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
lcd.init();
lcd.backlight();
if (!rtc.begin())
{
lcd.setCursor(0, 0);
lcd.print("RTC not found");
while (1);
}
if (!rtc.isrunning())
{
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Alarm Time");
}
void loop()
{
char key = keypad.getKey();
if (key)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Key Pressed: ");
lcd.print(key);
if (key >= '0' && key <= '9')
{
static int inputCount = 0;
static String inputTime = "";
inputTime += key;
lcd.setCursor(0, 1);
lcd.print("Input: ");
lcd.print(inputTime);
if (inputTime.length() == 4)
{
int enteredHour = (inputTime[0] - '0') * 10 + (inputTime[1] - '0');
int enteredMinute = (inputTime[2] - '0') * 10 + (inputTime[3] - '0');
if (enteredHour >= 0 && enteredHour < 24 && enteredMinute >= 0 && enteredMinute < 60)
{
alarmHour = enteredHour;
alarmMinute = enteredMinute;
alarmSet = true;
lcd.setCursor(0, 2);
lcd.print("Alarm Set for:");
lcd.setCursor(0, 3);
lcd.print(alarmHour);
lcd.print(":");
lcd.print(alarmMinute);
delay(2000);
}
else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Invalid Time!");
delay(2000);
}
inputTime = "";
}
}
}
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print("Time: ");
printTime(now.hour(), now.minute(), now.second());
if (alarmSet && now.hour() == alarmHour && now.minute() == alarmMinute)
{
activateAlarm();
}
}
void printTime(int hour, int minute, int second)
{
if (hour < 10) lcd.print("0");
lcd.print(hour);
lcd.print(":");
if (minute < 10) lcd.print("0");
lcd.print(minute);
lcd.print(":");
if (second < 10) lcd.print("0");
lcd.print(second);
}
void activateAlarm()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ALARM! MOVE TO STOP!");
alarmStartTime = millis();
while (millis() - alarmStartTime < 60000)
{
tone(BUZZER_PIN, 1000);
int motionDetected = digitalRead(PIR_PIN);
static unsigned long lastMotionTime = 0;
if (motionDetected == HIGH && millis() - lastMotionTime > 500)
{
lastMotionTime = millis();
if (motionStartTime == 0)
{
motionStartTime = millis();
}
lcd.setCursor(0, 1);
lcd.print("Motion Detected!");
if (millis() - motionStartTime >= 5000)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Good Morning!");
lcd.setCursor(0, 1);
lcd.print("Have a nice day!");
noTone(BUZZER_PIN);
delay(1000);
return;
}
} else
{
motionStartTime = 0;
lcd.setCursor(0, 1);
lcd.print("No Motion! ");
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No Motion Detected");
delay(1000);
}