Components and supplies
Resistor 1k ohm
Arduino Nano R3
General Purpose Transistor NPN
Real Time Clock Module, DS3231
Photo resistor
Resistor 560 ohm
Red LED display-4 digit,
5 mm LED: Red
Project description
Code
Retro Clock/Timer Code
arduino
Controls the Retro clock/timer
1#include <RTClib.h> 2#include <tm1637.h> 3#include <Wire.h> 4 5RTC_DS3231 rtc; 6DateTime NOW; 7bool timedOut; 8bool modeClock; 9bool modeTimer; 10bool modeDateClock; 11bool timerRunning;// use for pausing timer 12 13uint8_t button1 = 11;// Pin Definitions 14uint8_t button2 = 10; 15uint8_t button3 = 9; 16uint8_t led0 = 2; 17uint8_t led1 = 3; 18uint8_t led2 = 4; 19uint8_t led3 = 5; 20uint8_t ldr = A7; 21uint8_t ledPWMPin = 6; 22 23const int AM = 1;// Constants to set indicator LEDs to AM or PM or OFF (led0 = AM, led3=PM) 24const int PM = 5; 25const int NONE = 0; 26 27uint8_t MONTHx = 0;// Field editing flags for setting clock 28uint8_t DAYx = 1; 29uint8_t YEARx = 2; 30uint8_t DAYofWEEKx = 3; 31uint8_t HOURx = 4; 32uint8_t MINUTEx = 5; 33 34long lapStartMillis;// Timer Lap Start Value 35long totalMillis;// Timer Lapsed Time Count 36 37uint8_t brightness; 38bool clockMode_12 = true; 39bool leadingZero = false; 40char dspVal[8];// Char array to store values to be sent to display 41 42void setup() { 43 44 Serial.begin(9600); 45 timedOut = false; 46 if (! rtc.begin()) {// start RTC module communication 47 Serial.println("Couldn't find RTC"); 48 //while (1); 49 } 50 if (rtc.lostPower()) {// This will initialize clock setting after battery change etc. when clock HAS NOT been set 51 Serial.println("RTC needs set!"); 52 // following line sets the RTC to the date & time this sketch was compiled 53 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 54 // This line sets the RTC with an explicit date & time, for example to set 55 // January 21, 2014 at 3am you would call: 56 // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); 57 } 58 59 pinMode(button1, INPUT_PULLUP); 60 pinMode(button2, INPUT_PULLUP); 61 pinMode(button3, INPUT_PULLUP); 62 pinMode(led0, OUTPUT); 63 pinMode(led1, OUTPUT); 64 pinMode(led2, OUTPUT); 65 pinMode(led3, OUTPUT); 66 pinMode(ledPWMPin, OUTPUT); 67 digitalWrite(led0, LOW); 68 digitalWrite(led1, LOW); 69 digitalWrite(led2, LOW); 70 digitalWrite(led3, LOW); 71 72 pinMode(ldr, INPUT); 73 brightness = 3; 74 modeClock = true; 75 NOW = rtc.now(); 76 77 ledSetBrightness(3); 78 ledSequence(1); 79 ledSequence(0); 80 tm1637Init(7, 8); // clock, data pins of LED Display 81 tm1637SetBrightness(brightness); 82}// ******************************************* End of setup 83 84void ledSequence(int cmd) { 85 // display short sequences on the LED column. 86 //Turn all LEDs OFF to start 87 digitalWrite(led0, LOW); 88 digitalWrite(led1, LOW); 89 digitalWrite(led2, LOW); 90 digitalWrite(led3, LOW); 91 if (cmd) { 92 digitalWrite(led0, HIGH); 93 delay(100); 94 digitalWrite(led1, HIGH); 95 delay(100); 96 digitalWrite(led2, HIGH); 97 delay(100); 98 digitalWrite(led3, HIGH); 99 delay(100); 100 digitalWrite(led0, LOW); 101 delay(100); 102 digitalWrite(led1, LOW); 103 delay(100); 104 digitalWrite(led2, LOW); 105 delay(100); 106 digitalWrite(led3, LOW); 107 delay(100); 108 109 } 110 else { 111 digitalWrite(led3, HIGH); 112 delay(100); 113 digitalWrite(led2, HIGH); 114 delay(100); 115 digitalWrite(led1, HIGH); 116 delay(100); 117 digitalWrite(led0, HIGH); 118 delay(100); 119 digitalWrite(led3, LOW); 120 delay(100); 121 digitalWrite(led2, LOW); 122 delay(100); 123 digitalWrite(led1, LOW); 124 delay(100); 125 digitalWrite(led0, LOW); 126 delay(100); 127 } 128 129}// ******************************************* End of ledSequence 130 131long numberGet(uint8_t myField, int startVal, int maxVal) { 132 // Get user input for setting clock/timer/etc. 133 bool gotNumber = false; 134 135 //bool timeOut = millis()+10000; 136 timedOut = false; 137 int debounce = 30; 138 char dspTemp[8]; 139 140 while (!gotNumber) { 141 NOW = rtc.now();// This is only done here to provide flashing digits when setting. 142 switch (myField) { 143 case 0: 144 145 if (NOW.second() & 1) { 146 Serial.println("Setting Month"); 147 sprintf(dspTemp, "%02d ", startVal); 148 } 149 else { 150 sprintf(dspTemp, " : " ); 151 } 152 153 break; 154 case 1://day 155 Serial.println("Setting Day"); 156 if (NOW.second() & 1) { 157 sprintf(dspTemp, " %02d", startVal); 158 } 159 else { 160 sprintf(dspTemp, " : " ); 161 } 162 163 break; 164 case 2://year 165 Serial.println("Setting Year"); 166 if (NOW.second() & 1) { 167 sprintf(dspTemp, "%02d %02d", startVal / 100, startVal % 100); 168 } 169 else { 170 sprintf(dspTemp, " " ); 171 } 172 173 break; 174 175 case 4://hour 176 if (NOW.second() & 1) { 177 sprintf(dspTemp, "%02d ", startVal); 178 } 179 else { 180 sprintf(dspTemp, " : " ); 181 } 182 183 break; 184 185 case 5://minute 186 if (NOW.second() & 1) { 187 sprintf(dspTemp, " %02d", startVal); 188 } 189 else { 190 sprintf(dspTemp, " : " ); 191 } 192 break; 193 } 194 tm1637ShowDigits(dspTemp); 195 196 // Check for button presses here 197 if (digitalRead(button1) == LOW) { 198 delay(debounce); 199 if (digitalRead(button1) == LOW) { 200 201 while (digitalRead(button1) == LOW) { // Lets pause until button released 202 203 } 204 } 205 startVal--; 206 //timeOut = millis() + 10000; 207 delay(300); 208 } 209 if (digitalRead(button2) == LOW) { 210 delay(debounce); 211 if (digitalRead(button2) == LOW) { 212 213 while (digitalRead(button2) == LOW) { // Lets pause until button released 214 215 } 216 } 217 startVal++; 218 //timeOut = millis() + 10000; 219 delay(300); 220 } 221 if (digitalRead(button3) == LOW) { 222 delay(debounce); 223 if (digitalRead(button3) == LOW) { 224 225 while (digitalRead(button3) == LOW) { // Lets pause until button released 226 227 } 228 } 229 gotNumber = true; 230 if (myField < 5) { // DON'T delay if minutes field is being set. 231 delay(300); 232 } 233 234 } 235 } 236 237 if (startVal > maxVal) { 238 startVal = 0; 239 } 240 if (startVal < 0) { 241 startVal = maxVal; 242 } 243 return (startVal); 244}// ******************************************* End of numberGet 245 246 247void clockSet() { 248 bool doneSetting = false; 249 bool settingMonth = true; 250 bool settingDay = false; 251 bool settingWeekDay = false; 252 bool settingYear = false; 253 bool settingHours = false; 254 bool settingMinutes = false; 255 int myMonth, myDay, myYear, myHour, myMinute; 256 257 NOW = rtc.now(); 258 myMonth = NOW.month(); 259 myDay = NOW.day(); 260 myYear = NOW.year(); 261 myHour = NOW.hour(); 262 myMinute = NOW.minute(); 263 Serial.println(myMonth); 264 Serial.println(myDay); 265 Serial.println(myYear); 266 Serial.println(myHour); 267 Serial.println(myMinute); 268 269 while (!doneSetting) { 270 271 while (settingMonth & !timedOut) { // Month Set routine here 272 myMonth = numberGet(MONTHx, myMonth, 12); 273 settingMonth = false; 274 settingDay = true; 275 } 276 while (settingDay & !timedOut) { 277 //Day Set Routine here 278 myDay = numberGet(DAYx, myDay, 31); 279 settingDay = false; 280 settingYear = true; 281 } 282 while (settingYear & !timedOut) { 283 // Year Set Routine here 284 myYear = numberGet(YEARx, myYear, 3000); 285 settingYear = false; 286 settingHours = true; 287 } 288 while (settingHours & !timedOut) { 289 // hour Set Routine here 290 myHour = numberGet(HOURx, myHour, 23); 291 settingHours = false; 292 settingMinutes = true; 293 } 294 while (settingMinutes & !timedOut) { 295 // Minute Set Routine here 296 myMinute = numberGet(MINUTEx, myMinute, 59); 297 settingMinutes = false; 298 doneSetting = true; 299 } 300 301 } 302 if (!timedOut) { 303 rtc.adjust(DateTime(myYear, myMonth, myDay, myHour, myMinute, 0)); 304 } 305 timedOut = false; 306}// ******************************************* End of clockSet 307 308void ledSet(int setting) {// Set the 4 LEDs that display timer hours count/AM/PM 309 static int oldSetting; 310 311 if (oldSetting != setting) { // only update if setting changes 312 //Turn all LEDs OFF to start 313 digitalWrite(led0, LOW); 314 digitalWrite(led1, LOW); 315 digitalWrite(led2, LOW); 316 digitalWrite(led3, LOW); 317 318 if (setting > 4) { // Display PM indication 319 digitalWrite(led3, HIGH); 320 } 321 else { 322 if (setting > 3) { 323 digitalWrite(led3, HIGH); 324 } 325 if (setting > 2) { 326 digitalWrite(led2, HIGH); 327 } 328 if (setting > 1) { 329 digitalWrite(led1, HIGH); 330 } 331 if (setting > 0) { 332 digitalWrite(led0, HIGH); 333 } 334 } 335 oldSetting = setting; 336 } 337}// ******************************************* End of ledSet 338 339void ledSetBrightness(int setting) { 340// Setting multipliers were chosen by trial and error, to best match the display brightness settings. 341// Other LED/resistor/display combinations may require different values. 342 Serial.println( setting); 343 if (setting == 1) { 344 analogWrite(ledPWMPin, setting * 25); 345 } 346 if (setting == 2) { 347 analogWrite(ledPWMPin, setting * 25); 348 } 349 if (setting == 3) { 350 analogWrite(ledPWMPin, setting * 40); 351 } 352 if (setting == 4) { 353 analogWrite(ledPWMPin, setting * 50); 354 } 355 if (setting == 5) { 356 analogWrite(ledPWMPin, setting * 51); 357 } 358 359}// ******************************************* End of ledSetBrightness 360 361void showClock() { 362 363 NOW = rtc.now(); 364 int myHour = NOW.hour(); 365 bool morning = (myHour < 12); // 366 int myMin = NOW.minute(); 367 if (myHour > 12) { 368 369 myHour -= (12 * clockMode_12); //Displays 12 or 24 hour modes 370 } 371 if (NOW.second() & 1) { 372 ledSet(NONE); 373 if ((myHour < 10) && clockMode_12 && !leadingZero) { 374 sprintf(dspVal, " %01d:%02d", myHour, myMin); 375 376 } 377 else { 378 sprintf(dspVal, "%02d:%02d", myHour, myMin); 379 380 } 381 382 } 383 else { 384 if (morning) { 385 ledSet(AM); 386 } 387 else { 388 ledSet(PM); 389 } 390 if ((myHour < 10) && clockMode_12 && !leadingZero) { 391 sprintf(dspVal, " %01d %02d", myHour, myMin); 392 393 } 394 else { 395 sprintf(dspVal, "%02d %02d", myHour, myMin); 396 } 397 } 398 399}// ******************************************* End of showClock 400 401void showTimer() { 402 403 404 long currentMillis = totalMillis; 405 406 if (timerRunning) { 407 currentMillis = totalMillis + (millis() - lapStartMillis); 408 } 409 currentMillis /= 1000; // convert to seconds 410 int tmrHours = (currentMillis / 60) / 60; 411 if (tmrHours > 4) { 412 413 tmrHours -= 4; 414 } 415 int curMins = (currentMillis / 60) % 60; 416 int curSecs = currentMillis % 60; 417 418 sprintf(dspVal, "%02d:%02d", curMins, curSecs); 419 ledSet(tmrHours); 420 421}// ******************************************* End of showTImer 422 423void readButtons() { 424 425 long holdTimer; 426 int debounce = 30; 427 428 if (digitalRead(button3) == LOW) { 429 delay(debounce); 430 if (digitalRead(button3) == LOW) { 431 holdTimer = millis(); 432 while (digitalRead(button3) == LOW) { // Lets pause until button released 433 434 } 435 if (millis() > holdTimer + 5000) { 436 Serial.println ("clock Mode Changed"); 437 clockMode_12 = !clockMode_12; 438 ledSequence(1); 439 ledSequence(0); 440 ledSequence(1); 441 ledSequence(0); 442 } 443 444 else if (millis() > holdTimer + 2300) { 445 ledSequence(1); 446 Serial.println("Setting Clock"); 447 clockSet(); 448 449 } 450 451 else { 452 453 if (modeClock) { 454 modeTimer = true; 455 modeClock = false; 456 } 457 else if (modeTimer) { 458 modeClock = true; 459 modeTimer = false; 460 } 461 else if (modeDateClock) { 462 463 modeClock = true; 464 modeDateClock = false; 465 } 466 } 467 468 delay(500); 469 } 470 } 471 472 473 if (digitalRead(button1) == LOW) { 474 delay(debounce); 475 if (digitalRead(button1) == LOW) { 476 477 holdTimer = millis(); 478 while (digitalRead(button1) == LOW) { // Lets pause until button released 479 480 } 481 if (millis() > holdTimer + 2300) { 482 Serial.println ("Leading Zero Changed"); 483 leadingZero = !leadingZero; 484 ledSequence(0); 485 ledSequence(1); 486 } 487 else { 488 modeTimer = true; 489 modeClock = false; 490 timerRunning = !timerRunning; 491 if (timerRunning) { // Lap has just been started 492 lapStartMillis = millis(); 493 494 } 495 else { // Lap ending, 496 totalMillis += millis() - lapStartMillis; 497 498 } 499 } 500 } 501 } 502 if (digitalRead(button2) == LOW) { 503 delay(debounce); 504 if (digitalRead(button2) == LOW) { 505 holdTimer = millis(); 506 507 while (digitalRead(button2) == LOW) { // Lets pause until button released 508 509 } 510 if (millis() > holdTimer + 6000) { 511 Serial.println("Set Clock"); 512 delay(1000); 513 } 514 515 else if (millis() > holdTimer + 3000) { 516 clockMode_12 = !clockMode_12; 517 } 518 519 else { 520 if (!timerRunning) { 521 totalMillis = 0;// Reset timer to 0 522 sprintf(dspVal, "%02d %02d", 0, 0); 523 tm1637ShowDigits(dspVal); 524 525 } 526 527 } 528 delay(500); 529 } 530 } 531}// ******************************************* End of readButtons 532 533void lightMeter() { // Used to read light level and set brightness of LEDs/Display 534 static int oldBrt; 535 int myBrt = 6 - (analogRead(ldr) / 150 - 1);// Read value from Light Dependent Resistor, 536 // convert to value from 1 to 5 537 if (oldBrt != myBrt) {// Only adjsut if light reading changes 538 ledSetBrightness(myBrt); 539 tm1637SetBrightness(myBrt); 540 oldBrt = myBrt; 541 } 542}// ******************************************* End of lightMeter 543 544void loop() { 545 546 readButtons();// Check to see if any buttons are pressed 547 lightMeter();// check/adjust display brightness if needed 548 549 if (modeClock) { 550 showClock(); 551 } 552 else if (modeTimer) { 553 showTimer(); 554 } 555 tm1637ShowDigits(dspVal); 556 557}
Retro Clock/Timer Code
arduino
Controls the Retro clock/timer
1#include <RTClib.h> 2#include <tm1637.h> 3#include <Wire.h> 4 5RTC_DS3231 rtc; 6DateTime NOW; 7bool timedOut; 8bool modeClock; 9bool modeTimer; 10bool modeDateClock; 11bool timerRunning;// use for pausing timer 12 13uint8_t button1 = 11;// Pin Definitions 14uint8_t button2 = 10; 15uint8_t button3 = 9; 16uint8_t led0 = 2; 17uint8_t led1 = 3; 18uint8_t led2 = 4; 19uint8_t led3 = 5; 20uint8_t ldr = A7; 21uint8_t ledPWMPin = 6; 22 23const int AM = 1;// Constants to set indicator LEDs to AM or PM or OFF (led0 = AM, led3=PM) 24const int PM = 5; 25const int NONE = 0; 26 27uint8_t MONTHx = 0;// Field editing flags for setting clock 28uint8_t DAYx = 1; 29uint8_t YEARx = 2; 30uint8_t DAYofWEEKx = 3; 31uint8_t HOURx = 4; 32uint8_t MINUTEx = 5; 33 34long lapStartMillis;// Timer Lap Start Value 35long totalMillis;// Timer Lapsed Time Count 36 37uint8_t brightness; 38bool clockMode_12 = true; 39bool leadingZero = false; 40char dspVal[8];// Char array to store values to be sent to display 41 42void setup() { 43 44 Serial.begin(9600); 45 timedOut = false; 46 if (! rtc.begin()) {// start RTC module communication 47 Serial.println("Couldn't find RTC"); 48 //while (1); 49 } 50 if (rtc.lostPower()) {// This will initialize clock setting after battery change etc. when clock HAS NOT been set 51 Serial.println("RTC needs set!"); 52 // following line sets the RTC to the date & time this sketch was compiled 53 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 54 // This line sets the RTC with an explicit date & time, for example to set 55 // January 21, 2014 at 3am you would call: 56 // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); 57 } 58 59 pinMode(button1, INPUT_PULLUP); 60 pinMode(button2, INPUT_PULLUP); 61 pinMode(button3, INPUT_PULLUP); 62 pinMode(led0, OUTPUT); 63 pinMode(led1, OUTPUT); 64 pinMode(led2, OUTPUT); 65 pinMode(led3, OUTPUT); 66 pinMode(ledPWMPin, OUTPUT); 67 digitalWrite(led0, LOW); 68 digitalWrite(led1, LOW); 69 digitalWrite(led2, LOW); 70 digitalWrite(led3, LOW); 71 72 pinMode(ldr, INPUT); 73 brightness = 3; 74 modeClock = true; 75 NOW = rtc.now(); 76 77 ledSetBrightness(3); 78 ledSequence(1); 79 ledSequence(0); 80 tm1637Init(7, 8); // clock, data pins of LED Display 81 tm1637SetBrightness(brightness); 82}// ******************************************* End of setup 83 84void ledSequence(int cmd) { 85 // display short sequences on the LED column. 86 //Turn all LEDs OFF to start 87 digitalWrite(led0, LOW); 88 digitalWrite(led1, LOW); 89 digitalWrite(led2, LOW); 90 digitalWrite(led3, LOW); 91 if (cmd) { 92 digitalWrite(led0, HIGH); 93 delay(100); 94 digitalWrite(led1, HIGH); 95 delay(100); 96 digitalWrite(led2, HIGH); 97 delay(100); 98 digitalWrite(led3, HIGH); 99 delay(100); 100 digitalWrite(led0, LOW); 101 delay(100); 102 digitalWrite(led1, LOW); 103 delay(100); 104 digitalWrite(led2, LOW); 105 delay(100); 106 digitalWrite(led3, LOW); 107 delay(100); 108 109 } 110 else { 111 digitalWrite(led3, HIGH); 112 delay(100); 113 digitalWrite(led2, HIGH); 114 delay(100); 115 digitalWrite(led1, HIGH); 116 delay(100); 117 digitalWrite(led0, HIGH); 118 delay(100); 119 digitalWrite(led3, LOW); 120 delay(100); 121 digitalWrite(led2, LOW); 122 delay(100); 123 digitalWrite(led1, LOW); 124 delay(100); 125 digitalWrite(led0, LOW); 126 delay(100); 127 } 128 129}// ******************************************* End of ledSequence 130 131long numberGet(uint8_t myField, int startVal, int maxVal) { 132 // Get user input for setting clock/timer/etc. 133 bool gotNumber = false; 134 135 //bool timeOut = millis()+10000; 136 timedOut = false; 137 int debounce = 30; 138 char dspTemp[8]; 139 140 while (!gotNumber) { 141 NOW = rtc.now();// This is only done here to provide flashing digits when setting. 142 switch (myField) { 143 case 0: 144 145 if (NOW.second() & 1) { 146 Serial.println("Setting Month"); 147 sprintf(dspTemp, "%02d ", startVal); 148 } 149 else { 150 sprintf(dspTemp, " : " ); 151 } 152 153 break; 154 case 1://day 155 Serial.println("Setting Day"); 156 if (NOW.second() & 1) { 157 sprintf(dspTemp, " %02d", startVal); 158 } 159 else { 160 sprintf(dspTemp, " : " ); 161 } 162 163 break; 164 case 2://year 165 Serial.println("Setting Year"); 166 if (NOW.second() & 1) { 167 sprintf(dspTemp, "%02d %02d", startVal / 100, startVal % 100); 168 } 169 else { 170 sprintf(dspTemp, " " ); 171 } 172 173 break; 174 175 case 4://hour 176 if (NOW.second() & 1) { 177 sprintf(dspTemp, "%02d ", startVal); 178 } 179 else { 180 sprintf(dspTemp, " : " ); 181 } 182 183 break; 184 185 case 5://minute 186 if (NOW.second() & 1) { 187 sprintf(dspTemp, " %02d", startVal); 188 } 189 else { 190 sprintf(dspTemp, " : " ); 191 } 192 break; 193 } 194 tm1637ShowDigits(dspTemp); 195 196 // Check for button presses here 197 if (digitalRead(button1) == LOW) { 198 delay(debounce); 199 if (digitalRead(button1) == LOW) { 200 201 while (digitalRead(button1) == LOW) { // Lets pause until button released 202 203 } 204 } 205 startVal--; 206 //timeOut = millis() + 10000; 207 delay(300); 208 } 209 if (digitalRead(button2) == LOW) { 210 delay(debounce); 211 if (digitalRead(button2) == LOW) { 212 213 while (digitalRead(button2) == LOW) { // Lets pause until button released 214 215 } 216 } 217 startVal++; 218 //timeOut = millis() + 10000; 219 delay(300); 220 } 221 if (digitalRead(button3) == LOW) { 222 delay(debounce); 223 if (digitalRead(button3) == LOW) { 224 225 while (digitalRead(button3) == LOW) { // Lets pause until button released 226 227 } 228 } 229 gotNumber = true; 230 if (myField < 5) { // DON'T delay if minutes field is being set. 231 delay(300); 232 } 233 234 } 235 } 236 237 if (startVal > maxVal) { 238 startVal = 0; 239 } 240 if (startVal < 0) { 241 startVal = maxVal; 242 } 243 return (startVal); 244}// ******************************************* End of numberGet 245 246 247void clockSet() { 248 bool doneSetting = false; 249 bool settingMonth = true; 250 bool settingDay = false; 251 bool settingWeekDay = false; 252 bool settingYear = false; 253 bool settingHours = false; 254 bool settingMinutes = false; 255 int myMonth, myDay, myYear, myHour, myMinute; 256 257 NOW = rtc.now(); 258 myMonth = NOW.month(); 259 myDay = NOW.day(); 260 myYear = NOW.year(); 261 myHour = NOW.hour(); 262 myMinute = NOW.minute(); 263 Serial.println(myMonth); 264 Serial.println(myDay); 265 Serial.println(myYear); 266 Serial.println(myHour); 267 Serial.println(myMinute); 268 269 while (!doneSetting) { 270 271 while (settingMonth & !timedOut) { // Month Set routine here 272 myMonth = numberGet(MONTHx, myMonth, 12); 273 settingMonth = false; 274 settingDay = true; 275 } 276 while (settingDay & !timedOut) { 277 //Day Set Routine here 278 myDay = numberGet(DAYx, myDay, 31); 279 settingDay = false; 280 settingYear = true; 281 } 282 while (settingYear & !timedOut) { 283 // Year Set Routine here 284 myYear = numberGet(YEARx, myYear, 3000); 285 settingYear = false; 286 settingHours = true; 287 } 288 while (settingHours & !timedOut) { 289 // hour Set Routine here 290 myHour = numberGet(HOURx, myHour, 23); 291 settingHours = false; 292 settingMinutes = true; 293 } 294 while (settingMinutes & !timedOut) { 295 // Minute Set Routine here 296 myMinute = numberGet(MINUTEx, myMinute, 59); 297 settingMinutes = false; 298 doneSetting = true; 299 } 300 301 } 302 if (!timedOut) { 303 rtc.adjust(DateTime(myYear, myMonth, myDay, myHour, myMinute, 0)); 304 } 305 timedOut = false; 306}// ******************************************* End of clockSet 307 308void ledSet(int setting) {// Set the 4 LEDs that display timer hours count/AM/PM 309 static int oldSetting; 310 311 if (oldSetting != setting) { // only update if setting changes 312 //Turn all LEDs OFF to start 313 digitalWrite(led0, LOW); 314 digitalWrite(led1, LOW); 315 digitalWrite(led2, LOW); 316 digitalWrite(led3, LOW); 317 318 if (setting > 4) { // Display PM indication 319 digitalWrite(led3, HIGH); 320 } 321 else { 322 if (setting > 3) { 323 digitalWrite(led3, HIGH); 324 } 325 if (setting > 2) { 326 digitalWrite(led2, HIGH); 327 } 328 if (setting > 1) { 329 digitalWrite(led1, HIGH); 330 } 331 if (setting > 0) { 332 digitalWrite(led0, HIGH); 333 } 334 } 335 oldSetting = setting; 336 } 337}// ******************************************* End of ledSet 338 339void ledSetBrightness(int setting) { 340// Setting multipliers were chosen by trial and error, to best match the display brightness settings. 341// Other LED/resistor/display combinations may require different values. 342 Serial.println( setting); 343 if (setting == 1) { 344 analogWrite(ledPWMPin, setting * 25); 345 } 346 if (setting == 2) { 347 analogWrite(ledPWMPin, setting * 25); 348 } 349 if (setting == 3) { 350 analogWrite(ledPWMPin, setting * 40); 351 } 352 if (setting == 4) { 353 analogWrite(ledPWMPin, setting * 50); 354 } 355 if (setting == 5) { 356 analogWrite(ledPWMPin, setting * 51); 357 } 358 359}// ******************************************* End of ledSetBrightness 360 361void showClock() { 362 363 NOW = rtc.now(); 364 int myHour = NOW.hour(); 365 bool morning = (myHour < 12); // 366 int myMin = NOW.minute(); 367 if (myHour > 12) { 368 369 myHour -= (12 * clockMode_12); //Displays 12 or 24 hour modes 370 } 371 if (NOW.second() & 1) { 372 ledSet(NONE); 373 if ((myHour < 10) && clockMode_12 && !leadingZero) { 374 sprintf(dspVal, " %01d:%02d", myHour, myMin); 375 376 } 377 else { 378 sprintf(dspVal, "%02d:%02d", myHour, myMin); 379 380 } 381 382 } 383 else { 384 if (morning) { 385 ledSet(AM); 386 } 387 else { 388 ledSet(PM); 389 } 390 if ((myHour < 10) && clockMode_12 && !leadingZero) { 391 sprintf(dspVal, " %01d %02d", myHour, myMin); 392 393 } 394 else { 395 sprintf(dspVal, "%02d %02d", myHour, myMin); 396 } 397 } 398 399}// ******************************************* End of showClock 400 401void showTimer() { 402 403 404 long currentMillis = totalMillis; 405 406 if (timerRunning) { 407 currentMillis = totalMillis + (millis() - lapStartMillis); 408 } 409 currentMillis /= 1000; // convert to seconds 410 int tmrHours = (currentMillis / 60) / 60; 411 if (tmrHours > 4) { 412 413 tmrHours -= 4; 414 } 415 int curMins = (currentMillis / 60) % 60; 416 int curSecs = currentMillis % 60; 417 418 sprintf(dspVal, "%02d:%02d", curMins, curSecs); 419 ledSet(tmrHours); 420 421}// ******************************************* End of showTImer 422 423void readButtons() { 424 425 long holdTimer; 426 int debounce = 30; 427 428 if (digitalRead(button3) == LOW) { 429 delay(debounce); 430 if (digitalRead(button3) == LOW) { 431 holdTimer = millis(); 432 while (digitalRead(button3) == LOW) { // Lets pause until button released 433 434 } 435 if (millis() > holdTimer + 5000) { 436 Serial.println ("clock Mode Changed"); 437 clockMode_12 = !clockMode_12; 438 ledSequence(1); 439 ledSequence(0); 440 ledSequence(1); 441 ledSequence(0); 442 } 443 444 else if (millis() > holdTimer + 2300) { 445 ledSequence(1); 446 Serial.println("Setting Clock"); 447 clockSet(); 448 449 } 450 451 else { 452 453 if (modeClock) { 454 modeTimer = true; 455 modeClock = false; 456 } 457 else if (modeTimer) { 458 modeClock = true; 459 modeTimer = false; 460 } 461 else if (modeDateClock) { 462 463 modeClock = true; 464 modeDateClock = false; 465 } 466 } 467 468 delay(500); 469 } 470 } 471 472 473 if (digitalRead(button1) == LOW) { 474 delay(debounce); 475 if (digitalRead(button1) == LOW) { 476 477 holdTimer = millis(); 478 while (digitalRead(button1) == LOW) { // Lets pause until button released 479 480 } 481 if (millis() > holdTimer + 2300) { 482 Serial.println ("Leading Zero Changed"); 483 leadingZero = !leadingZero; 484 ledSequence(0); 485 ledSequence(1); 486 } 487 else { 488 modeTimer = true; 489 modeClock = false; 490 timerRunning = !timerRunning; 491 if (timerRunning) { // Lap has just been started 492 lapStartMillis = millis(); 493 494 } 495 else { // Lap ending, 496 totalMillis += millis() - lapStartMillis; 497 498 } 499 } 500 } 501 } 502 if (digitalRead(button2) == LOW) { 503 delay(debounce); 504 if (digitalRead(button2) == LOW) { 505 holdTimer = millis(); 506 507 while (digitalRead(button2) == LOW) { // Lets pause until button released 508 509 } 510 if (millis() > holdTimer + 6000) { 511 Serial.println("Set Clock"); 512 delay(1000); 513 } 514 515 else if (millis() > holdTimer + 3000) { 516 clockMode_12 = !clockMode_12; 517 } 518 519 else { 520 if (!timerRunning) { 521 totalMillis = 0;// Reset timer to 0 522 sprintf(dspVal, "%02d %02d", 0, 0); 523 tm1637ShowDigits(dspVal); 524 525 } 526 527 } 528 delay(500); 529 } 530 } 531}// ******************************************* End of readButtons 532 533void lightMeter() { // Used to read light level and set brightness of LEDs/Display 534 static int oldBrt; 535 int myBrt = 6 - (analogRead(ldr) / 150 - 1);// Read value from Light Dependent Resistor, 536 // convert to value from 1 to 5 537 if (oldBrt != myBrt) {// Only adjsut if light reading changes 538 ledSetBrightness(myBrt); 539 tm1637SetBrightness(myBrt); 540 oldBrt = myBrt; 541 } 542}// ******************************************* End of lightMeter 543 544void loop() { 545 546 readButtons();// Check to see if any buttons are pressed 547 lightMeter();// check/adjust display brightness if needed 548 549 if (modeClock) { 550 showClock(); 551 } 552 else if (modeTimer) { 553 showTimer(); 554 } 555 tm1637ShowDigits(dspVal); 556 557}
Downloadable files
Fritzing Schematic-RetroClock
Fritzing Schematic-RetroClock
Fritzing Schematic-RetroClock
Fritzing Schematic-RetroClock
Retro LED Clock/Timer Schematic-Updated
Schematic for Retro Clock
Retro LED Clock/Timer Schematic-Updated
Comments
Only logged in users can leave comments
randtekk
0 Followers
•0 Projects
Table of contents
Intro
11
0