Arduino led clock minutes and hours settings

i first had this code for my led clock and its working wel.
Then i added three buttons for setting the time.
when switch pin 5 on the minutes go blink then i also connect pin 3 but the minutes wil not add 1 also with the ours.

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include "RTClib.h"

// Definieer het aantal LED's en de pin waarop de LED-strip is aangesloten
#define LED_PIN     6
#define LED_COUNT   60

// Definieer de pinnen voor de schakelaars
#define SET_MINUTES_PIN 5  // Schakelaar om minuten in te stellen
#define INCREASE_PIN    3  // Schakelaar om uren/minuten te verhogen
#define SET_HOURS_PIN   4  // Schakelaar om uren in te stellen

// Initialiseer de LED-strip
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

// Initialiseer de DS3231 RTC
RTC_DS3231 rtc;

// Variabelen voor handmatige tijdinstelling
unsigned long manualTimeSetMillis = 0; // Tijdstip waarop de handmatige tijd is ingesteld
int manualHours = -1;
int manualMinutes = -1;
int manualSeconds = -1;

// Variabelen voor het instellen van uren en minuten
bool settingHours = false; // Geeft aan of we uren instellen
bool settingMinutes = false; // Geeft aan of we minuten instellen
unsigned long lastBlinkMillis = 0; // Tijdstip van de laatste knipperactie
bool blinkState = false; // Huidige knipperstatus (aan/uit)

