CPP   59
code
By Petre on 6th February 2023 11:15:04 PM


  1. #include <Adafruit_GFX.h>
  2. #include "Font4x7Fixed.h"
  3. #include <Adafruit_NeoMatrix.h>
  4. #include <Adafruit_NeoPixel.h>
  5. #include <ESP8266WiFi.h>
  6. #include <time.h>
  7.  
  8. #define PIN  3
  9. #define BRIGHTNESS 10
  10.  
  11. const char *ssid = "cammar";
  12. const char *password = "shadow10121976";
  13. const char* ntpServer = "ro.pool.ntp.org";
  14. const char* TZ_INFO    = "EET-2EEST,M3.5.0/3,M10.5.0/4";
  15. struct tm timeinfo;
  16. time_t now;
  17. long unsigned lastNTPtime;
  18. unsigned long lastEntryTime;
  19. unsigned long previousMillis = 0;
  20.  
  21. String dash = String(':');
  22. String minus = String('-');
  23. String space = String(' ');
  24.  
  25. Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(32, 8, PIN,
  26.                             NEO_MATRIX_TOP     + NEO_MATRIX_LEFT +
  27.                             NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
  28.                             NEO_GRB            + NEO_KHZ800);
  29.  
  30. const uint16_t colors[] = {
  31.   matrix.Color(128, 0, 0), matrix.Color(0, 128, 0), matrix.Color(0, 0, 128),
  32.   matrix.Color(128, 0, 128), matrix.Color(0, 128, 128), matrix.Color(128, 128, 0)
  33. };
  34.  
  35. int pass = 0;
  36. int x = matrix.width();
  37.  
  38. char daysOfTheWeek[7][12] = {"Duminica", "Luni", "Marti", "Miercuri", "Joi", "Vineri", "Sambata"};
  39.  
  40. bool getNTPtime(unsigned long sec) {
  41.   {
  42.     unsigned long start1 = millis();
  43.     do
  44.     {
  45.       time(&now);
  46.       localtime_r(&now, &timeinfo);
  47.       delay(10);
  48.     } while (((millis() - start1) <= (1000 * sec)) && (timeinfo.tm_year < (2023 - 1900)));
  49.  
  50.     if (timeinfo.tm_year <= (2023 - 1900))
  51.       return false;
  52.   }
  53.   return true;
  54. }
  55.  
  56.  
  57. void setup() {
  58.   Serial.begin(9600);
  59.   WiFi.begin(ssid, password);
  60.   while (WiFi.status() != WL_CONNECTED) {
  61.     delay(500);
  62.   }
  63.   configTime(0, 0, ntpServer);
  64.   setenv("TZ", TZ_INFO, 1);
  65.   getNTPtime(10);
  66.   showTime(&timeinfo);
  67.   lastNTPtime = time(&now);
  68.   lastEntryTime = millis();
  69.   matrix.begin();
  70.   matrix.setFont(&Font4x7Fixed);
  71.   matrix.setTextSize(1);
  72.   matrix.setTextWrap(false);
  73.   matrix.setBrightness(BRIGHTNESS);
  74.   matrix.setTextColor(colors[0]);
  75. }
  76.  
  77. void loop() {
  78.   showTime(&timeinfo);
  79.   getNTPtime(10);
  80. }
  81.  
  82. bool doneOnce=false;
  83. void showTime(tm *localTime) {
  84. String timeHour = String(localTime->tm_hour);
  85. Serial.print("timeHour = "); Serial.println(String(timeHour));
  86.  
  87. String timeMinute = String(localTime->tm_min);
  88. Serial.print("timeMinute = "); Serial.println(String(timeMinute));
  89.  
  90. String timeSeconds = String(localTime->tm_sec);
  91. Serial.print("timeSeconds = "); Serial.println(String(timeSeconds));
  92.  
  93. String timeWeekDay = String(daysOfTheWeek[localTime->tm_wday]);
  94. Serial.print("timeWeekDay = "); Serial.println(String(timeWeekDay));
  95.  
  96. String timeMonth = String(localTime->tm_mon + 1);
  97. Serial.print("timeMonth = "); Serial.println(String(timeMonth));
  98.  
  99. String timeDay = String(localTime->tm_mday);
  100. Serial.print("timeDay = "); Serial.println(String(timeDay));
  101.  
  102. String timeYear = String(localTime->tm_year - 100);
  103. Serial.print("timeYear = "); Serial.println(String(timeYear));
  104.  
  105.  
  106.   String timeShow = String(timeHour) + String(space) + String(dash) + String(space) + String(timeMinute) + String(space) + String(dash) + String(space) + String(timeSeconds);
  107.   String dateShow = String(timeWeekDay) + String(space) + String(space) + String(timeDay) + String(space) + String(minus) + String(space) + String(timeMonth) + String(space) + String(minus) + String(space) + String(timeYear);
  108.   Serial.print("timeShow = "); Serial.println(timeShow);
  109.   Serial.print("dateShow = "); Serial.println(dateShow);
  110.  
  111.   const long interval = 30000;
  112.   const long interval2 = 50000;
  113.   unsigned long currentMillis = millis();
  114.   if (currentMillis - previousMillis >= interval) {
  115.     if(doneOnce==false){x=0; doneOnce=true;}
  116.     if (currentMillis - previousMillis >= interval2) {
  117.       previousMillis = currentMillis;
  118.     }
  119.     matrix.fillScreen(0);
  120.     matrix.setCursor(x, 0);
  121.     matrix.print(dateShow);
  122.     if (--x < -40) {
  123.       matrix.setTextColor(matrix.Color(128, 0, 0));
  124.     }
  125.     matrix.show();
  126.     delay(150);
  127.   } else {
  128.     doneOnce=false;
  129.     matrix.clear();
  130.     matrix.setCursor(0, 0);
  131.     matrix.print(timeShow);
  132.     pass++;
  133.     if (pass >= 6) pass = 0;
  134.     matrix.setTextColor(colors[pass]);
  135.     matrix.show();
  136.     delay(1000);
  137.   }
  138. }

Raw Paste

Login or Register to edit or fork this paste. It's free.