Components and supplies
Power MOSFET N-Channel
DS3231M - ±5ppm, I2C Real-Time Clock
LED (generic)
Arduino Mega 2560
Tools and machines
Soldering iron (generic)
Apps and platforms
Arduino IDE
Project description
Code
Clock
c_cpp
Digital Wall Clock code
1#include <EEPROM.h> 2#include <Sodaq_DS3231.h> 3#include <Wire.h> 4 5/* 6 * 7 * Output A frequency: 16 MHz / 8 / 256 = 7812.5Hz//Prescaler is 8//CS21 is ON 8 Output A duty cycle: (180+1) / 256 = 70.7% 9 Output B frequency: 16 MHz / 8 / 256 = 7812.5Hz //Prescaler is 8//CS21 is ON 10 Output B duty cycle: (50+1) / 256 = 19.9% 11 12The output frequency is the 16MHz system clock frequency, divided by the prescaler value (64), divided by the 256 cycles it takes for the timer to wrap around. 13Note that fast PWM holds the output high one cycle longer than the compare register value. 14 */ 15boolean set_button;//setting button 16boolean dec_button;//decrease button 17boolean inc_button;//increase button 18 19boolean trig_set_button; 20boolean trig_dec_button; 21boolean trig_inc_button; 22boolean trig_writing; 23boolean trig_dots = false; 24boolean trig_temp; 25 26int clock_array[10]={126,48,109,121,51,91,95,112,127,123};//from 0 to 9 27int seg1_array[7] = {23,25,27,29,31,33,35};//G,F,E,D,C,B,A 28int seg2_array[7] = {22,24,26,28,30,32,34};//G,F,E,D,C,B,A 29int seg3_array[7] = {37,39,41,43,45,47,49};//G,F,E,D,C,B,A 30int seg4_array[7] = {36,38,40,42,44,46,48};//G,F,E,D,C,B,A 31 32unsigned int tm_second, tm_minute, tm_hour, tm_day, tm_month, tm_year; 33uint32_t old_ts; //RTC data// 34int h_t; 35int h_d; 36int m_t; 37int m_d; 38int pwm_h;//hundred 39int pwm_t;//ten 40int pwm_d; 41short previous_second; 42 43float temp; 44unsigned int temp_int; 45short temp_1; 46short temp_2; 47short temp_3; 48short temp_4; 49unsigned int pwm_brightness; 50 51int mode = 0; 52 53void setup() { 54 //INPUTS// 55 pinMode(11, INPUT_PULLUP); 56 pinMode(12, INPUT_PULLUP); 57 pinMode(13, INPUT_PULLUP); 58 //PWM SETUP// 59 pinMode(10, OUTPUT); //OCR2A PIN 10 for MEGA2560//PIN 3 for UNO 60 pinMode(9, OUTPUT);//OCR2B PIN 9 for MEGA2560//PIN 11 for UNO 61 TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); 62 TCCR2B = _BV(CS21);//7745Hz 63 pwm_brightness = EEPROM.read(0); 64 //Clock SETUP// 65 Wire.begin(); //enable I2C// 66 rtc.begin(); //enable RTC// 67 //OUTPUTS// 68 pinMode(22, OUTPUT); 69 pinMode(23, OUTPUT); 70 pinMode(24, OUTPUT); 71 pinMode(25, OUTPUT); 72 pinMode(26, OUTPUT); 73 pinMode(27, OUTPUT); 74 pinMode(28, OUTPUT); 75 pinMode(29, OUTPUT); 76 pinMode(30, OUTPUT); 77 pinMode(31, OUTPUT); 78 pinMode(32, OUTPUT); 79 pinMode(33, OUTPUT); 80 pinMode(34, OUTPUT); 81 pinMode(35, OUTPUT); 82 pinMode(36, OUTPUT); 83 pinMode(37, OUTPUT); 84 pinMode(38, OUTPUT); 85 pinMode(39, OUTPUT); 86 pinMode(40, OUTPUT); 87 pinMode(41, OUTPUT); 88 pinMode(42, OUTPUT); 89 pinMode(43, OUTPUT); 90 pinMode(44, OUTPUT); 91 pinMode(45, OUTPUT); 92 pinMode(46, OUTPUT); 93 pinMode(47, OUTPUT); 94 pinMode(48, OUTPUT); 95 pinMode(49, OUTPUT); 96 pinMode(50, OUTPUT);//upper dot 97 pinMode(51, OUTPUT);//down dot 98} 99 100void loop() { 101set_button = !digitalRead(13); 102dec_button = !digitalRead(12); 103inc_button = !digitalRead(11); 104 105if(set_button==false){trig_set_button = false;} 106if(set_button==true&&trig_set_button==false) 107{ 108 mode++; 109 if(mode>3){mode = 0;}//only 3 modes enabled 110 trig_set_button = true; 111} 112if(previous_second!=tm_second) 113{ 114 trig_dots = !trig_dots; 115 previous_second = tm_second; 116} 117/////////////////////////////////// 118//Buttons Mode configuration// 119switch (mode) 120{ 121 case 0: 122 break; 123 case 1: 124 trig_writing = true;//enabling this trig to write data one shoot 125 126 if(dec_button==false){trig_dec_button = false;} 127 if(dec_button==true&&trig_dec_button==false) 128 { 129 if(tm_hour>0&&tm_hour<=24){tm_hour--;} 130 trig_dec_button = true; 131 } 132 if(inc_button==false){trig_inc_button = false;} 133 if(inc_button==true&&trig_inc_button==false) 134 { 135 if(tm_hour>=0&&tm_hour<24){tm_hour++;} 136 trig_inc_button = true; 137 } 138 break; 139 case 2: 140 if(dec_button==false){trig_dec_button = false;} 141 if(dec_button==true&&trig_dec_button==false) 142 { 143 if(tm_minute>0&&tm_minute<=59){tm_minute--;} 144 trig_dec_button = true; 145 } 146 if(inc_button==false){trig_inc_button = false;} 147 if(inc_button==true&&trig_inc_button==false) 148 { 149 if(tm_minute>=0&&tm_minute<59){tm_minute++;} 150 trig_inc_button = true; 151 } 152 break; 153 case 3: 154 if(dec_button==false){trig_dec_button = false;} 155 if(dec_button==true&&trig_dec_button==false) 156 { 157 if(pwm_brightness>0&&pwm_brightness<=255){pwm_brightness--;} 158 trig_dec_button = true; 159 } 160 if(inc_button==false){trig_inc_button = false;} 161 if(inc_button==true&&trig_inc_button==false) 162 { 163 if(pwm_brightness>=0&&pwm_brightness<255){pwm_brightness++;} 164 trig_inc_button = true; 165 } 166 break; 167} 168if(mode==0){ 169 //RTC// 170 if(trig_writing==true)//writing in one shoot mode 171 { 172 DateTime dt(2019, 2, 19, tm_hour, tm_minute, 0, 0); 173 rtc.setDateTime(dt); 174 EEPROM.write(0, pwm_brightness); 175 delay(50); 176 trig_writing = false; 177 } 178 rtc.convertTemperature(); 179 if(trig_temp==false){temp = rtc.getTemperature();}//not to update temperature value when display the temp 180 DateTime now = rtc.now(); //get the current date-time 181 uint32_t ts = now.getEpoch(); 182 if (old_ts == 0 || old_ts != ts) { 183 old_ts = ts; 184 tm_second = now.second(); 185 tm_minute = now.minute(); 186 tm_hour = now.hour(); 187 tm_day = now.date(); 188 tm_month = now.month(); 189 tm_year = now.year(); 190 } 191}//end if mode 192 //separating hour and minute// 193 h_t = (tm_hour % 100)/10; 194 h_d = tm_hour % 10; 195 m_t = (tm_minute % 100)/10; 196 m_d = tm_minute % 10; 197 ////////////////////// 198 199 //separating pwm_brightness// 200 pwm_h = (pwm_brightness % 1000)/100; 201 pwm_t = (pwm_brightness % 100)/10; 202 pwm_d = pwm_brightness % 10; 203 ////////////////////// 204 205 //separating temperature// 206 temp_int = temp * 100;//convert point float to integer 207 temp_1 = (temp_int % 10000)/1000; 208 temp_2 = (temp_int % 1000)/100; 209 temp_3 = (temp_int % 100)/10; 210 temp_4 = temp_int % 10; 211 ////////////////////// 212for(int i=0;i<7;i++) 213{ 214 switch (mode) 215 { 216 case 0://normal time mode 217 218 if(tm_second<30||tm_second>32)//display the DS3231 temperature between 30 and 32 second of each minute 219 { 220 trig_temp = false; 221 if(tm_hour>9) 222 { 223 digitalWrite(seg1_array[i], bitRead(clock_array[h_t], i));//Output of 1 segment 224 digitalWrite(seg2_array[i], bitRead(clock_array[h_d], i));//Output of 2 segment 225 }else 226 { 227 digitalWrite(seg1_array[i], bitRead(clock_array[0], i));//Output of 1 segment 228 digitalWrite(seg2_array[i], bitRead(clock_array[tm_hour], i));//Output of 2 segment 229 } 230 if(tm_minute>9) 231 { 232 digitalWrite(seg3_array[i], bitRead(clock_array[m_t], i));//Output of 3 segment 233 digitalWrite(seg4_array[i], bitRead(clock_array[m_d], i));//Output of 4 segment 234 }else//displaying DS3231 temperature 235 { 236 digitalWrite(seg3_array[i], bitRead(clock_array[0], i));//Output of 3 segment 237 digitalWrite(seg4_array[i], bitRead(clock_array[tm_minute], i));//Output of 4 segment 238 } 239 digitalWrite(50, trig_dots); 240 digitalWrite(51, trig_dots); 241 }else 242 { 243 trig_temp = true; 244 digitalWrite(seg1_array[i], bitRead(clock_array[temp_1], i));//Output of 1 segment 245 digitalWrite(seg2_array[i], bitRead(clock_array[temp_2], i));//Output of 2 segment 246 digitalWrite(seg3_array[i], bitRead(clock_array[temp_3], i));//Output of 3 segment 247 digitalWrite(seg4_array[i], bitRead(clock_array[temp_4], i));//Output of 4 segment 248 digitalWrite(50, LOW); 249 digitalWrite(51, HIGH); 250 } 251 break; 252 case 1://adjusting hour 253 if(tm_hour>9) 254 { 255 digitalWrite(seg1_array[i], bitRead(clock_array[h_t], i));//Output of 1 segment 256 digitalWrite(seg2_array[i], bitRead(clock_array[h_d], i));//Output of 2 segment 257 }else 258 { 259 digitalWrite(seg1_array[i], bitRead(clock_array[0], i));//Output of 1 segment 260 digitalWrite(seg2_array[i], bitRead(clock_array[tm_hour], i));//Output of 2 segment 261 } 262 digitalWrite(seg3_array[i], 0);//OFF the 3 segment 263 digitalWrite(seg4_array[i], 0);//OFF the 4 segment 264 digitalWrite(50, 0);//OFF the dot 265 digitalWrite(50, 0);//OFF the dot 266 break; 267 case 2://adjustng minute 268 if(tm_minute>9) 269 { 270 digitalWrite(seg3_array[i], bitRead(clock_array[m_t], i));//Output of 3 segment 271 digitalWrite(seg4_array[i], bitRead(clock_array[m_d], i));//Output of 4 segment 272 }else 273 { 274 digitalWrite(seg3_array[i], bitRead(clock_array[0], i));//Output of 3 segment 275 digitalWrite(seg4_array[i], bitRead(clock_array[tm_minute], i));//Output of 4 segment 276 } 277 digitalWrite(seg1_array[i], 0);//OFF the 1 segment 278 digitalWrite(seg2_array[i], 0);//OFF the 2 segment 279 digitalWrite(50, 0);//OFF the dot 280 digitalWrite(50, 0);//OFF the dot 281 break; 282 case 3://adjusting pwm 283 if(pwm_brightness>99) 284 { 285 digitalWrite(seg1_array[i], LOW);//Output of 1 segment 286 digitalWrite(seg2_array[i], bitRead(clock_array[pwm_h], i));//Output of 2 segment 287 digitalWrite(seg3_array[i], bitRead(clock_array[pwm_t], i));//Output of 3 segment 288 digitalWrite(seg4_array[i], bitRead(clock_array[pwm_d], i));//Output of 4 segment 289 }else if(pwm_brightness>9&&pwm_brightness<=99) 290 { 291 digitalWrite(seg1_array[i], LOW);//Output of 1 segment 292 digitalWrite(seg2_array[i], LOW);//Output of 2 segment 293 digitalWrite(seg3_array[i], bitRead(clock_array[pwm_t], i));//Output of 3 segment 294 digitalWrite(seg4_array[i], bitRead(clock_array[pwm_d], i));//Output of 4 segment 295 }else if(pwm_brightness<=9) 296 { 297 digitalWrite(seg1_array[i], LOW);//Output of 1 segment 298 digitalWrite(seg2_array[i], LOW);//Output of 2 segment 299 digitalWrite(seg3_array[i], LOW);//Output of 3 segment 300 digitalWrite(seg4_array[i], bitRead(clock_array[pwm_d], i));//Output of 4 segment 301 } 302 digitalWrite(50, 0);//OFF the dot 303 digitalWrite(51, 0);//OFF the dot 304 break; 305 }//end switch(mode) 306}//end for 307 OCR2A = pwm_brightness; 308 OCR2B = 5;//has no effect 309}//end loop 310
Downloadable files
Schematic connection
Schematic connection of clock
Schematic connection
Schematic connection
Schematic connection of clock
Schematic connection
Comments
Only logged in users can leave comments
Deep_Sky
0 Followers
•0 Projects
Table of contents
Intro
21
0