Advertisement
tuldok89

LED Clock for RP2040

Jun 11th, 2025
1,253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | Source Code | 0 0
  1. #include <SPI.h>
  2. #include <MD_Parola.h>
  3. #include <MD_MAX72xx.h>
  4. #include <Arduino.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #include <MBED_RPi_Pico_TimerInterrupt.h>
  8. #include <NTPClient.h>
  9. #include <WiFiEspAT.h>
  10. #include <WiFiUdp.h>
  11. #include <sys/time.h>
  12. #include "main.h"
  13.  
  14. #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
  15. #define MAX_DEVICES 8
  16. #define CS1 27
  17. #define CS2 26
  18. #define SEC2PS(x) ((x) * 1000000)
  19. #define SEC2MS(x) ((x) * 1000)
  20.  
  21. const char ssid[] = "Tuldok-House";
  22. const char pass[] = "HelloWorld";
  23. MD_Parola* line1;
  24. MD_Parola* line2;
  25. MBED_RPI_PICO_Timer clockTimer(0);
  26. MBED_RPI_PICO_Timer updateTimer(1);
  27. WiFiUDP ntpUdp;
  28. NTPClient timeClient(ntpUdp, "time.google.com", 0, SEC2MS(64));
  29.  
  30.  
  31. void setup() {
  32.   Serial.begin(115200); // USB UART
  33.   Serial1.begin(115200); // Serial line to the ESP-01 WiFi module
  34.  
  35.   initWiFi();
  36.   initSpi();
  37.   initDisplay();
  38.   initTime();
  39.   initTimer();
  40. }
  41.  
  42. void loop() {
  43.   timeClient.update();
  44.   sleep_ms(500);
  45. }
  46.  
  47. void clockTick(uint alarmNum)
  48. {
  49.   time_t now;
  50.   char buf[16];
  51.   time(&now);
  52.   auto tm = localtime(&now);
  53.   strftime(buf, 16, "%m/%d/%Y", tm);
  54.   line1->setTextAlignment(PA_CENTER);
  55.   line1->print(buf);
  56.   strftime(buf, 16, "%I:%M:%S %P", tm);
  57.   line2->setTextAlignment(PA_CENTER);
  58.   line2->print(buf);
  59. }
  60.  
  61. void syncRtcTick(uint alarmNum)
  62. {
  63.   timeval tv {.tv_sec = timeClient.getEpochTime()};
  64.   settimeofday(&tv, NULL);
  65. }
  66.  
  67. void initWiFi()
  68. {
  69.   WiFi.init(Serial1);
  70.   WiFi.begin(ssid, pass);
  71.  
  72.   while (WiFi.status() != WL_CONNECTED)
  73.   {
  74.     Serial.print(".");
  75.     delay(500);
  76.   }
  77.  
  78.   Serial.println("WiFi connected.");
  79. }
  80.  
  81. void initDisplay()
  82. {
  83.   line1 = new MD_Parola(HARDWARE_TYPE, SPI, CS1, 8);
  84.   line2 = new MD_Parola(HARDWARE_TYPE, SPI, CS2, 8);
  85.   line1->begin();
  86.   line2->begin();
  87.   line1->setIntensity(15);
  88.   line2->setIntensity(15);
  89.   line1->displayClear();
  90.   line2->displayClear();
  91. }
  92.  
  93. void initTimer()
  94. {
  95.   // clock tick interrupt
  96.   clockTimer.attachInterruptInterval(SEC2PS(1), clockTick);
  97.   // rtc sync interrupt
  98.   updateTimer.attachInterrupt(SEC2PS(5*60), syncRtcTick);
  99. }
  100.  
  101. void initTime()
  102. {
  103.   setenv("TZ", "Asia/Manila", 1);
  104.   tzset();
  105.   timeClient.update();
  106.   timeval tv {.tv_sec = timeClient.getEpochTime()};
  107.   settimeofday(&tv, NULL);
  108. }
  109.  
  110. void initSpi()
  111. {
  112.   SPI.begin();
  113.   pinMode(CS1, OUTPUT);
  114.   pinMode(CS2, OUTPUT);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement