Help my 7segment project

#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.

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);

Hi,
What model Arduino?
Some woki info would be good to?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

esp32

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.

post the link to the wokwi (save the wokwi and post the shared link)

image

thanks

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.

update the code in the topic

HI, @jason7766
I just tried your woki, and it seems to work.

Tom.. :grinning: :+1: :coffee: :australia:

ok,updated completed.

the serial monitor yes, but not the 7 segments

But it displays confusingly on the 7 segment tube and I'm confused.

Actually on second thought, the sevenSegment library does not work with the 74hc595...

see the gitHub

Note on shift registers

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.
image

image

Tom.... :grinning: :+1: :coffee: :australia:

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.