Digital clock with RTC and serial 7 segment display

Hi,

So I’ve got myself an uno, a DS3231 RTC and a serial 7 segment display and i’m trying to get the display to show the current time. Bear in mind that i’m a complete coding novice.

I’ve connected the display TX>8 and RX>7 and i’ve bodged/copied the below code but i’m getting the error message ‘rtc’ was not declared in this scope on the “s7s.print(String(rtc.hour())); // Print hour” line.

  1. Is this code way more complicated than I need?
  2. If it’s what I need whats the problem with that line?
#include <DS3231.h>
#include <SoftwareSerial.h>

#define DS3231_CS_PIN A5 // RTC Chip-select pin

const int softwareTx = 8; //serial pin assignment for 7SD
const int softwareRx = 7;

unsigned long previousTime = 0; 
const long interval = 1000;
boolean colon = false;

SoftwareSerial s7s(softwareRx, softwareTx);

void setup() 
{ 
  Serial.begin(9600); // Baud for serial monitor communication
  s7s.begin(9600); // Baud for 7 segment communication
    
  clearDisplay(); // Clear the display before jumping into loop

  setBrightness(255); // max brightness

  rtc.begin(DS3231_CS_PIN);

  //rtc.autoTime(); // Pulls an initial time from IDE's time, comment out once time is set
}
void loop() {
  static int8_t lastSecond = -1;
  static int8_t lastMinute = -1;
  static int8_t lastHour = -1;
  
  // Call rtc.update() to update all rtc.seconds(), rtc.minutes(),
  // etc. return functions.
  rtc.update();

  if (rtc.second() != lastSecond) // If the second has changed
  {
    printTime(); // Print the new time

    lastSecond = rtc.second(); // Update lastSecond value
  }
  if (rtc.minute() != lastMinute) // If the minute has changed
  {
    printTime(); // Print the new time

    lastMinute = rtc.minute(); // Update lastMinute value
  }
  if (rtc.hour() != lastHour) // If the hour has changed
  {
    printTime(); // Print the new time

    lastHour = rtc.hour(); // Update lastHour value
  }
  
   colonBlink();

}  // end loop

//////////FUNCTIONS///////////

void printTime()
{
  if (rtc.hour() < 10)
    s7s.print('0'); // Print leading '0' for hour
  s7s.print(String(rtc.hour())); // Print hour
  if (rtc.minute() < 10)
    s7s.print('0'); // Print leading '0' for minute
  s7s.print(String(rtc.minute())); // Print minute
        
/*Uncomment to see time on serial monitor
    Serial.print(String(rtc.hour()) + ":"); // Print hour
  if (rtc.minute() < 10)
    Serial.print('0'); // Print leading '0' for minute
  Serial.print(String(rtc.minute()) + ":"); // Print minute
  if (rtc.second() < 10)
    Serial.print('0'); // Print leading '0' for second
  Serial.print(String(rtc.second())); // Print second
  Serial.println();*/
}
void clearDisplay()
{
  s7s.write(0x76);  // Clear display command
}
void setDecimals(byte decimals)
{
  s7s.write(0x77);
  s7s.write(decimals);
}

void setBrightness(byte value)  //Brightness of S7D
{
  s7s.write(0x7A);  // Set brightness command byte
  s7s.write(value);  // brightness data byte
} 
void colonBlink()
{
  unsigned long currentTime = millis();
  if( currentTime - previousTime >= interval)
  {
    previousTime = currentTime;

    if(colon == false)
    {
      colon = true;
      setDecimals(0b00010000);
    }
    else
    {
      colon = false; 
      setDecimals(0b00000000);
    }
   }
}

Can you post links to the DS3231 library your using as there are several versions out there but none I have seen need a Chip Select signal (DS3231_CS_PIN).

The error your getting is probably because the library is not installed correctly or your not using it correctly in your code.
You don't have a global (outside of setup/loop) definition for rtc so this might be a problem.

The below code is not really needed as the seconds changed code will do all you need (you don't change minutes or hours without changing seconds first)

  if (rtc.minute() != lastMinute) // If the minute has changed
  {
    printTime(); // Print the new time
    
    lastMinute = rtc.minute(); // Update lastMinute value
  }
  if (rtc.hour() != lastHour) // If the hour has changed
  {
    printTime(); // Print the new time
    
    lastHour = rtc.hour(); // Update lastHour value
  }

colonBlink() is more complex than needed but that's just a cosmetic thing so no important yet.

Don't i2c device libraries also need Wire.h?

rtc is an object that you have to initialize.
I use this:

DS3231  rtc(SDA, SCL);

I’m using an arduino mega but I’d imagine that you could set SDA and SCL to A0 and A1.