Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Code was designed by Joseph melton and David Carver.
- // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
- #include <Wire.h>
- #include "RTClib.h"
- #include "Button.h"
- #define DS1307_ADDRESS 0x68
- byte zero = 0x00; //workaround for issue #527
- int latchPin = 8;
- int clockPin = 12;
- int dataPin = 11;
- int grd[] = {9,13,7,10};
- int PM_Light = 2;
- int aH;
- int aM;
- boolean AM_PM = false;
- int lastHour;
- int alarmSwitch = A1;
- int alarmOnIndicator = 6;
- boolean alarmAM_PM = false;
- int lastAlarmHour;
- int buzzerPin = 5;
- int buzzerCounter = 0;
- int snoozeCounter = 0;
- boolean snoozer = false;
- int lastTime;
- bool soundAlarm;
- int snoozeTime;
- int h;
- int m;
- //int minutebutton = 3;
- //int hourbutton = 4;
- Button mButton(3);
- Button hButton(4);
- Button timeButton(17);
- Button alarmButton(16);
- Button snoozeButton(14);
- RTC_DS1307 rtc;
- void resetTime(){
- Wire.beginTransmission(DS1307_ADDRESS);
- Wire.write(zero);
- Wire.endTransmission();
- Wire.requestFrom(DS1307_ADDRESS, 3);
- int second = 0;
- int minute = 12;
- int hour = 12;
- Wire.beginTransmission(DS1307_ADDRESS);
- Wire.write(zero); //stop Oscillator
- Wire.write(decToBcd(second));
- Wire.write(decToBcd(minute));
- Wire.write(decToBcd(hour));
- Wire.write(zero); //start
- Wire.endTransmission();
- }
- void setup () {
- Serial.begin(9600);
- Wire.begin();
- //resetTime();
- pinMode(dataPin, OUTPUT);
- pinMode(latchPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(grd[0], OUTPUT);
- pinMode(grd[1], OUTPUT);
- pinMode(grd[2], OUTPUT);
- pinMode(grd[3], OUTPUT);
- pinMode(PM_Light, OUTPUT);
- pinMode(3, INPUT);
- pinMode(4, INPUT);
- pinMode(alarmOnIndicator, OUTPUT);
- pinMode(alarmSwitch, INPUT);
- pinMode(buzzerPin, OUTPUT);
- aH = 12;
- aM = 0;
- timeButton.setHeldReturnDelay(0);
- timeButton.setHeldDelay(0);
- alarmButton.setHeldReturnDelay(0);
- alarmButton.setHeldDelay(0);
- }
- byte decToBcd(byte val){
- // Convert normal decimal numbers to binary coded decimal
- return ( (val/10*16) + (val%10) );
- }
- byte bcdToDec(byte val) {
- // Convert binary coded decimal to normal decimal numbers
- return ( (val/16*10) + (val%16) );
- }
- int displays = 4;
- //display fuction
- void segment(int screen, int displayNumber){
- //turn off all screens
- for(int i=0;i < 4;i++){
- digitalWrite(grd[i], LOW);
- }
- //turn on the one you want
- digitalWrite(screen, HIGH);
- float delayTime = 2;
- int dataNumber;
- digitalWrite(latchPin, LOW);
- if(displayNumber == 0){
- dataNumber = 253;
- shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
- delay(delayTime);
- }else if (displayNumber == 1){
- dataNumber = 97;
- shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
- delay(delayTime);
- }else if (displayNumber == 2){
- dataNumber = 219;
- shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
- delay(delayTime);
- }else if (displayNumber == 3){
- dataNumber = 243;
- shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
- delay(delayTime);
- }else if (displayNumber == 4){
- dataNumber = 103;
- shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
- delay(delayTime);
- }else if (displayNumber == 5){
- dataNumber = 183;
- shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
- delay(delayTime);
- }else if (displayNumber == 6){
- dataNumber = 191;
- shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
- delay(delayTime);
- }else if (displayNumber == 7){
- dataNumber = 225;
- shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
- delay(delayTime);
- }else if (displayNumber == 8){
- dataNumber = 255;
- shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
- delay(delayTime);
- }else if (displayNumber == 9){
- dataNumber = 247;
- shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
- delay(delayTime);
- }else if (displayNumber == 10){
- dataNumber = 1;
- shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
- delay(delayTime);
- }
- digitalWrite(latchPin, HIGH);
- delay(1);
- digitalWrite(latchPin, LOW);
- shiftOut(dataPin, clockPin, LSBFIRST, 1);
- digitalWrite(latchPin, HIGH);
- }
- void checkAlarmIndicator(){
- int alarmSwitchCheck = digitalRead(alarmSwitch);
- if(alarmSwitchCheck == HIGH){
- digitalWrite(alarmOnIndicator, HIGH);
- }else{
- digitalWrite(alarmOnIndicator, LOW);
- }
- }
- void changeAlarm(int alarmTime){
- if(alarmTime == 3){
- aM++;
- if(aM > 59){
- aM = 0;
- }
- }else if(alarmTime == 4){
- aH++;
- if(aH > 12){
- aH = 1;
- }else if(lastAlarmHour == 11 && aH == 12){
- alarmAM_PM = !alarmAM_PM;
- }
- }
- lastAlarmHour = aH;
- }
- bool checkAlarm(){
- int check = digitalRead(alarmSwitch);
- if(check == LOW){
- soundAlarm = false;
- snoozeTime = 0;
- }
- if(soundAlarm == true){
- return true;
- }
- if(check == HIGH){
- if(aH == h && aM == m && alarmAM_PM == AM_PM){
- if(lastTime != m && snoozeTime == 0){
- soundAlarm = true;
- }
- }
- }
- return false;
- }
- void changeTime(int time){
- Wire.beginTransmission(DS1307_ADDRESS);
- Wire.write(zero);
- Wire.endTransmission();
- Wire.requestFrom(DS1307_ADDRESS, 3);
- int second = bcdToDec(Wire.read());
- int minute = bcdToDec(Wire.read());
- int hour = bcdToDec(Wire.read());
- second = 0;
- if(time == 1){
- minute++;
- if(minute > 59){
- minute = 0;
- }
- }else if(time == 2){
- hour++;
- if(hour > 24){
- hour = 1;
- }
- }
- Wire.beginTransmission(DS1307_ADDRESS);
- Wire.write(zero); //stop Oscillator
- Wire.write(decToBcd(second));
- Wire.write(decToBcd(minute));
- Wire.write(decToBcd(hour));
- Wire.write(zero); //start
- Wire.endTransmission();
- }
- void convertHour(){
- Wire.beginTransmission(DS1307_ADDRESS);
- Wire.write(zero);
- Wire.endTransmission();
- Wire.requestFrom(DS1307_ADDRESS, 3);
- int second = bcdToDec(Wire.read());
- int minute = bcdToDec(Wire.read());
- int hour = bcdToDec(Wire.read());
- if(hour > 11 && hour < 24){
- AM_PM = true;
- }else{
- AM_PM = false;
- }
- if(hour > 24){
- hour = 1;
- Wire.beginTransmission(DS1307_ADDRESS);
- Wire.write(zero); //stop Oscillator
- Wire.write(decToBcd(second));
- Wire.write(decToBcd(minute));
- Wire.write(decToBcd(hour));
- }
- Wire.write(zero); //start
- Wire.endTransmission();
- }
- void checkInput(){
- boolean minuteButton = mButton.getInput();
- boolean hourButton = hButton.getInput();
- boolean alarmB = alarmButton.getInput();
- boolean timeB = timeButton.getInput();
- if(timeB == true && minuteButton == true){
- changeTime(1);
- }
- if(timeB == true && hourButton == true){
- changeTime(2);
- }
- if(alarmB == true && minuteButton == true){
- changeAlarm(3);
- }
- if(alarmB == true && hourButton == true){
- changeAlarm(4);
- }
- }
- void playAlarm(){
- if(buzzerCounter < 20){
- tone(buzzerPin, 100);
- }
- if(buzzerCounter > 20){
- noTone(buzzerPin);
- }
- if(buzzerCounter > 40){
- buzzerCounter = 0;
- }
- buzzerCounter++;
- }
- void loop () {
- checkInput();
- DateTime now = rtc.now();
- h = now.hour();
- m = now.minute();
- //what is this doing?
- convertHour();
- if(h > 12){
- h = h - 12;
- }
- if(h == 0){
- h = 12;
- }
- //display stuff
- if(alarmButton.getInput() == false){
- if(h / 10 != 0){
- segment(grd[0], h / 10);
- }
- segment(grd[1], h % 10);
- segment(grd[2], m / 10);
- segment(grd[3], m % 10);
- if(AM_PM == true){
- digitalWrite(PM_Light, HIGH);
- }else{
- digitalWrite(PM_Light, LOW);
- }
- }else{
- if(aH / 10 != 0){
- segment(grd[0], aH / 10);
- }
- segment(grd[1], aH % 10);
- segment(grd[2], aM / 10);
- segment(grd[3], aM % 10);
- if(alarmAM_PM == true){
- digitalWrite(PM_Light, HIGH);
- }else{
- digitalWrite(PM_Light, LOW);
- }
- }
- checkAlarmIndicator();
- int snoozeChecker = snoozeButton.getInput();
- if(snoozeChecker == true){
- soundAlarm = false;
- snoozeTime = 5;
- }
- if(lastTime != m && snoozeTime != 0){
- snoozeTime--;
- if(snoozeTime == 0){
- soundAlarm = true;
- }
- }
- if(checkAlarm() == true){
- playAlarm();
- }else{
- noTone(buzzerPin);
- }
- lastTime = m;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement