Is there any way to do this faster?

I am a beginner is all this sort of stuff but I know there has to be a quicker way to do this.
I am trying to make a clock using a RTC and a ledstrip and it is taking forever to write the code.
the code is still unfinished as I still need to do the rest of the minutes

#include <FastLED.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;

#define NUM_LEDS 28
#define LED_PIN 3

CRGB leds[NUM_LEDS];

void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(20);
    Serial.begin(9600);
    Wire.begin();
    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(__DATE__, __TIME__));
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  DateTime now = RTC.now(); 
   int uur = now.hour();
   int minuten = now.minute();
 
 if(uur == 0){
  leds[0] = CRGB::Red;
   leds[1] = CRGB::Red;
    leds[2] = CRGB::Red;
     leds[3] = CRGB::Red;
      leds[4] = CRGB::Red;
       leds[5] = CRGB::Red;
  
 }
 
  if(uur == 1){
     leds[1] = CRGB::Red;
      leds[2] = CRGB::Red;
  }
  
  if(uur == 2){
     leds[0] = CRGB::Red;
      leds[1] = CRGB::Red;
       leds[3] = CRGB::Red;
        leds[4] = CRGB::Red;
         leds[6] = CRGB::Red;
  }
  if(uur == 3){
    leds[0] = CRGB::Red;
    leds[1] = CRGB::Red;
    leds[2] = CRGB::Red;
    leds[3] = CRGB::Red;
    leds[6] = CRGB::Red;
  }
  if (uur == 4){
    leds[1] = CRGB::Red;
     leds[2] = CRGB::Red;
      leds[5] = CRGB::Red;
       leds[6] = CRGB::Red;    
  }
  if (uur == 5){
     leds[0] = CRGB::Red;
      leds[2] = CRGB::Red;
       leds[3] = CRGB::Red;
        leds[5] = CRGB::Red;
         leds[6] = CRGB::Red;
  }
  if (uur == 6){
     leds[0] = CRGB::Red;
      leds[2] = CRGB::Red;
       leds[3] = CRGB::Red;
        leds[4] = CRGB::Red;
         leds[5] = CRGB::Red;
          leds[6] = CRGB::Red;
  }
  if (uur == 7){
     leds[0] = CRGB::Red;
      leds[1] = CRGB::Red;
       leds[2] = CRGB::Red;
  }
  if (uur = 8){
     leds[7] = CRGB::Red;
      leds[8] = CRGB::Red;
       leds[9] = CRGB::Red;
        leds[10] = CRGB::Red;
         leds[11] = CRGB::Red;
          leds[12] = CRGB::Red;
           leds[13] = CRGB::Red;
  }
  if (uur == 9){
     leds[0] = CRGB::Red;
      leds[1] = CRGB::Red;
       leds[2] = CRGB::Red;
        leds[3] = CRGB::Red;
         leds[5] = CRGB::Red;
          leds[6] = CRGB::Red;
  }
  if (uur == 10){
     leds[1] = CRGB::Red;
      leds[2] = CRGB::Red;

       leds[7] = CRGB::Red;
        leds[8] = CRGB::Red;
         leds[9] = CRGB::Red;
          leds[10] = CRGB::Red;
           leds[11] = CRGB::Red;
            leds[12] = CRGB::Red;
  }
  if (uur == 11){
     leds[1] = CRGB::Red;
      leds[2] = CRGB::Red;

       leds[8] = CRGB::Red;
        leds[9] = CRGB::Red;
  }
  if (uur == 12){
     leds[1] = CRGB::Red;
      leds[2] = CRGB::Red;

       leds[7] = CRGB::Red;
        leds[8] = CRGB::Red;
         leds[10] = CRGB::Red;
          leds[11] = CRGB::Red;
           leds[13] = CRGB::Red;
  }
  if (minuten == 1){
     leds[22] = CRGB::Red;
      leds[23] = CRGB::Red;
  }
  if (minuten == 2){
     leds[21] = CRGB::Red;
      leds[22] = CRGB::Red;
       leds[24] = CRGB::Red;
        leds[25] = CRGB::Red;
         leds[27] = CRGB::Red;
  }
  if (minuten == 3){
     leds[21] = CRGB::Red;
      leds[22] = CRGB::Red;
       leds[23] = CRGB::Red;
        leds[24] = CRGB::Red;
         leds[27] = CRGB::Red;
  }
  if (minuten == 4){
     leds[22] = CRGB::Red;
      leds[23] = CRGB::Red;
       leds[26] = CRGB::Red;
        leds[27] = CRGB::Red;
  }
  if (minuten == 5){
    
  }
  FastLED.show();


   //uur -> seg 1 en 2 

   // uur = 0 =>led à tem6 en 7 tem 12 
   // uur = 01 =>
   //minuten -> seg 3 en 4 
    Serial.print(' ');
    Serial.print(uur);
    Serial.print(':');
    Serial.print(minuten);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println(); 
    delay(1000);
}

Welcome to the forum

What exactly is it that you want to do faster ?

Are you actually asking for a neater way to write the sketch ?

Could your problem be the one second delay?

You will get faster at programming as you learn more about how to. Program.

Be patient. Even something simple will take some time to get right!

a7

Hi,
I am mainly asking for a better way to write this code. As you can see I am now trying that for every time the good leds will be on. I just want to know if there is any faster way of doing this in place of writing a piece of code for every single minute there is.

Yes it is a bit pedestrian the way you are going about it.
Why not make use of arrays to encode your data and have a function where you pass the number you want to display directly to that function.
Rather in the same way as this tutorial
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

There is no recipe or manual or tutorial that will give you the ability to program the way you want.

The refined code can be achieved only by programming over and over again, until you master it.

Right now the expert on the hardware you want to control is yourself. We are just spectators with the occasional different idea to contribute.

RTC, fastLED and delay() don't work well together, you should consider millis(). There is your first refinement in the code

Yes. It looks like you are making a seven-segment display. Each group of seven LEDs represents a number. You know the first LED of each digit and you know the offset from there of each segment.

Write a function that will display one digit given the digit value and the LED number of the first LED for the digit (0, 7, 14...)

Split the minutes into two digits.

  int highDigit = minuten / 10;
  int lowDigit = minuten % 10;  // 'modulo' = remainder after division.
  ShowDigit(14, highDigit);
  ShowDigit(21, lowDigit);

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