Components and supplies
Youtube video
Apps and platforms
Arduino IDE 2.0 (beta)
Project description
Code
final code
cpp
code for the project
1#include <Adafruit_NeoPixel.h> 2#include <RtcDS1302.h> 3 4ThreeWire myWire(3, 2, 4); // IO, SCLK, CE 5RtcDS1302<ThreeWire> Rtc(myWire); 6 7int hue = 150; // Variable to track the hue 8int hue2 = 42; // Variable to track the hue 9 10int changeTime = 0; 11 12int bx, by = 0; 13 14int h1, h2 = 0; // h1= hour ones, h2= hour tens (digits) 15int m1, m2 = 0; // m1= minutes ones, m2=minutes tens 16int s = 0; // seconds 17int dots = 0; 18int year, month, day, hours, minutes, seconds = 0; 19int shift = 0; 20int mode = 0; 21 22int brightness = 5; 23// Define shift values for each digit type 24int shifts[] = { 0, 22, 55, 77 }; 25int col = 0; 26int row = 0; 27 28const int numButtons = 19; 29const int numRows = 9; // Total number of row pins 30const int numBacklights = 19; // Total number of row pins 31int expectedADCValues[numButtons] = { 1020, 925, 765, 612, 475, 361, 265, 190, 133, 92 }; 32int threshold = 10; // Adjusted threshold for better accuracy (test and adjust further) 33int rowPins[numRows] = { 5, 6, 7, 8, 9, 10, 11, 12, 13 }; 34int backLights[numBacklights] = { 5, 10, 16, 21, 27, 32, 38, 43, 49, 54, 60, 65, 71, 76, 82, 87, 93, 98, 104 }; 35 36 37 38 39int displayLeds[9][10] = { 40 { 0, 11, 22, 33, 44, 55, 66, 77, 88, 99 }, 41 { 9, 20, 31, 42, 53, 64, 75, 86, 97, 0 }, 42 { 1, 12, 23, 34, 45, 56, 67, 78, 89, 100 }, 43 { 8, 19, 30, 41, 52, 63, 74, 85, 96, 0 }, 44 { 2, 13, 24, 35, 46, 57, 68, 79, 90, 101 }, 45 { 7, 18, 29, 40, 51, 62, 73, 84, 95, 0 }, 46 { 3, 14, 25, 36, 47, 58, 69, 80, 91, 102 }, 47 { 6, 17, 28, 39, 50, 61, 72, 83, 94, 0 }, 48 { 4, 15, 26, 37, 48, 59, 70, 81, 92, 103 } 49}; 50 51int litLeds[54] = {}; 52 53 54 55int litNumbers[4]; 56 57 58int digit0[] = { 9, 8, 7, 6, 11, 15, 20, 19, 18, 17 }; 59int digit1[] = { 20, 19, 18, 17 }; 60int digit2[] = { 9, 7, 6, 11, 13, 14, 20, 19, 17 }; 61int digit3[] = { 9, 18, 6, 11, 13, 15, 20, 19, 17 }; 62int digit4[] = { 9, 8, 13, 20, 19, 18, 17 }; 63int digit5[] = { 9, 8, 7, 11, 13, 15, 18, 17 }; 64int digit6[] = { 9, 8, 7, 6, 11, 13, 15, 20, 18, 17 }; 65int digit7[] = { 9, 11, 20, 19, 18, 17 }; 66int digit8[] = { 9, 8, 7, 6, 11, 13, 15, 20, 19, 18, 17 }; //13*4=52 67int digit9[] = { 9, 8, 11, 13, 20, 19, 18, 17 }; 68 69 70//determine sizes of the arrays 71int sizeZero = sizeof(digit0) / sizeof(digit0[0]); 72int sizeOne = sizeof(digit1) / sizeof(digit1[0]); 73int sizeTwo = sizeof(digit2) / sizeof(digit2[0]); 74int sizeThree = sizeof(digit3) / sizeof(digit3[0]); 75int sizeFour = sizeof(digit4) / sizeof(digit4[0]); 76int sizeFive = sizeof(digit5) / sizeof(digit5[0]); 77int sizeSix = sizeof(digit6) / sizeof(digit6[0]); 78int sizeSeven = sizeof(digit7) / sizeof(digit7[0]); 79int sizeEight = sizeof(digit8) / sizeof(digit8[0]); 80int sizeNine = sizeof(digit9) / sizeof(digit9[0]); 81 82//stores all the digit sizes into a single array 0 for sizeZero etc. 83int sizes[] = { sizeZero, sizeOne, sizeTwo, sizeThree, sizeFour, sizeFive, sizeSix, sizeSeven, sizeEight, sizeNine }; 84 85 86 87 88 89 90 91#define LED_PIN A1 92#define LED_COUNT 105 93 94Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); 95 96// Array to store the color state for each button 97int colorState[numButtons] = { 0 }; // 0: Red, 1: Green, 2: Blue, 3: White, 4: Off 98 99 100void updateTime() { 101 // Create a new RtcDateTime object with updated minutes 102 RtcDateTime updatedTime( 103 year, month, day, 104 hours, minutes, seconds); 105 106 // Set the updated time back to the RTC 107 Rtc.SetIsWriteProtected(false); 108 Rtc.SetDateTime(updatedTime); 109 delay(200); 110 Rtc.SetIsWriteProtected(true); 111} 112 113 114 115void clearDisplay() { 116 for (int i = 0; i < LED_COUNT; i++) { 117 strip.setPixelColor(i, strip.Color(0, 0, 0)); // Off 118 } 119 //strip.show(); 120} 121 122 123 124void fillColor(uint8_t hue) { 125 126 127 //uint32_t color = strip.ColorHSV(hue * 256); // Using the built-in ColorHSV function 128 129 for (int i = 0; i < LED_COUNT; i++) { 130 strip.setPixelColor(i, strip.ColorHSV(hue * 256)); // Set each LED color 131 } 132 // strip.show(); // Update the LED strip 133} 134 135void redBee() { 136 strip.setPixelColor(bx, strip.Color(255, 0, 0)); // Set the pixel color 137 bx++; 138 if (bx > 105) { 139 bx = 0; 140 } 141} 142//void fillColor(uint8_t red, uint8_t green, uint8_t blue) { 143// for (int i = 0; i < LED_COUNT; i++) { 144// strip.setPixelColor(i, strip.Color(red, green, blue)); // Set the pixel color 145//} 146//strip.show(); // Update the LED strip 147//} 148 149 150 151void zero() { 152 for (int i = 0; i < sizeZero; i++) { 153 strip.setPixelColor(digit0[i] + shift, strip.ColorHSV(hue2 * 256)); 154 155 // int shifts[] = {0, 22, 55, 77}; 156 if (shift == 0) litLeds[i] = digit0[i] + shift; 157 if (shift == 22) litLeds[i + 12] = digit0[i] + shift; 158 if (shift == 55) litLeds[i + 24] = digit0[i] + shift; 159 if (shift == 77) litLeds[i + 36] = digit0[i] + shift; 160 } 161} 162 163void one() { 164 for (int i = 0; i < sizeOne; i++) { 165 strip.setPixelColor(digit1[i] + shift, strip.ColorHSV(hue2 * 256)); 166 if (shift == 0) litLeds[i] = digit1[i] + shift; 167 if (shift == 22) litLeds[i + 12] = digit1[i] + shift; 168 if (shift == 55) litLeds[i + 24] = digit1[i] + shift; 169 if (shift == 77) litLeds[i + 36] = digit1[i] + shift; 170 } 171} 172 173void two() { 174 for (int i = 0; i < sizeTwo; i++) { 175 strip.setPixelColor(digit2[i] + shift, strip.ColorHSV(hue2 * 256)); 176 if (shift == 0) litLeds[i] = digit2[i] + shift; 177 if (shift == 22) litLeds[i + 12] = digit2[i] + shift; 178 if (shift == 55) litLeds[i + 24] = digit2[i] + shift; 179 if (shift == 77) litLeds[i + 36] = digit2[i] + shift; 180 } 181} 182 183void three() { 184 for (int i = 0; i < sizeThree; i++) { 185 strip.setPixelColor(digit3[i] + shift, strip.ColorHSV(hue2 * 256)); 186 if (shift == 0) litLeds[i] = digit3[i] + shift; 187 if (shift == 22) litLeds[i + 12] = digit3[i] + shift; 188 if (shift == 55) litLeds[i + 24] = digit3[i] + shift; 189 if (shift == 77) litLeds[i + 36] = digit3[i] + shift; 190 } 191} 192 193void four() { 194 for (int i = 0; i < sizeFour; i++) { 195 strip.setPixelColor(digit4[i] + shift, strip.ColorHSV(hue2 * 256)); 196 if (shift == 0) litLeds[i] = digit4[i] + shift; 197 if (shift == 22) litLeds[i + 12] = digit4[i] + shift; 198 if (shift == 55) litLeds[i + 24] = digit4[i] + shift; 199 if (shift == 77) litLeds[i + 36] = digit4[i] + shift; 200 } 201} 202 203void five() { 204 for (int i = 0; i < sizeFive; i++) { 205 strip.setPixelColor(digit5[i] + shift, strip.ColorHSV(hue2 * 256)); 206 if (shift == 0) litLeds[i] = digit5[i] + shift; 207 if (shift == 22) litLeds[i + 12] = digit5[i] + shift; 208 if (shift == 55) litLeds[i + 24] = digit5[i] + shift; 209 if (shift == 77) litLeds[i + 36] = digit5[i] + shift; 210 } 211} 212 213void six() { 214 for (int i = 0; i < sizeSix; i++) { 215 strip.setPixelColor(digit6[i] + shift, strip.ColorHSV(hue2 * 256)); 216 if (shift == 0) litLeds[i] = digit6[i] + shift; 217 if (shift == 22) litLeds[i + 12] = digit6[i] + shift; 218 if (shift == 55) litLeds[i + 24] = digit6[i] + shift; 219 if (shift == 77) litLeds[i + 36] = digit6[i] + shift; 220 } 221} 222 223void seven() { 224 for (int i = 0; i < sizeSeven; i++) { 225 strip.setPixelColor(digit7[i] + shift, strip.ColorHSV(hue2 * 256)); 226 if (shift == 0) litLeds[i] = digit7[i] + shift; 227 if (shift == 22) litLeds[i + 12] = digit7[i] + shift; 228 if (shift == 55) litLeds[i + 24] = digit7[i] + shift; 229 if (shift == 77) litLeds[i + 36] = digit7[i] + shift; 230 } 231} 232 233void eight() { 234 for (int i = 0; i < sizeEight; i++) { 235 strip.setPixelColor(digit8[i] + shift, strip.ColorHSV(hue2 * 256)); 236 if (shift == 0) litLeds[i] = digit8[i] + shift; 237 if (shift == 22) litLeds[i + 12] = digit8[i] + shift; 238 if (shift == 55) litLeds[i + 24] = digit8[i] + shift; 239 if (shift == 77) litLeds[i + 36] = digit8[i] + shift; 240 } 241} 242 243void nine() { 244 for (int i = 0; i < sizeNine; i++) { 245 strip.setPixelColor(digit9[i] + shift, strip.ColorHSV(hue2 * 256)); 246 if (shift == 0) litLeds[i] = digit9[i] + shift; 247 if (shift == 22) litLeds[i + 12] = digit9[i] + shift; 248 if (shift == 55) litLeds[i + 24] = digit9[i] + shift; 249 if (shift == 77) litLeds[i + 36] = digit9[i] + shift; 250 } 251} 252 253 254 255 256 257 258 259 260// Declare an array of function pointers to hold the functions 261void (*functionArray[10])(); // Array to hold 10 function pointers 262 263 264 265void displayTime() { 266 267 268if (hue!=157) fillColor(hue); // Update the LED color based on the current hue 269if (hue==157) clearDisplay(); 270 271 272 273 ///fillColor(0,100,0); 274 //randomHex(); 275 276 277 if (seconds % 2 == 0 && changeTime != 1) { 278 strip.setPixelColor(52, strip.ColorHSV(hue2 * 256)); // Red 279 strip.setPixelColor(50, strip.ColorHSV(hue2 * 256)); // Red 280 //redBee(); 281 } 282 283 if (changeTime == 1) { 284 strip.setPixelColor(displayLeds[3][4], strip.Color(255, 0, 0)); // Red 285 strip.setPixelColor(displayLeds[7][4], strip.Color(255, 0, 0)); // Red 286 } 287 //strip.show(); 288 289 shift = 0; // Set shift to 0 once since it's always 0 290 if (h2 >= 0 && h2 <= 9) functionArray[h2](); 291 shift = 22; // Set shift to 0 once since it's always 0 292 if (h1 >= 0 && h1 <= 9) functionArray[h1](); 293 shift = 55; // Set shift to 0 once since it's always 0 294 if (m2 >= 0 && m2 <= 9) functionArray[m2](); 295 shift = 77; // Set shift to 0 once since it's always 0 296 if (m1 >= 0 && m1 <= 9) functionArray[m1](); 297 strip.show(); 298} 299 300 301void setup() { 302 Serial.begin(9600); 303 304 functionArray[0] = zero; 305 functionArray[1] = one; 306 functionArray[2] = two; 307 functionArray[3] = three; 308 functionArray[4] = four; 309 functionArray[5] = five; 310 functionArray[6] = six; 311 functionArray[7] = seven; 312 functionArray[8] = eight; 313 functionArray[9] = nine; 314 315 for (int i = 0; i < 52; i++) { // 13 * 4 = 52 316 litLeds[i] = -1; 317 } 318 319 320 321 randomSeed(analogRead(2)); 322 323 Serial.print("compiled: "); 324 Serial.print(__DATE__); 325 Serial.println(__TIME__); 326 327 Rtc.Begin(); 328 329 RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); 330 printDateTime(compiled); 331 Serial.println(); 332 333 if (!Rtc.IsDateTimeValid()) { 334 // Common Causes: 335 // 1) first time you ran and the device wasn't running yet 336 // 2) the battery on the device is low or even missing 337 338 Serial.println("RTC lost confidence in the DateTime!"); 339 Rtc.SetDateTime(compiled); 340 } 341 342 if (Rtc.GetIsWriteProtected()) { 343 Serial.println("RTC was write protected, enabling writing now"); 344 Rtc.SetIsWriteProtected(false); 345 } 346 347 if (!Rtc.GetIsRunning()) { 348 Serial.println("RTC was not actively running, starting now"); 349 Rtc.SetIsRunning(true); 350 } 351 352 RtcDateTime now = Rtc.GetDateTime(); 353 if (now < compiled) { 354 Serial.println("RTC is older than compile time! (Updating DateTime)"); 355 Rtc.SetDateTime(compiled); 356 } else if (now > compiled) { 357 Serial.println("RTC is newer than compile time. (this is expected)"); 358 } else if (now == compiled) { 359 Serial.println("RTC is the same as compile time! (not expected but all is fine)"); 360 } 361 362 363 364 for (int i = 0; i < numRows; i++) { 365 pinMode(rowPins[i], OUTPUT); 366 digitalWrite(rowPins[i], LOW); // Ensure rows are initially LOW 367 } 368 369 strip.begin(); // Initialize NeoPixel strip object 370 clearDisplay(); 371 delay(1000); 372 strip.setBrightness(brightness); // Set maximum brightness 373} 374 375void loop() { 376 377 RtcDateTime now = Rtc.GetDateTime(); 378 379 m1 = minutes % 10; 380 m2 = minutes / 10; 381 h1 = hours % 10; 382 h2 = hours / 10; 383 s = seconds % 10; 384 printDateTime(now); 385 Serial.println(); 386 387 388 //strip.setBrightness(brightness); 389 390 391 //fillColor(0,10,100); 392 //clearDisplay(); 393 394 395 if (mode == 0) { 396 397 displayTime(); 398 } 399 400 401 402 403 //Check the buttons pressed 404 for (int i = 0; i < numRows; i++) { 405 // Activate the current row 406 row = i; 407 digitalWrite(rowPins[row], HIGH); 408 409 // Read the ADC value 410 int sensorValue = analogRead(A0); 411 412 // Check which button is pressed 413 int buttonPressed = getButtonPressed(sensorValue); 414 415 if (buttonPressed != -1) { 416 col = buttonPressed; 417 418 419 // Cycle through colors for the button pressed 420 cycleColor(buttonPressed); 421 // if (mode == 1&&col!=0&&row!=i,&&row!=i) delay(200); // if drawing mode 422 423 if (row == 0 && col == 0 && brightness < 255) { 424 brightness += 5; 425 strip.setBrightness(brightness); 426 } 427 428 if (row == 2 && col == 0 && brightness > 5) { 429 brightness -= 5; 430 strip.setBrightness(brightness); 431 } 432 433 434 // Simple to just check a button and do something 435// change the background hue 436 if (row == 0 && col == 9) { 437 hue=hue+3; // Increment the hue to cycle through colors 438 if (hue >= 256) hue = 0; // Reset hue when it reaches 255 439 } 440 if (row==2&&col==9) 441 { 442 hue=157; 443 } 444 445 // if (row==2&&col==9) 446 //{ 447 // hue2++; // Increment the hue to cycle through colors 448 ///if (hue2 >= 256) hue2 = 0; // Reset hue when it reaches 255 449 //} 450 451 452 453 //change time if either the two center dots are pressed and not drawing mode 454 if (((row == 3 && col == 4) || (row == 7 && col == 4)) 455 && mode != 1) { 456 457 changeTime = !changeTime; 458 displayTime(); 459 460 //if (changeTime==1) { 461 mode = 2; //change time mode 462 //} 463 464 if (changeTime == 0) { 465 466 mode = 0; 467 } 468 469 470 delay(200); 471 } 472 473 // 474 //change the time. 475 if (changeTime == 1) { 476 477 478 // change h2 digit if any of the three top button are pressed over that digit. 479 480 481 482 if (col == 8) { 483 m1 += 1; 484 if (m1 == 10) { 485 m1 = 0; 486 } 487 minutes = m2 * 10 + m1; 488 updateTime(); 489 } 490 491 if (col == 6) { 492 m2 += 1; 493 if (m2 == 6) { 494 m2 = 0; 495 } 496 minutes = m2 * 10 + m1; 497 updateTime(); 498 } 499 500 501 if (col == 3) { 502 h1 += 1; 503 if (h1 == 10 || h1 > 3 && h2 == 2) { 504 if (h2 != 0) h1 = 0; 505 if (h2 == 0) h1 = 1; 506 } 507 hours = h2 * 10 + h1; 508 updateTime(); 509 } 510 if (col == 1) { 511 h2 += 1; 512 if (h2 == 3) { 513 if (h1 != 0) h2 = 0; 514 if (h1 == 0) h2 = 1; 515 } 516 hours = h2 * 10 + h1; 517 updateTime(); 518 } 519 520 //update the display to show the red dots 521 displayTime(); 522 } 523 524 if (changeTime == 0) { 525 526 // test to see if button coresponds to a lit led from the digits. 527 528 if(mode!=1) 529 { 530 for (int c = 0; c < 13 * 4; c++) { 531 532 if (litLeds[c] == displayLeds[row][col]) /// test to see if the button pressesed led matched the litLEDS array 533 { 534 hue2 = hue2 + 3; // Increment the hue to cycle through colors 535 if (hue2 >= 256) hue2 = 0; // Reset hue when it reaches 255 536 break; 537 } 538 } 539 } 540 //drawing mode 541 542 543 544 545 if (row == 8 && col == 0) { 546 mode++; 547 fillColor(hue); // Increment the hue to cycle through colors 548 if (mode == 1) strip.setPixelColor(displayLeds[row][col], strip.Color(255, 0, 0)); // Red 549 550 strip.show(); 551 delay(200); 552 if (mode >= 2) mode = 0; // Reset hue when it reaches 255 553 } 554 555 556 557 558 559 if (mode == 1) // coloring mode 560 { 561 562 if (row == 4 && col == 0) { 563 clearDisplay(); 564 } 565 if (row == 6 && col == 0) 566 fillColor(hue); 567 strip.show(); 568 //delay(200); 569 } 570 } 571 572 573 574 575 // Print which button is pressed 576 Serial.print("Button ["); 577 Serial.print(row); 578 Serial.print(","); 579 Serial.print(col); 580 Serial.print("]: "); 581 582 583 584 Serial.print(sensorValue); 585 Serial.print(","); 586 Serial.print(h2); 587 Serial.print(h1); 588 Serial.print(m2); 589 Serial.print(m1); 590 591 // for (int i = 0; i < 13 * 4; i++) { 592 // Serial.print(litLeds[i]); // Print the value 593 // Serial.print(","); 594 // } 595 Serial.println(""); 596 597 //Serial.println(displayLeds[row][col]); 598 } 599 600 // Deactivate the row 601 digitalWrite(rowPins[row], LOW); 602 } 603} 604 605int getButtonPressed(int adcValue) { 606 for (int i = 0; i < numButtons; i++) { 607 if ((adcValue >= (expectedADCValues[i] - threshold) && adcValue <= (expectedADCValues[i] + threshold)) && adcValue > threshold) { 608 return i; 609 } 610 } 611 return -1; 612} 613 614 615 616 617 618 619 620 621void randomHex() { 622 //strip.setPixelColor(displayLeds[random(9)][random(10)], strip.Color(random(200, 240), random(200, 240), random(0, 10))); // yellow 623 strip.setPixelColor(displayLeds[random(9)][random(10)], strip.Color(random(0, 255), random(0, 255), random(0, 255))); // yellow 624 //strip.show(); 625 //delay(200); 626} 627 628void cycleColor(int buttonIndex) { 629 colorState[buttonIndex] = (colorState[buttonIndex] + 1) % 5; // Cycle through 0 to 4 630 631 // Set the color based on the current state 632 switch (colorState[buttonIndex]) { 633 case 0: 634 strip.setPixelColor(displayLeds[row][col], strip.Color(255, 0, 0)); // Red 635 break; 636 case 1: 637 strip.setPixelColor(displayLeds[row][col], strip.Color(0, 255, 0)); // Green 638 break; 639 case 2: 640 strip.setPixelColor(displayLeds[row][col], strip.Color(238, 210, 2)); // yellow 641 break; 642 case 3: 643 strip.setPixelColor(displayLeds[row][col], strip.Color(255, 255, 255)); // White 644 break; 645 case 4: 646 strip.setPixelColor(displayLeds[row][col], strip.Color(0, 0, 0)); // Off 647 break; 648 } 649 650 strip.show(); 651} 652 653 654#define countof(a) (sizeof(a) / sizeof(a[0])) 655 656void printDateTime(const RtcDateTime& dt) { 657 char datestring[26]; 658 // uint8_t hours = dt.Hour(); 659 660 month = dt.Month(); 661 year = dt.Year(); 662 day = dt.Day(); 663 hours = dt.Hour(); 664 minutes = dt.Minute(); 665 seconds = dt.Second(); 666 667 snprintf_P(datestring, 668 countof(datestring), 669 PSTR("%02u/%02u/%04u %02u:%02u:%02u"), 670 dt.Month(), 671 dt.Day(), 672 dt.Year(), 673 dt.Hour(), 674 dt.Minute(), 675 dt.Second()); 676 Serial.print(datestring); 677}
Comments
Only logged in users can leave comments
newsonator
9 Followers
•3 Projects
1
0