Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // write if functions for all
- #include <virtuabotixRTC.h> //Library used
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x3F, 16, 2);
- #include <Wire.h>
- #include <DS1302.h>
- virtuabotixRTC myRTC(4, 3, 2);
- int pset = 8; // pushbutton for setting alarm
- int phour = 9; // pushbutton for hour
- int pmin = 10; // pushbutton for minutes
- int pexit = 11; // pushbutton for exit of set alarm
- int buzzer = 6;
- const byte LedPin = 7;
- int h;
- int m;
- int buttonforset = 0; // pushbutton state for setting alarm
- int buttonforhour = 0; // pushbutton state for hour
- int buttonformin = 0;// pushbutton state for minutes
- int buttonforexit = 0; // pushbutton state for exit of set alarm
- int activate = 0;
- Time t;
- void setup()
- {
- pinMode(pset, INPUT);
- pinMode(phour, INPUT);
- pinMode(pmin, INPUT);
- pinMode(pexit, INPUT);
- pinMode(LedPin, OUTPUT);
- Serial.begin(9600);
- lcd.begin();
- // Set the current date, and time in the following format:
- // seconds, minutes, hours, day of the week, day of the month, month, year
- myRTC.setDS1302Time(0, 11, 11, 5, 18, 5, 2019); //Here you write your actual time/date as shown above
- //but remember to "comment/remove" this function once you're done
- //The setup is done only one time and the module will continue counting it automatically
- }
- void loop() {
- // This allows for the update of variables for time or accessing the individual elements.
- myRTC.updateTime();
- // Start printing elements as individuals
- lcd.setCursor(0, 0);
- lcd.print("Time: ");
- lcd.setCursor(5, 1);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print("Date: ");
- lcd.setCursor (9, 1);
- lcd.print(myRTC.dayofmonth);
- lcd.setCursor (8, 1);
- lcd.print("/");
- lcd.setCursor (6, 1);
- if (myRTC.month > 9)
- {
- lcd.print(myRTC.month);
- }
- else
- {
- lcd.setCursor (7, 1);
- lcd.print(myRTC.month);
- }
- lcd.setCursor (11, 1);
- lcd.print("/");
- lcd.setCursor (12, 1);
- lcd.print(myRTC.year);
- lcd.setCursor(5, 0);
- lcd.print(" ");
- lcd.setCursor (6, 0);
- if (myRTC.hours > 9)
- {
- lcd.print(myRTC.hours);
- }
- else
- {
- lcd.setCursor (7, 0);
- lcd.print(myRTC.hours);
- }
- lcd.setCursor(8, 0);
- lcd.print(":");
- lcd.setCursor (9, 0);
- if (myRTC.minutes > 9)
- {
- lcd.print(myRTC.minutes);
- }
- else
- {
- lcd.setCursor (10, 0);
- lcd.print(myRTC.minutes);
- lcd.setCursor (9, 0);
- lcd.print("0");
- }
- lcd.setCursor(11, 0);
- lcd.print(":");
- lcd.setCursor(12, 0);
- if (myRTC.seconds > 9)
- {
- lcd.println(myRTC.seconds);
- }
- else
- {
- lcd.setCursor(13, 0);
- lcd.println(myRTC.seconds);
- lcd.setCursor(12, 0);
- lcd.print("0");
- }
- lcd.setCursor(14, 0);
- lcd.print(" ");
- lcd.setCursor(15, 0);
- lcd.print(" ");
- // Delay so the program doesn't print non-stop
- delay(1000);
- buttonforset = digitalRead(pset);
- // setting button pressed
- if (buttonforset == HIGH)
- {
- activate = 1;
- lcd.clear();
- }
- while (activate == 1) {
- lcd.setCursor(0, 0);
- lcd.print("Set Alarm");
- lcd.setCursor(0, 1);
- lcd.print("Hour= ");
- lcd.setCursor(9, 1);
- lcd.print("Min= ");
- buttonforhour = digitalRead(phour); // set hour for alarm
- if (buttonforhour == HIGH)
- {
- h++;
- lcd.setCursor(5, 1);
- lcd.print(h);
- if (h > 23)
- {
- h = 0;
- lcd.clear();
- }
- delay(100);
- }
- buttonformin = digitalRead(pmin); // set minutes for alarm
- if (buttonformin == HIGH)
- {
- m++;
- lcd.setCursor(13, 1);
- lcd.print(m);
- if (m > 59)
- {
- m = 0;
- lcd.clear();
- }
- delay(100);
- }
- lcd.setCursor(5, 1);
- lcd.print(h);
- lcd.setCursor(13, 1);
- lcd.print(m);
- buttonforexit = digitalRead(pexit); // exit from set alarm mode
- if (buttonforexit == HIGH) {
- activate = 0;
- lcd.clear();
- }
- if (myRTC.hours == h && myRTC.minutes == m)
- {
- tone(6, 400, 300);
- }
- delay (500);
- if (myRTC.hours == h && myRTC.minutes == m)
- {
- digitalWrite(LedPin, HIGH);
- delay(300);
- digitalWrite(LedPin, LOW);
- delay(300);
- }
- }
- }
RAW Paste Data