Hackster is hosting Impact Spotlights: Smart Home. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Smart Home. Stream on Thursday!
Arnov Sharma
Published © MIT

Date Counter

A 10x10 LED matrix displays the current day of the month with a color transition from red to green as the days progress.

BeginnerFull instructions provided172
Date Counter

Things used in this project

Hardware components

Raspberry Pi Pico W
Raspberry Pi Pico W
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

SCH

Code

code

C/C++
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <time.h>

// WiFi credentials
const char* ssid = "UR SSID";
const char* password = "UR PASS";

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800, 60000); // IST is UTC+5:30 or 19800 seconds

// Define LED matrix
#define MATRIX_PIN 12 // GPIO pin connected to the LED matrix data pin
#define MATRIX_WIDTH 10
#define MATRIX_HEIGHT 10
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(MATRIX_WIDTH, MATRIX_HEIGHT, MATRIX_PIN,
  NEO_MATRIX_TOP + NEO_MATRIX_LEFT +
  NEO_MATRIX_ROWS,
  NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(115200);
  Serial.println("Setting up WiFi and NTP...");

  // Initialize matrix
  matrix.begin();
  matrix.setTextWrap(false);
  matrix.setBrightness(50);
  matrix.setRotation(3); // Rotate the text 90 degrees in the opposite direction

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected.");

  // Initialize NTPClient
  timeClient.begin();
}

void loop() {
  timeClient.update();

  // Get current date and time from NTP client
  time_t rawTime = timeClient.getEpochTime();
  struct tm *timeInfo = localtime(&rawTime);

  // Extract the current day of the month
  int currentDay = timeInfo->tm_mday;
  String dayStr = String(currentDay); // Convert day to string

  // Calculate the color based on the day of the month
  int r = 255 - ((currentDay - 1) * 255 / 30); // Red decreases
  int g = (currentDay - 1) * 255 / 30; // Green increases
  int color = matrix.Color(r, g, 0);

  // Display the day number on the LED matrix with the calculated color
  matrix.fillScreen(0); // Clear screen
  matrix.setTextSize(1); // Set text size to fit within 10x10 matrix
  matrix.setTextColor(color);

  if (dayStr.length() < 2) {
    matrix.setCursor((matrix.width() - 6) / 2, (matrix.height() - 8) / 2);
  } else {
    matrix.setCursor((matrix.width() - 12) / 2, (matrix.height() - 8) / 2);
  }

  matrix.print(dayStr);
  matrix.show();

  delay(1000); // Update every second
}

Credits

Arnov Sharma
328 projects • 332 followers
Just your average MAKER
Contact

Comments

Please log in or sign up to comment.