Created
August 20, 2014 07:02
-
-
Save sj82516/efd30e89d3056c7ecd40 to your computer and use it in GitHub Desktop.
Arduino_personal work_alarm_v1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <LiquidCrystal_I2C.h> | |
#include <virtuabotixRTC.h> | |
//use interrupt 0 -> pin 2 (button0) | |
#define lock 0 | |
//button1 | |
#define add_sec 4 | |
//button2 | |
#define add_min 3 | |
//RGB LED | |
#define R 11 | |
#define G 10 | |
#define B 9 | |
//consult library | |
virtuabotixRTC myRTC(6, 7, 8); | |
LiquidCrystal_I2C lcd(0x27,16,2); | |
uint8_t set_time[3]={0,0,0}; | |
uint8_t temp_time[3]={0,0,0}; | |
//mode(HIGH for display time , LOW for set alarm) | |
volatile int state=HIGH; | |
void setup() { | |
//LCD setup | |
lcd.init(); | |
lcd.backlight(); | |
//interrupt 0 -> pin 2 | |
attachInterrupt(lock,timeMode,FALLING); | |
//set time | |
pinMode(add_sec,INPUT); | |
pinMode(add_min,INPUT); | |
//RGB LED as alarm | |
pinMode(R,OUTPUT); | |
pinMode(G,OUTPUT); | |
pinMode(B,OUTPUT); | |
//DS1302 setup | |
myRTC.setDS1302Time(00, 59, 23, 6, 10, 1, 2014); | |
Serial.begin(9600); | |
} | |
void loop() { | |
if(state==HIGH){ | |
displayTime(); | |
}else{ | |
setAlarm(); | |
} | |
} | |
void timeMode(){ | |
digitalWrite(R,LOW); | |
digitalWrite(G,LOW); | |
digitalWrite(B,HIGH); | |
if(state==HIGH){ | |
set_time[0]=temp_time[0]; | |
set_time[1]=temp_time[1]; | |
set_time[2]=temp_time[2]; | |
} | |
state=!state; | |
} | |
void setAlarm(){ | |
digitalWrite(R,LOW); | |
digitalWrite(G,LOW); | |
digitalWrite(B,HIGH); | |
//set up alarm | |
if(digitalRead(add_min)==0)set_time[1]++; | |
if(digitalRead(add_sec)==0)set_time[2]++; | |
//carry in or no | |
if(set_time[0]>=24){ | |
set_time[0]=set_time[0]-24; | |
} | |
if(set_time[1]>=60){ | |
set_time[0]++; | |
set_time[1]=set_time[1]-60;} | |
if(set_time[2]>=60){ | |
set_time[1]++; | |
set_time[2]=set_time[2]-60;} | |
//output time | |
lcd.setCursor(0,1); | |
lcd.print(set_time[0]); | |
lcd.print(":"); | |
lcd.print(set_time[1]); | |
lcd.print(":"); | |
lcd.print(set_time[2]); | |
delay(250); | |
lcd.clear(); | |
} | |
void displayTime(){ | |
//ouput time | |
myRTC.updateTime(); | |
lcd.setCursor(0,0); | |
lcd.print(myRTC.hours); | |
lcd.print(":"); | |
lcd.print(myRTC.minutes); | |
lcd.print(":"); | |
lcd.print(myRTC.seconds); | |
//save temporary time | |
temp_time[0]=myRTC.hours; | |
temp_time[1]=myRTC.minutes; | |
temp_time[2]=myRTC.seconds; | |
delay(1000); | |
lcd.clear(); | |
//check the time whether reach the alarm time or not. | |
if(set_time[0]==myRTC.hours && set_time[1]==myRTC.minutes && set_time[2]==myRTC.seconds){ | |
digitalWrite(R,HIGH); | |
digitalWrite(G,LOW); | |
digitalWrite(B,LOW); | |
delay(2000); | |
}else{ | |
digitalWrite(R,LOW); | |
digitalWrite(G,HIGH); | |
digitalWrite(B,LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment