Problem with RTC and 4digit 7-Segment-Display (LedControl Library)

Hello

I’m not really good with programming and finding errors in my programm, so i need your help.
I wrote a sketch that should programm the Arduino to read the time out of the DS1307RTC-Module and than show the time on a 4-digit 7-Segment-Display. I’m using the LedControl Library to control the 7-Segment-Display which is connected to a MAX7219.
This is my code:

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
 
//7-Segment
#include <LedControl.h>
 
void setup () {
  Serial.begin(9600);
 
//RTC - Vom Beispiel einfach übernommen
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin()
#endif
  rtc.begin();
  
 
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
 
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
 

 
void loop () {
DateTime now = rtc.now();
LedControl seg=LedControl (11, 13, 7, 0);

 seg.shutdown(0, false);
seg.setIntensity(0, 15);
  
  //// ANZEIGE DER UHRZEIT
  
  //wie oben: führende 0 bei der Stunde
  if (now.hour() < 10) {
    seg.setDigit (0, 3, 0, false);
  }
  //Anzeige Stunde
  {seg.setDigit (0, 2, now.hour(), false);}
    
  //wie oben: führende 0 bei der Minute
  if (now.minute() < 10) {
    seg.setDigit (0, 1, 0, false);
  }
  
  //Anzeige der Minute
 {seg.setDigit (0, 0, now.minute(), false);
 
}
  }

And this is the ERROR-Message i get:

C:\Users\Alexia\Desktop\programme_arduino\ma\time\time.ino: In function 'void setup()':

time:32: error: a function-definition is not allowed here before '{' token

 void loop () {

              ^

time:57: error: expected '}' at end of input

   }

   ^

time:57: error: expected '}' at end of input

exit status 1
a function-definition is not allowed here before '{' token

Is there anyone who could help me?
Thank you
techniclover

(deleted)

It looks like you forgot to put one of these

}

at the end of your setup() function.

Also, this

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
 
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

needs this

  }

after it.

Remember: every { needs a }.

Also, you will have a problem with the digits:

  {seg.setDigit (0, 2, now.hour(), false);}

You can only use setDigit() for single-digit (0 through 9) numbers, otherwise it won’t do what you want. Here is a way to make the code what you want:

  byte hourOnes = now.hour();
  byte hourTens = 0;
  while (hourOnes > 9) {
    hourOnes = hourOnes - 10;
    hourTens = hourTens + 1;
  }
  seg.setDigit (0, 3, hourTens, false);
  seg.setDigit (0, 2, hourOnes, false);

Here is a slightly more “professional” way to write the same thing:

  byte hourOnes = now.hour();
  byte hourTens = 0;
  while (hourOnes > 9) {
    hourOnes -= 10;
    hourTens++;
  }
  seg.setDigit (0, 3, hourTens, false);
  seg.setDigit (0, 2, hourOnes, false);

Thank you. This should be ok now.
But i got another ERROR-Message:

Arduino: 1.6.10 (Windows 8.1), Board: "Arduino/Genuino Uno"

time:22: error: expected unqualified-id before 'if'

if (! rtc.isrunning()) {

^

exit status 1
expected unqualified-id before 'if'

The code is now:

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
 
//7-Segment
#include <LedControl.h>
 
void setup () {
  Serial.begin(9600);
 
//RTC - Vom Beispiel einfach übernommen
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin()
#endif
  rtc.begin();
}
  
 
if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
 
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

 
void loop () {
DateTime now = rtc.now();
LedControl seg=LedControl (11, 13, 7, 0);

 seg.shutdown(0, false);
seg.setIntensity(0, 15);
  
  //// ANZEIGE DER UHRZEIT
  
  //wie oben: führende 0 bei der Stunde
 byte hourOnes = now.hour();
  byte hourTens = 0;
  while (hourOnes > 9) {
    hourOnes = hourOnes - 10;
    hourTens = hourTens + 1;
  }
  seg.setDigit (0, 3, hourTens, false);
  seg.setDigit (0, 2, hourOnes, false);
  
  //wie oben: führende 0 bei der Minute
  byte minuteOnes = now.minute();
  byte minuteTens = 0;
  while (minuteOnes > 9) {
    minuteOnes = minuteOnes - 10;
    minuteTens = minuteTens + 1;
  }
  seg.setDigit (0, 1, minuteTens, false);
  seg.setDigit (0, 0, minuteOnes, false);
 
}

(deleted)

This part

if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
 
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

is supposed to go inside your setup() function.

Take it and move it to just before the } at the end of your setup() function. Then it will be inside the setup() function, where it is supposed to be.

And how does a if-statement floating in mid air suppose to do anything? Doesn't that need to be IN setup()?

Thank you for your help. The code can be uploaded now. But the 7-Segment-Display is't static. The numbers shown can't be read and flicker all the time.
Anyone knows what this could be?
Thanks

Try putting this inside your loop() function:

delay(50); // pause for 0.05 second

This might help reduce the flicker.

50mS delay will make it worse - that's 20 Hz.

I believe this belongs before setup():
LedControl seg=LedControl (11, 13, 7, 0);
These belong in setup():
seg.shutdown(0, false);
seg.setIntensity(0, 15);

RTCs only update their time once a second. I'd suggest changing your code to request time once a second, when you detect that the minutes or hours have changed, update the display then, not continuously.
Either that, or explain better what is supposed to be happening.

I tried putting in delay (1000); now it only flickers every second. But sometimes one of the digits just doesn't light up.

I believe this belongs before setup():
LedControl seg=LedControl (11, 13, 7, 0);
These belong in setup():
seg.shutdown(0, false);
seg.setIntensity(0, 15);

When I tried this, none of the digits did light up.

It's supposed to only change time every minute. And the hour should only change when the hour changes for example from 6 to 7.