Components and supplies
Arduino Nano R3
Buzzer
TM1637 4 digit display
LilyPad LED Blue (5pcs)
DS1302 RTC module
Project description
Code
Displaying time from RTC module plus time setup and Alarm setup functionality
arduino
This code is reading current time from the RTC module. Then it also allows to update current time and also setup Alarm time. Setup actions are performed using 3 buttons.
1// Mario's Ideas 2// TM1637 Digital Clock with Setup and Alarm Functionality 3 4#include <Arduino.h> 5#include <TM1637Display.h> 6#include <virtuabotixRTC.h> // 7 8// RTC module declaration 9// PINS CLK, DAT, RST 10virtuabotixRTC myRTC(A1,A2,A3); 11 12// Variables to store previous values read off RTC module 13int minutes; 14int hours; 15// Variables to store the time at which Alarm should go off 16int Alarm_minutes=05; 17int Alarm_hours=18; 18uint8_t Blank[] = {0x0}; 19 20int POSITION=0; 21 22//Table to store 4 digits, used in time and Alarm setup procedure 23int digits[3]; 24 25int interval=0; 26 27#define Alarm 9 // Button to switch alarm mode on or off 28#define Buzer 13 29#define Led 8 30 31 32//Statuses 33boolean Alarm_set=false; 34boolean Alarm_in_progress=false; 35boolean Setup_on= false; 36 37// 4digit display connection pins (Digital Pins) 38#define CLK 4 39#define DIO 5 40 41 42// 4 digit display declaration 43TM1637Display clock(CLK, DIO); 44 45void setup() { 46 47 attachInterrupt(digitalPinToInterrupt(2), Press_A_Button,RISING); 48 attachInterrupt(digitalPinToInterrupt(3), Press_B_Button,RISING); 49 pinMode(Alarm, INPUT); 50 pinMode(Buzer, OUTPUT); 51 pinMode(Led, OUTPUT); 52 digitalWrite(Buzer,LOW); 53 clock.setBrightness(0x0f); 54 Serial.begin(9600); 55} 56 57void Press_A_Button(){ 58 if (interval>3){ 59 if (Setup_on==false){ 60 Setup_on=true; 61 if (Alarm_set==true){ 62 digits[0]= (int)Alarm_hours/10; 63 digits[1]= Alarm_hours-((int)Alarm_hours/10)*10; 64 digits[2]= (int)Alarm_minutes/10; 65 digits[3]= Alarm_minutes-((int)Alarm_minutes/10)*10; 66 } 67 else { 68 digits[0]= (int)myRTC.hours/10; 69 digits[1]= myRTC.hours-((int)myRTC.hours/10)*10; 70 digits[2]= (int)myRTC.minutes/10; 71 digits[3]= myRTC.minutes-((int)myRTC.minutes/10)*10; 72 } 73 } 74 else{ 75 POSITION++; 76 if (POSITION==4){ 77 if (Alarm_set==true){ 78 Alarm_minutes=digits[3]+digits[2]*10; 79 Alarm_hours=digits[1]+digits[0]*10; 80 } 81 else { 82 myRTC.setDS1302Time( 0, digits[3]+digits[2]*10, digits[1]+digits[0]*10,myRTC.dayofweek, myRTC.dayofmonth, myRTC.month, myRTC.year); 83 clock.showNumberDec(digits[0],false,1,0); 84 clock.showNumberDec(digits[1],false,1,1); 85 clock.showNumberDec(digits[2],false,1,2); 86 clock.showNumberDec(digits[3],false,1,3); 87 } 88 POSITION=0; 89 Setup_on=false; 90 } 91 } 92 } 93 interval=0; 94} 95 96void Press_B_Button(){ 97 if (interval>3){ 98 if(Setup_on){ 99 digits[POSITION]=digits[POSITION]+1; 100 if (POSITION==0 and digits[POSITION]==3) digits[POSITION]=0; 101 if (POSITION!=0 and digits[POSITION]==10) digits[POSITION]=0; 102 interval=0; 103 } 104 } 105 interval=0; 106} 107 108void loop() { 109 if (digitalRead(Alarm)==HIGH and Alarm_set==false ){ 110 if (interval>30){ 111 digitalWrite(Led,HIGH); 112 Alarm_set=true; 113 delay(300); 114 } 115 } else{ 116 if (digitalRead(Alarm)==HIGH and Alarm_set==true){ 117 if (interval>30){ 118 Alarm_set=false; 119 Alarm_in_progress=false; 120 digitalWrite(Led,LOW); 121 delay(300); 122 } 123 } 124 } 125 myRTC.updateTime(); 126 127 128 if (Setup_on==true){ 129 clock.showNumberDec(digits[0],false,1,0); 130 clock.showNumberDec(digits[1],false,1,1); 131 clock.showNumberDec(digits[2],false,1,2); 132 clock.showNumberDec(digits[3],false,1,3); 133 delay(200); 134 clock.setSegments(Blank, 1,POSITION); 135 delay(200); 136 clock.showNumberDec(digits[0],false,1,0); 137 clock.showNumberDec(digits[1],false,1,1); 138 clock.showNumberDec(digits[2],false,1,2); 139 clock.showNumberDec(digits[3],false,1,3); 140 } 141 else{ 142 if (myRTC.minutes==Alarm_minutes and myRTC.hours==Alarm_hours and Alarm_set==true and Setup_on==false){ 143 Alarm_in_progress=true; 144 } 145 146 if (Alarm_in_progress){ 147 digitalWrite(Buzer,HIGH); 148 delay(1000); 149 digitalWrite(Buzer,LOW); 150 delay(1000); 151 } 152 153 if (myRTC.minutes!=minutes){ 154 155 clock.showNumberDecEx((int)myRTC.hours/10*1000+(myRTC.hours-((int)myRTC.hours/10)*10)*100+ (int)myRTC.minutes/10*10+myRTC.minutes-((int)myRTC.minutes/10)*10, (0x80 >> 1), true); 156 157 minutes=myRTC.minutes; 158 hours=myRTC.hours; 159 160 } 161 } 162 if (interval<200) interval++; 163}
Displaying time from RTC module plus time setup and Alarm setup functionality
arduino
This code is reading current time from the RTC module. Then it also allows to update current time and also setup Alarm time. Setup actions are performed using 3 buttons.
1// Mario's Ideas 2// TM1637 Digital Clock with Setup and Alarm Functionality 3 4#include 5 <Arduino.h> 6#include <TM1637Display.h> 7#include <virtuabotixRTC.h> // 8 9// 10 RTC module declaration 11// PINS CLK, DAT, RST 12virtuabotixRTC myRTC(A1,A2,A3); 13 14// 15 Variables to store previous values read off RTC module 16int minutes; 17int hours; 18// 19 Variables to store the time at which Alarm should go off 20int Alarm_minutes=05; 21int 22 Alarm_hours=18; 23uint8_t Blank[] = {0x0}; 24 25int POSITION=0; 26 27//Table 28 to store 4 digits, used in time and Alarm setup procedure 29int digits[3]; 30 31int 32 interval=0; 33 34#define Alarm 9 // Button to switch alarm mode on or off 35#define 36 Buzer 13 37#define Led 8 38 39 40//Statuses 41boolean Alarm_set=false; 42boolean 43 Alarm_in_progress=false; 44boolean Setup_on= false; 45 46// 4digit display connection 47 pins (Digital Pins) 48#define CLK 4 49#define DIO 5 50 51 52// 4 digit display 53 declaration 54TM1637Display clock(CLK, DIO); 55 56void setup() { 57 58 attachInterrupt(digitalPinToInterrupt(2), 59 Press_A_Button,RISING); 60 attachInterrupt(digitalPinToInterrupt(3), Press_B_Button,RISING); 61 62 pinMode(Alarm, INPUT); 63 pinMode(Buzer, OUTPUT); 64 pinMode(Led, OUTPUT); 65 66 digitalWrite(Buzer,LOW); 67 clock.setBrightness(0x0f); 68 Serial.begin(9600); 69} 70 71void 72 Press_A_Button(){ 73 if (interval>3){ 74 if (Setup_on==false){ 75 Setup_on=true; 76 77 if (Alarm_set==true){ 78 digits[0]= (int)Alarm_hours/10; 79 digits[1]= 80 Alarm_hours-((int)Alarm_hours/10)*10; 81 digits[2]= (int)Alarm_minutes/10; 82 83 digits[3]= Alarm_minutes-((int)Alarm_minutes/10)*10; 84 } 85 else 86 { 87 digits[0]= (int)myRTC.hours/10; 88 digits[1]= myRTC.hours-((int)myRTC.hours/10)*10; 89 90 digits[2]= (int)myRTC.minutes/10; 91 digits[3]= myRTC.minutes-((int)myRTC.minutes/10)*10; 92 93 } 94 } 95 else{ 96 POSITION++; 97 if (POSITION==4){ 98 99 if (Alarm_set==true){ 100 Alarm_minutes=digits[3]+digits[2]*10; 101 102 Alarm_hours=digits[1]+digits[0]*10; 103 } 104 else { 105 106 myRTC.setDS1302Time( 0, digits[3]+digits[2]*10, digits[1]+digits[0]*10,myRTC.dayofweek, 107 myRTC.dayofmonth, myRTC.month, myRTC.year); 108 clock.showNumberDec(digits[0],false,1,0); 109 110 clock.showNumberDec(digits[1],false,1,1); 111 clock.showNumberDec(digits[2],false,1,2); 112 113 clock.showNumberDec(digits[3],false,1,3); 114 } 115 POSITION=0; 116 117 Setup_on=false; 118 } 119 } 120 } 121 interval=0; 122} 123 124void 125 Press_B_Button(){ 126 if (interval>3){ 127 if(Setup_on){ 128 digits[POSITION]=digits[POSITION]+1; 129 130 if (POSITION==0 and digits[POSITION]==3) digits[POSITION]=0; 131 if (POSITION!=0 132 and digits[POSITION]==10) digits[POSITION]=0; 133 interval=0; 134 } 135 136 } 137 interval=0; 138} 139 140void loop() { 141 if (digitalRead(Alarm)==HIGH 142 and Alarm_set==false ){ 143 if (interval>30){ 144 digitalWrite(Led,HIGH); 145 146 Alarm_set=true; 147 delay(300); 148 } 149 } else{ 150 if (digitalRead(Alarm)==HIGH 151 and Alarm_set==true){ 152 if (interval>30){ 153 Alarm_set=false; 154 Alarm_in_progress=false; 155 156 digitalWrite(Led,LOW); 157 delay(300); 158 } 159 } 160 } 161 myRTC.updateTime(); 162 163 164 165 if (Setup_on==true){ 166 clock.showNumberDec(digits[0],false,1,0); 167 168 clock.showNumberDec(digits[1],false,1,1); 169 clock.showNumberDec(digits[2],false,1,2); 170 171 clock.showNumberDec(digits[3],false,1,3); 172 delay(200); 173 clock.setSegments(Blank, 174 1,POSITION); 175 delay(200); 176 clock.showNumberDec(digits[0],false,1,0); 177 178 clock.showNumberDec(digits[1],false,1,1); 179 clock.showNumberDec(digits[2],false,1,2); 180 181 clock.showNumberDec(digits[3],false,1,3); 182 } 183 else{ 184 if (myRTC.minutes==Alarm_minutes 185 and myRTC.hours==Alarm_hours and Alarm_set==true and Setup_on==false){ 186 Alarm_in_progress=true; 187 188 } 189 190 if (Alarm_in_progress){ 191 digitalWrite(Buzer,HIGH); 192 delay(1000); 193 194 digitalWrite(Buzer,LOW); 195 delay(1000); 196 } 197 198 if (myRTC.minutes!=minutes){ 199 200 201 clock.showNumberDecEx((int)myRTC.hours/10*1000+(myRTC.hours-((int)myRTC.hours/10)*10)*100+ 202 (int)myRTC.minutes/10*10+myRTC.minutes-((int)myRTC.minutes/10)*10, (0x80 >> 1), 203 true); 204 205 minutes=myRTC.minutes; 206 hours=myRTC.hours; 207 208 } 209 210 } 211 if (interval<200) interval++; 212}
Downloadable files
Schematics
Schematics
Comments
Only logged in users can leave comments
mdraber
0 Followers
•0 Projects
Table of contents
Intro
9
0