i have make a clock using one P10 display , but i made this clock using DMD library, which is not supported brightness control, later i found DMD2 which is supported brightness control, but now i cannot modify my code as DMD2 use different parameters, i am not expert in coding , can anybody help me? this is my original code using DMD library.
#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <DMD.h>
#include <TimerOne.h>
//#include "Arial_black_16.h"
#include "Font.h"
#define DISPLAYS_ACROSS 1
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);
RTC_DS1307 rtc;
int h, m, s;
int buttonSetPin = 2; // Pin to enter time setting mode
int buttonHourPin = 3; // Pin to increment hour
int buttonMinutePin = 4; // Pin to increment minute
bool settingMode = false; // Track if in setting mode
void setup() {
Serial.begin(115200);
Wire.begin();
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running! Setting the time.");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set to compile time
}
// Pin setup
pinMode(buttonSetPin, INPUT_PULLUP);
pinMode(buttonHourPin, INPUT_PULLUP);
pinMode(buttonMinutePin, INPUT_PULLUP);
Timer1.initialize(1000);
Timer1.attachInterrupt(ScanDMD);
dmd.clearScreen(true);
}
int lastHour = -1, lastMinute = -1, lastSecond = -1;
bool colonState = false; // To track colon blinking state
void loop() {
// Check button for setting mode
if (digitalRead(buttonSetPin) == LOW) {
delay(200); // Debounce delay
settingMode = !settingMode; // Toggle setting mode
if (settingMode) {
Serial.println("Entering time setting mode");
DateTime now = rtc.now(); // Get current time
h = now.hour(); // Use current hour for setting
m = now.minute(); // Use current minute for setting
} else {
Serial.println("Exiting time setting mode");
rtc.adjust(DateTime(rtc.now().year(), rtc.now().month(), rtc.now().day(), h, m, 0)); // Save time in 24-hour format
dmd.clearScreen(true); // Clear the screen when exiting time-setting mode
}
delay(200); // Debounce delay
}
// Time setting mode (24-hour format)
if (settingMode) {
// Increment hour
if (digitalRead(buttonHourPin) == LOW) {
delay(200); // Debounce delay
h = (h + 1) % 24; // Properly wrap hour to stay between 0 and 23
Serial.print("Hour set to: ");
Serial.println(h); // This will print the 24-hour format during setting mode
}
// Increment minute
if (digitalRead(buttonMinutePin) == LOW) {
delay(200); // Debounce delay
m = (m + 1) % 60; // Properly wrap minutes from 59 to 0
Serial.print("Minute set to: ");
Serial.println(m);
}
// Show the current time being set on the display in 24-hour format
displayTime(h, m, s, true); // Passing `true` to indicate time-setting mode
} else {
// Display mode (convert 24-hour to 12-hour format for display)
DateTime now = rtc.now();
h = now.hour(); // Keep `h` in 24-hour format internally
m = now.minute();
s = now.second();
// Only update the display if the time has changed
if (h != lastHour || m != lastMinute || s != lastSecond) {
displayTime(h, m, s, false); // Passing `false` for normal display mode
lastHour = h;
lastMinute = m;
lastSecond = s;
}
// Handle colon blinking separately without clearing the display
updateColon(s);
}
}
// Function to display time on the DMD
void displayTime(int h, int m, int s, bool isSettingMode) {
dmd.selectFont(Font);
// Create buffers for hours and minutes
char hourBuffer[3];
char minuteBuffer[3];
int displayHour = h;
// If not in time-setting mode, convert 24-hour to 12-hour format for display
if (!isSettingMode) {
displayHour = h % 12; // Convert 24-hour to 12-hour format
if (displayHour == 0) {
displayHour = 12; // Handle midnight/noon cases
}
}
// Format the hour for display
if (isSettingMode) {
// Always show hour in 24-hour format in time-setting mode
snprintf(hourBuffer, sizeof(hourBuffer), "%02d", h); // Keep leading zero for time setting
} else {
// Remove leading zeros for single-digit hours in 12-hour format
if (displayHour < 10) {
snprintf(hourBuffer, sizeof(hourBuffer), "%d", displayHour); // No leading zero for single-digit hours
} else {
snprintf(hourBuffer, sizeof(hourBuffer), "%02d", displayHour); // Keep leading zero for double digits
}
}
// Format the minute with leading zeros
snprintf(minuteBuffer, sizeof(minuteBuffer), "%02d", m);
// Adjust the position of the hour based on whether it's single or double digits
int hourPosition = (!isSettingMode && displayHour < 10) ? 7 : 0; // Adjust position only if in display mode and hour is single digit
// Update only the hour if it has changed
if (displayHour != lastHour) {
dmd.drawString(hourPosition, 0, hourBuffer, 2, GRAPHICS_NORMAL); // Display hour
}
// Update only the minute if it has changed
if (m != lastMinute) {
dmd.drawString(19, 0, minuteBuffer, 2, GRAPHICS_NORMAL); // Display minute
}
}
// Function to handle colon blinking without clearing the screen
void updateColon(int s) {
// Blink colon every second
if (s % 2 == 0 && !colonState) {
colonState = true;
dmd.drawFilledBox(15, 3, 16, 4, GRAPHICS_OR); // Draw top part of colon
dmd.drawFilledBox(15, 11, 16, 12, GRAPHICS_OR); // Draw bottom part of colon
} else if (s % 2 != 0 && colonState) {
colonState = false;
dmd.drawFilledBox(15, 3, 16, 4, GRAPHICS_NOR); // Clear top part of colon
dmd.drawFilledBox(15, 11, 16, 12, GRAPHICS_NOR); // Clear bottom part of colon
}
}
void ScanDMD() {
dmd.scanDisplayBySPI();
}
and my modified code which use DMD2 but not able to see stable display and not readable of hour digit, minute can be readable but contains flickering. here is the modified code.
#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <DMD2.h>
#include "Font.h"
#define DISPLAYS_ACROSS 1
#define DISPLAYS_DOWN 1
SoftDMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN); // Use SoftDMD for DMD2
RTC_DS1307 rtc;
int h, m, s;
int buttonSetPin = 2; // Pin to enter time setting mode
int buttonHourPin = 3; // Pin to increment hour
int buttonMinutePin = 4; // Pin to increment minute
bool settingMode = false; // Track if in setting mode
int brightness = 50; // Brightness level (0-255)
void setup() {
Serial.begin(9600);
Wire.begin();
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running! Setting the time.");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set to compile time
}
// Pin setup
pinMode(buttonSetPin, INPUT_PULLUP);
pinMode(buttonHourPin, INPUT_PULLUP);
pinMode(buttonMinutePin, INPUT_PULLUP);
// Initialize DMD
dmd.begin();
dmd.setBrightness(brightness); // Set initial brightness
dmd.selectFont(Font); // Set font (can change to preferred font)
dmd.clearScreen(); // Clear the screen initially
}
int lastHour = -1, lastMinute = -1, lastSecond = -1;
bool colonState = false; // To track colon blinking state
void loop() {
// Check button for setting mode
if (digitalRead(buttonSetPin) == LOW) {
delay(200); // Debounce delay
settingMode = !settingMode; // Toggle setting mode
if (settingMode) {
Serial.println("Entering time setting mode");
DateTime now = rtc.now(); // Get current time
h = now.hour(); // Use current hour for setting
m = now.minute(); // Use current minute for setting
} else {
Serial.println("Exiting time setting mode");
rtc.adjust(DateTime(rtc.now().year(), rtc.now().month(), rtc.now().day(), h, m, 0)); // Save time in 24-hour format
dmd.clearScreen(); // Clear the screen when exiting time-setting mode
}
delay(200); // Debounce delay
}
// Time setting mode (24-hour format)
if (settingMode) {
// Increment hour
if (digitalRead(buttonHourPin) == LOW) {
delay(200); // Debounce delay
h = (h + 1) % 24; // Properly wrap hour to stay between 0 and 23
Serial.print("Hour set to: ");
Serial.println(h); // This will print the 24-hour format during setting mode
}
// Increment minute
if (digitalRead(buttonMinutePin) == LOW) {
delay(200); // Debounce delay
m = (m + 1) % 60; // Properly wrap minutes from 59 to 0
Serial.print("Minute set to: ");
Serial.println(m);
}
// Show the current time being set on the display in 24-hour format
displayTime(h, m, s, true); // Passing true to indicate time-setting mode
} else {
// Display mode (convert 24-hour to 12-hour format for display)
DateTime now = rtc.now();
h = now.hour(); // Keep h in 24-hour format internally
m = now.minute();
s = now.second();
// Only update the display if the time has changed
if (h != lastHour || m != lastMinute || s != lastSecond) {
displayTime(h, m, s, false); // Passing false for normal display mode
lastHour = h;
lastMinute = m;
lastSecond = s;
}
// Handle colon blinking separately without clearing the display
updateColon(s);
}
}
// Function to display time on the DMD2
void displayTime(int h, int m, int s, bool isSettingMode) {
dmd.selectFont(Font); // Select the font
// Create buffers for hours and minutes
char hourBuffer[3];
char minuteBuffer[3];
int displayHour = h;
// If not in time-setting mode, convert 24-hour to 12-hour format for display
if (!isSettingMode) {
displayHour = h % 12; // Convert 24-hour to 12-hour format
if (displayHour == 0) {
displayHour = 12; // Handle midnight/noon cases
}
}
// Format the hour for display
snprintf(hourBuffer, sizeof(hourBuffer), "%02d", displayHour);
// Format the minute with leading zeros
snprintf(minuteBuffer, sizeof(minuteBuffer), "%02d", m);
// Clear the display before updating
dmd.clearScreen();
// Display hour and minute
dmd.drawString(0, 0, hourBuffer, GRAPHICS_ON); // Position hour
dmd.drawString(19, 0, minuteBuffer, GRAPHICS_ON); // Position minute
}
// Function to handle colon blinking without clearing the screen
void updateColon(int s) {
// Blink colon every second
if (s % 2 == 0 && !colonState) {
colonState = true;
dmd.drawFilledBox(15, 3, 16, 4, GRAPHICS_ON); // Draw top part of colon
dmd.drawFilledBox(15, 11, 16, 12, GRAPHICS_ON); // Draw bottom part of colon
} else if (s % 2 != 0 && colonState) {
colonState = false;
dmd.drawFilledBox(15, 3, 16, 4, GRAPHICS_OFF); // Clear top part of colon
dmd.drawFilledBox(15, 11, 16, 12, GRAPHICS_OFF); // Clear bottom part of colon
}
}
void ScanDMD() {
dmd.scanDisplay();
}