#include "SevSeg.h"
#include "RTClib.h"
SevSeg sevseg;
RTC_DS1307 rtc;
int datapin = 12;
int clockpin = 13;
int latchpin = 14;
String getTimeString() {
DateTime now = rtc.now();
int hour = now.hour();
int minute = now.minute();
int second = now.second();
char timeStr[10];
sprintf(timeStr, "%02d:%02d:%02d", hour, minute,second);
return String(timeStr);
}
void setup() {
Serial.begin(115200);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
}
byte numDigits = 4;
byte digitPins[] = {15, 14, 2, 5};
byte segmentPins[] = {12, 4, 16, 19, 26, 27, 13, 18, 25};
bool resistorsOnSegments = false;
byte hardwareConfig = COMMON_ANODE;
bool updateWithDelays = false;
bool disableDecPoint = false;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, disableDecPoint);
sevseg.setBrightness(90);
pinMode(12,OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
}
void loop() {
String timeStr = getTimeString();
Serial.println(timeStr);
sevseg.setChars(timeStr.c_str());
if (digitalRead(34) == LOW) {
sevseg.setBrightness(180);
}
if (digitalRead(34) == HIGH) {
sevseg.setBrightness(90);
}
sevseg.refreshDisplay();
delay(1000);
}
hi all,I simulated it on Wokwi and it showed confusion. What's going on? thanks.
Probably a good time to include a link to the Wokwi project.
J-M-L
October 25, 2023, 8:40am
3
you allocate 5 bytes and try to write 9 into the buffer... won't work if you overflow your buffer... (and you have 3 variable arguments to sprintf() and only provide 2)
char timeStr[10]; // be large as you are wasting memory with Strings anyway
sprintf(timeStr, "%02d:%02d", hour, minute);
J-M-L
October 25, 2023, 9:17am
6
just save the wokwi and post the shared link (fix what I said in post 3)
I modified the code according to your suggestion, but it still shows confusion.
J-M-L
October 25, 2023, 9:18am
8
post the link to the wokwi (save the wokwi and post the shared link)
J-M-L
October 25, 2023, 9:20am
11
your pins are not set as output
pinMode(12,OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
i add this code in setup,but still not work.
b707
October 25, 2023, 9:31am
13
update the code in the topic
J-M-L
October 25, 2023, 9:35am
16
TomGeorge:
it seems to work
the serial monitor yes, but not the 7 segments
But it displays confusingly on the 7 segment tube and I'm confused.
J-M-L
October 25, 2023, 9:42am
18
Actually on second thought, the sevenSegment library does not work with the 74hc595...
see the gitHub
It's often preferred to drive seven segment displays through shift register ICs (or other GPIO-expanding ICs), as that only uses ~3 micrcontroller pins instead of ~12 pins. This library does not support shift registers. However, there's a mostly-compatible branch that does support shift registers. See bridystone's SevSegShift .
you need to move to GitHub - bridystone/SevSegShift: Seven segment display controller library for Arduino
Hi.
As can be seen here and from library example codes.
The library controls the digit select and segment select directly, not though a 595.
1. The Library named SevSeg.h contains ready-made codes, routines, functions, and LUT tables written by somebody else, and it can be used to drive multi-digit 7-segment Display Unit. Now, the user is not required to execute pinMode(), digitalWrite(), bitRead(), delay(), for() and other commands in the sketch. The Library must be down loaded from this path: https://github.com/DeanIsMe/SevSeg and then it has to be included in the sketch and IDE. In this tutorial, we will use SevSeg.h Library to …
Tom....
2 Likes
system
Closed
April 22, 2024, 9:58am
20
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.