void setup() {
  // Start de seriële communicatie voor debugging
  Serial.begin(9600);

  // Initialiseer de LED-strip
  strip.begin();
  strip.show(); // Zet alle LED's uit
  strip.setBrightness(5); // Stel de helderheid in (0-255)

  // Initialiseer de RTC
  if (!rtc.begin()) {
    Serial.println("Kon RTC niet vinden!");
    while (1);
  }

  // Als de RTC geen tijd heeft, stel dan de tijd handmatig in
  if (rtc.lostPower()) {
    Serial.println("RTC heeft geen stroom gehad, stel de tijd in!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  // Stel de schakelaarpinnen in als invoer
  pinMode(SET_MINUTES_PIN, INPUT_PULLUP);
  pinMode(INCREASE_PIN, INPUT_PULLUP);
  pinMode(SET_HOURS_PIN, INPUT_PULLUP);

  // Instructies voor de seriële monitor
  Serial.println("Gebruik de schakelaars om uren/minuten in te stellen:");
  Serial.println("- Pin 5: Minuten instellen (knipperen)");
  Serial.println("- Pin 4: Uren instellen (knipperen)");
  Serial.println("- Pin 3: Verhoog uren/minuten");
}

void loop() {
  // Controleer of er invoer is op de seriële monitor
  if (Serial.available()) {
    String input = Serial.readStringUntil('\n');
    if (input.length() == 8 && input.charAt(2) == ':' && input.charAt(5) == ':') {
      manualHours = input.substring(0, 2).toInt();
      manualMinutes = input.substring(3, 5).toInt();
      manualSeconds = input.substring(6, 8).toInt();
      manualTimeSetMillis = millis(); // Sla het tijdstip van instellen op
      Serial.println("Tijd ingesteld op: " + input);
    } else {
      Serial.println("Ongeldige invoer. Gebruik het formaat HH:MM:SS.");
    }
  }

  // Controleer de schakelaars
  if (digitalRead(SET_MINUTES_PIN) == LOW) {
    settingMinutes = true;
    settingHours = false;
  } else if (digitalRead(SET_HOURS_PIN) == LOW) {
    settingHours = true;
    settingMinutes = false;
  } else {
    settingHours = false;
    settingMinutes = false;
  }

  // Verhoog uren of minuten als de verhoog-schakelaar is ingedrukt
  if (digitalRead(INCREASE_PIN) == LOW) {
    if (settingHours) {
      manualHours = (manualHours + 1) % 12; // Verhoog uren (modulo 12 voor 12-uursformaat)
      delay(200); // Debounce
    } else if (settingMinutes) {
      manualMinutes = (manualMinutes + 1) % 60; // Verhoog minuten (modulo 60)
      delay(200); // Debounce
    }
    // Wacht tot de knop is losgelaten om herhaaldelijk verhogen te voorkomen
    while (digitalRead(INCREASE_PIN) == LOW) {
      delay(10);
    }
  }

  // Bereken de huidige tijd (handmatig of van de RTC)
  int hours, minutes, seconds;
  if (manualHours != -1 && manualMinutes != -1 && manualSeconds != -1) {
    // Gebruik handmatige tijd en laat deze doortellen
    unsigned long elapsedSeconds = (millis() - manualTimeSetMillis) / 1000;
    seconds = manualSeconds + elapsedSeconds;
    minutes = manualMinutes + seconds / 60;
    hours = manualHours + minutes / 60;
    seconds %= 60;
    minutes %= 60;
    hours %= 12; // Gebruik modulo 12 voor 12-uursformaat
  } else {
    // Gebruik RTC-tijd
    DateTime now = rtc.now();
    hours = now.hour();
    minutes = now.minute();
    seconds = now.second();
  }

  // Zet alle LED's uit
  for (int i = 0; i < LED_COUNT; i++) {
    strip.setPixelColor(i, strip.Color(0, 0, 0)); // Uit
  }

  // Toon de uren (groen)
  int hourLED = (hours * 5) % LED_COUNT; // Verdeel de uren over 60 LED's
  for (int i = 0; i < 3; i++) {
    if (!settingHours || blinkState) { // Knipperen als uren worden ingesteld
      strip.setPixelColor((hourLED + i) % LED_COUNT, strip.Color(0, 255, 0)); // Groen
    }
  }

  // Toon de minuten (rood)
  int minuteLED = minutes % LED_COUNT; // Verdeel de minuten over 60 LED's
  if (!settingMinutes || blinkState) { // Knipperen als minuten worden ingesteld
    strip.setPixelColor(minuteLED, strip.Color(255, 0, 0)); // Rood (eerste LED)
    strip.setPixelColor((minuteLED + 1) % LED_COUNT, strip.Color(255, 0, 0)); // Rood (tweede LED)
  }

  // Toon de seconden (blauw)
  int secondLED = seconds % LED_COUNT; // Verdeel de seconden over 60 LED's
  strip.setPixelColor(secondLED, strip.Color(0, 0, 255)); // Blauw

  // Update de LED-strip
  strip.show();

  // Knipperlogica
  if (millis() - lastBlinkMillis >= 500) { // Knipper elke 500 ms
    blinkState = !blinkState;
    lastBlinkMillis = millis();
  }

  // Toon de tijd en LED-status in de seriële monitor
  Serial.print("Tijd: ");
  Serial.print(hours);
  Serial.print(":");
  Serial.print(minutes);
  Serial.print(":");
  Serial.print(seconds);
  Serial.print(" | Uren LED's: ");
  Serial.print(hourLED);
  Serial.print(", ");
  Serial.print((hourLED + 1) % LED_COUNT);
  Serial.print(", ");
  Serial.print((hourLED + 2) % LED_COUNT);
  Serial.print(" | Minuten LED's: ");
  Serial.print(minuteLED);
  Serial.print(", ");
  Serial.print((minuteLED + 1) % LED_COUNT);
  Serial.print(" | Seconden LED: ");
  Serial.println(secondLED);

  delay(100); // Korte vertraging voor betere responsiviteit
}

does somebody know what i did wrong?
arduino uno with ds3231 rtc and 60 ws2812b leds

Welcome to the forum

It looks like you tried to use code tags but failed. Please edit your post and correct the code tags

Should this be ||?

hi @jeronimus2212 welcome to the forum.

I am working on a 32 LED clock. I have played a bit with your code. I have changed the last dozen or so lines but have not found your problem.

This cuts down the printing and removes the baffling delay(). Stopping all progress (delay) cannot be necessary and in fact seems counterintuitive if the goal is to improve responsiveness?

  static int lastSecond = -1;
  if (lastSecond == seconds) return;

  lastSecond = seconds;

  // Toon de tijd en LED-status in de seriële monitor
  Serial.print("Tijd: ");
  Serial.print(hours);
  Serial.print(":");
  Serial.print(minutes);
  Serial.print(":");
  Serial.print(seconds);
  Serial.print(" | Uren LED's: ");
  Serial.print(hourLED);
  Serial.print(", ");
  Serial.print((hourLED + 1) % LED_COUNT);
  Serial.print(", ");
  Serial.print((hourLED + 2) % LED_COUNT);
  Serial.print(" | Minuten LED's: ");
  Serial.print(minuteLED);
  Serial.print(", ");
  Serial.print((minuteLED + 1) % LED_COUNT);
  Serial.print(" | Seconden LED: ");
  Serial.println(secondLED);

//  delay(100); // short delay for better responsiveness
}

I haven't found where after setting the clock manually with the buttons you set the RTC to agree. But that's for another day (or hiding from me so far).

You might like the wokwi simulator. I'll be adding the Neopixel ring sometime or you can save your own copy:

a7

it is fixed i removed the manual time setting now it works.

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include "RTClib.h"

// Definieer het aantal LED's en de pin waarop de LED-strip is aangesloten
#define LED_PIN     6
#define LED_COUNT   60

// Definieer de pinnen voor het aanpassen van de tijd
#define HOUR_ADJUST_PIN   3
#define MINUTE_MODE_PIN   4
#define HOUR_MODE_PIN     5

// Initialiseer de LED-strip
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

// Initialiseer de DS3231 RTC
RTC_DS3231 rtc;

// Variabelen voor knipperen en instellen
bool minuteMode = false; // Modus voor minuten instellen
bool hourMode = false;   // Modus voor uren instellen
bool blinkState = false; // Huidige knipperstatus
unsigned long lastBlinkTime = 0; // Tijdstip van laatste knipper

void setup() {
  // Start de seriële communicatie voor debugging
  Serial.begin(9600);

  // Initialiseer de LED-strip
  strip.begin();
  strip.show(); // Zet alle LED's uit
  strip.setBrightness(50); // Stel de helderheid in (0-255)

  // Initialiseer de RTC
  if (!rtc.begin()) {
    Serial.println("Kon RTC niet vinden!");
    while (1);
  }

  // Als de RTC geen tijd heeft, stel dan de tijd handmatig in
  if (rtc.lostPower()) {
    Serial.println("RTC heeft geen stroom gehad, stel de tijd in!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  // Stel de pinnen in voor het aanpassen van de tijd
  pinMode(HOUR_ADJUST_PIN, INPUT_PULLUP);
  pinMode(MINUTE_MODE_PIN, INPUT_PULLUP);
  pinMode(HOUR_MODE_PIN, INPUT_PULLUP);
}

void loop() {
  // Lees de huidige tijd van de RTC
  DateTime now = rtc.now();
  int hours = now.hour();
  int minutes = now.minute();
  int seconds = now.second();

  // Controleer of de minuten- of urenmodus actief is
  minuteMode = (digitalRead(MINUTE_MODE_PIN) == LOW); // Corrigeer de haakjes
  hourMode = (digitalRead(HOUR_MODE_PIN) == LOW);     // Corrigeer de haakjes

  // Als de minutenmodus actief is, knipper de minuten-LED's
  if (minuteMode) {
    if (millis() - lastBlinkTime > 500) { // Knipper elke 500 ms
      blinkState = !blinkState;
      lastBlinkTime = millis();
    }
  }

  // Als de urenmodus actief is, knipper de uren-LED's
  if (hourMode) {
    if (millis() - lastBlinkTime > 500) { // Knipper elke 500 ms
      blinkState = !blinkState;
      lastBlinkTime = millis();
    }
  }

  // Als Pin 3 (uren/minuten aanpassen) is ingedrukt
  if (digitalRead(HOUR_ADJUST_PIN) == LOW) {
    if (minuteMode) {
      minutes = (minutes + 1) % 60; // Verhoog minuten met 1
      rtc.adjust(DateTime(now.year(), now.month(), now.day(), hours, minutes, seconds));
      delay(200); // Debounce
    } else if (hourMode) {
      hours = (hours + 1) % 24; // Verhoog uren met 1
      rtc.adjust(DateTime(now.year(), now.month(), now.day(), hours, minutes, seconds));
      delay(200); // Debounce
    }
  }

  // Zet alle LED's uit
  for (int i = 0; i < LED_COUNT; i++) {
    strip.setPixelColor(i, strip.Color(0, 0, 0)); // Uit
  }

  // Toon de uren (groen)
  int hourLED = (hours * 5) % LED_COUNT; // Verdeel de uren over 60 LED's
  if (!hourMode || blinkState) { // Als urenmodus niet actief is of knipper aan
    for (int i = 0; i < 3; i++) {
      strip.setPixelColor((hourLED + i) % LED_COUNT, strip.Color(0, 255, 0)); // Groen
    }
  }

  // Toon de minuten (rood)
  int minuteLED = minutes % LED_COUNT; // Verdeel de minuten over 60 LED's
  if (!minuteMode || blinkState) { // Als minutenmodus niet actief is of knipper aan
    strip.setPixelColor(minuteLED, strip.Color(255, 0, 0)); // Rood (eerste LED)
    strip.setPixelColor((minuteLED + 1) % LED_COUNT, strip.Color(255, 0, 0)); // Rood (tweede LED)
  }

  // Toon de seconden (blauw)
  int secondLED = seconds % LED_COUNT; // Verdeel de seconden over 60 LED's
  strip.setPixelColor(secondLED, strip.Color(0, 0, 255)); // Blauw

  // Update de LED-strip
  strip.show();

  // Toon de tijd en LED-status in de seriële monitor
  Serial.print("Tijd: ");
  Serial.print(hours);
  Serial.print(":");
  Serial.print(minutes);
  Serial.print(":");
  Serial.print(seconds);
  Serial.print(" | Uren LED's: ");
  Serial.print(hourLED);
  Serial.print(", ");
  Serial.print((hourLED + 1) % LED_COUNT);
  Serial.print(", ");
  Serial.print((hourLED + 2) % LED_COUNT);
  Serial.print(" | Minuten LED's: ");
  Serial.print(minuteLED);
  Serial.print(", ");
  Serial.print((minuteLED + 1) % LED_COUNT);
  Serial.print(" | Seconden LED: ");
  Serial.println(secondLED);

  delay(500); // Korte vertraging voor betere responsiviteit
}

I did a 60 ring, in case you want to do the manual setting:

I don't use the RTC hh:mm:ss except to get the time at startup. Also at midnight I grab the RTC time.

Then I just watch to see when the RTC seconds change, and increment my own seconds, spilling into minutes, spilling into hours. The ring is updated at the same time.

If I use the buttons that changing my own hours and minutes, 15 seconds after the last button push I put my hours and minutes back into the RTC registers.

If the minutes are changed, I set the seconds in the RTC to zero. If only the hour changes, the seconds crank along undisturbed; this is for switching between standard and daylight savings without losing the accuracy the DS3231 provides.

a7