Adding buttons to change RTC Time

Hi everyone, I have a question and I can't seem to find the answer for myself, so I'll have to turn to the experts on this forum:

For my school, I'm making a project: a letter clock. Basically, it's 94 LEDs of a 30 LEDs per meter WS 2812 B LED strip connected in 9 rows of 10 LEDs and a lower row of 4 LEDs. I'm using a DS 3231 RTC for controlling the time. Basically, the code works, so I could put together the whole clock and hang it on a wall, but the problem is that I can't change the time without disassembling the clock. But when we have to change the time for daylight saving time or when the clock hasn't had enough power for a certain time I just want to be able to change the time. What I wan't is to add 2 buttons: one to add 1 minute to the time and the second to add one hour to the time. The setup I have now, without the buttons, is as the image below:

The program/code I have written so far is as follows (it has some Dutch in it because it's for a Dutch letter clock):

[code]
#include <RealTimeClockDS3231.h>
#include <Adafruit_NeoPixel.h>
#include <Wire.h>

#define PixelCount 94                                 // Number of pixels
#define PixelPin 6                                   // Data output pin for pixels
#define pixelR 50
#define pixelG 0
#define pixelB 250


char formatted[] = "Clock Was Not Set.  ";            // *Do not alter (reduce) length of array

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PixelCount, PixelPin, NEO_GRB + NEO_KHZ800);


void clearDisplay() {
  for (byte a = 0; a < PixelCount; a++) {
    pixels.setPixelColor(a, 0x00000000);
  }
}

void setup() {
  Serial.begin(115200);
  // Serial.print(F(_FILE_));                          // Always display sketch name and date info
  Serial.print("\t");
  // Serial.print(F(_DATE_));
  Serial.print("\t");
  // Serial.println(F(_TIME_));

  pixels.begin();

  RTC.readClock();                                    // Read the clock
  RTC.writeData(0x0E, 0x00);                          // Enable 1Hz output on INT pin
  // if(RTC.getYear() == 0)//{                             // If Year = 0 then clock probably not set
  Serial.println(F("Set Clock"));
  // Load some values
  RTC.setHours(21);
  RTC.setMinutes(56);
  RTC.setSeconds(30);
  RTC.setDate(27);
  RTC.setMonth(03);
  RTC.setYear(22);
  RTC.setDayOfWeek(7);
  RTC.setClock();
  // }
  RTC.getFormatted2k(formatted);
  Serial.println(formatted);
}

void loop() {
  delay(1000);
  doTime();                                           // Display the updated time
}

void doTime() {
  static int lastM = -1;                        // We are only interested in minute boundries but we get called every second

  RTC.readClock();                              // Read the RTC
  int h = RTC.getHours();                       // Get the hour
  int m = RTC.getMinutes();                     // Get the minute

  if (m == lastM) {                             // Has Minute value changed since last time?
    return;                                     // No, so return
  }
  lastM = m;                                    // Set last minute to current minute

  if ((m % 1) == 0) {     // Only on the 1 minute boundries
    clearDisplay();       // Clear display buffer

    het_is();             // Always start with an 'It's'

    // 5 minute boundrys
    switch (m) {
      case 0:             // Top of the hour so it's hour o'clock
        uur();    // O'clock
        break;

      case 1:             // one minute boundry
        uur();
        min1();
        break;

      case 2:             // one minute boundry
        uur();
        min2();
        break;

      case 3:            // one minute boundry
        uur();
        min3();
        break;

      case 4:             // one minute boundry
        uur();
        min4();
        break;

      case 5:             // For 5-9 minutes
        vijf_min();         // 'five'. It's five minutes past the hour
        over();
        break;

      case 6:             // For 5-9 minutes
        vijf_min();         // 'five'. It's five minutes past the hour
        over();
        min1();
        break;

      case 7:             // For 5-9 minutes
        vijf_min();         // 'five'. It's five minutes past the hour
        over();
        min2();
        break;

      case 8:             // For 5-9 minutes
        vijf_min();         // 'five'. It's five minutes past the hour
        over();
        min3();
        break;

      case 9:             // For 5-9 minutes
        vijf_min();         // 'five'. It's five minutes past the hour
        over();
        min4();
        break;


      case 10:            // 10-14, 50-54
        tien_min();         // 'ten'. It's ten minutes past the hour
        over();
        break;

      case 11:             // For 5-9 minutes
        tien_min();         // 'five'. It's five minutes past the hour
        over();
        min1();
        break;

      case 12:             // For 5-9 minutes
        tien_min();         // 'five'. It's five minutes past the hour
        over();
        min2();
        break;

      case 13:             // For 5-9 minutes
        tien_min();         // 'five'. It's five minutes past the hour
        over();
        min3();
        break;

      case 14:             // For 5-9 minutes
        tien_min();         // 'five'. It's five minutes past the hour
        over();
        min4();
        break;

      case 15:            //
        kwart();            // 'quarter'
        over();
        break;

      case 16:             // For 5-9 minutes
        kwart();         // 'five'. It's five minutes past the hour
        over();
        min1();
        break;

      case 17:             // For 5-9 minutes
        kwart();         // 'five'. It's five minutes past the hour
        over();
        min2();
        break;

      case 18:             // For 5-9 minutes
        kwart();         // 'five'. It's five minutes past the hour
        over();
        min3();
        break;

      case 19:             // For 5-9 minutes
        kwart();         // 'five'. It's five minutes past the hour
        over();
        min4();
        break;

      case 20:            // 20-24
        tien_min();
        voor();
        half();
        break;

      case 21:             // For 5-9 minutes
        tien_min();
        voor();
        half();
        min1();
        break;

      case 22:             // For 5-9 minutes
        tien_min();
        voor();
        half();
        min2();
        break;

      case 23:             // For 5-9 minutes
        tien_min();
        voor();
        half();
        min3();
        break;

      case 24:             // For 5-9 minutes
        tien_min();
        voor();
        half();
        min4();
        break;

      case 25:
        vijf_min();
        voor();
        half();
        break;

      case 26:             // For 5-9 minutes
        vijf_min();
        voor();
        half();
        min1();
        break;

      case 27:             // For 5-9 minutes
        vijf_min();
        voor();
        half();
        min2();
        break;

      case 28:             // For 5-9 minutes
        vijf_min();
        voor();
        half();
        min3();
        break;

      case 29:             // For 5-9 minutes
        vijf_min();
        voor();
        half();
        min4();
        break;

      case 30:
        half();
        break;

      case 31:             // For 5-9 minutes
        half();
        min1();
        break;

      case 32:             // For 5-9 minutes
        half();
        min2();
        break;

      case 33:             // For 5-9 minutes
        half();
        min3();
        break;

      case 34:             // For 5-9 minutes
        half();
        min4();
        break;

      case 35:
        vijf_min();
        over();
        half();
        break;

      case 36:             // For 5-9 minutes
        vijf_min();
        over();
        half();
        min1();
        break;

      case 37:             // For 5-9 minutes
        vijf_min();
        over();
        half();
        min2();
        break;

      case 38:             // For 5-9 minutes
        vijf_min();
        over();
        half();
        min3();
        break;

      case 39:             // For 5-9 minutes
        vijf_min();
        over();
        half();
        min4();
        break;

      case 40:
        tien_min();          // Twenty minutes past/to hour
        over();
        half();

      case 41:             // For 5-9 minutes
        tien_min();          // Twenty minutes past/to hour
        over();
        half();
        min1();
        break;

      case 42:             // For 5-9 minutes
        tien_min();          // Twenty minutes past/to hour
        over();
        half();
        min2();
        break;

      case 43:             // For 5-9 minutes
        tien_min();          // Twenty minutes past/to hour
        over();
        half();
        min3();
        break;

      case 44:             // For 5-9 minutes
        tien_min();          // Twenty minutes past/to hour
        over();
        half();
        min4();
        break;

      case 45:
        kwart();
        voor();
        break;

      case 46:             // For 5-9 minutes
        kwart();          // Twenty minutes past/to hour
        voor();
        min1();
        break;

      case 47:             // For 5-9 minutes
        kwart();         // Twenty minutes past/to hour
        voor();
        min2();
        break;

      case 48:             // For 5-9 minutes
        kwart();         // Twenty minutes past/to hour
        voor();
        min3();
        break;

      case 49:             // For 5-9 minutes
        kwart();         // Twenty minutes past/to hour
        voor();
        min4();
        break;

      case 50:
        tien_min();
        voor();
        break;

      case 51:             // For 5-9 minutes
        tien_min();          // Twenty minutes past/to hour
        voor();
        min1();
        break;

      case 52:             // For 5-9 minutes
        tien_min();        // Twenty minutes past/to hour
        voor();
        min2();
        break;

      case 53:             // For 5-9 minutes
        tien_min();        // Twenty minutes past/to hour
        voor();
        min3();
        break;

      case 54:             // For 5-9 minutes
        tien_min();        // Twenty minutes past/to hour
        voor();
        min4();
        break;

      case 55:
        vijf_min();
        voor();
        break;

      case 56:             // For 5-9 minutes
        vijf_min();         // Twenty minutes past/to hour
        voor();
        min1();
        break;

      case 57:             // For 5-9 minutes
        vijf_min();        // Twenty minutes past/to hour
        voor();
        min2();
        break;

      case 58:             // For 5-9 minutes
        vijf_min();       // Twenty minutes past/to hour
        voor();
        min3();
        break;

      case 59:             // For 5-9 minutes
        vijf_min();        // Twenty minutes past/to hour
        voor();
        min4();
        break;
    }

    if (m > 19)           // Greater than 20 minutes past the hour so we need to talk about the next hour
      h++;

    uint8_t mod = (h % 12);       // Ensure hour is between 0 and 11 on 24 hour clock
    switch (mod) {
      case 0:
        twaalf();           // Top of the hour
        break;

      case 1:
        een();
        break;

      case 2:
        twee();
        break;

      case 3:
        drie();
        break;

      case 4:
        vier();
        break;

      case 5:
        vijf();
        break;

      case 6:
        zes();
        break;

      case 7:
        zeven();
        break;

      case 8:
        acht();
        break;

      case 9:
        negen();
        break;

      case 10:
        tien();
        break;

      case 11:
        elf();
        break;
    }

    pixels.show();
  }
}


void het_is() {
  pixels.setPixelColor(0, pixelR, pixelG, pixelB);
  pixels.setPixelColor(1, pixelR, pixelG, pixelB);
  pixels.setPixelColor(2, pixelR, pixelG, pixelB);
  pixels.setPixelColor(5, pixelR, pixelG, pixelB);
  pixels.setPixelColor(6, pixelR, pixelG, pixelB);
}

void uur() {
  pixels.setPixelColor(87, pixelR, pixelG, pixelB);
  pixels.setPixelColor(88, pixelR, pixelG, pixelB);
  pixels.setPixelColor(89, pixelR, pixelG, pixelB);
}

void vijf_min() {
  pixels.setPixelColor(15, pixelR, pixelG, pixelB);
  pixels.setPixelColor(16, pixelR, pixelG, pixelB);
  pixels.setPixelColor(17, pixelR, pixelG, pixelB);
  pixels.setPixelColor(18, pixelR, pixelG, pixelB);
}

void tien_min() {
  pixels.setPixelColor(11, pixelR, pixelG, pixelB);
  pixels.setPixelColor(12, pixelR, pixelG, pixelB);
  pixels.setPixelColor(13, pixelR, pixelG, pixelB);
  pixels.setPixelColor(14, pixelR, pixelG, pixelB);
}

void kwart() {
  pixels.setPixelColor(20, pixelR, pixelG, pixelB);
  pixels.setPixelColor(21, pixelR, pixelG, pixelB);
  pixels.setPixelColor(22, pixelR, pixelG, pixelB);
  pixels.setPixelColor(23, pixelR, pixelG, pixelB);
  pixels.setPixelColor(24, pixelR, pixelG, pixelB);
}

void over() {
  pixels.setPixelColor(26, pixelR, pixelG, pixelB);
  pixels.setPixelColor(27, pixelR, pixelG, pixelB);
  pixels.setPixelColor(28, pixelR, pixelG, pixelB);
  pixels.setPixelColor(29, pixelR, pixelG, pixelB);
}

void voor() {
  pixels.setPixelColor(35, pixelR, pixelG, pixelB);
  pixels.setPixelColor(36, pixelR, pixelG, pixelB);
  pixels.setPixelColor(37, pixelR, pixelG, pixelB);
  pixels.setPixelColor(38, pixelR, pixelG, pixelB);
}

void half() {
  pixels.setPixelColor(30, pixelR, pixelG, pixelB);
  pixels.setPixelColor(31, pixelR, pixelG, pixelB);
  pixels.setPixelColor(32, pixelR, pixelG, pixelB);
  pixels.setPixelColor(33, pixelR, pixelG, pixelB);
}

void een() {
  pixels.setPixelColor(80, pixelR, pixelG, pixelB);
  pixels.setPixelColor(81, pixelR, pixelG, pixelB);
  pixels.setPixelColor(82, pixelR, pixelG, pixelB);
}

void twee() {
  pixels.setPixelColor(43, pixelR, pixelG, pixelB);
  pixels.setPixelColor(44, pixelR, pixelG, pixelB);
  pixels.setPixelColor(45, pixelR, pixelG, pixelB);
  pixels.setPixelColor(46, pixelR, pixelG, pixelB);
}

void drie() {
  pixels.setPixelColor(56, pixelR, pixelG, pixelB);
  pixels.setPixelColor(57, pixelR, pixelG, pixelB);
  pixels.setPixelColor(58, pixelR, pixelG, pixelB);
  pixels.setPixelColor(59, pixelR, pixelG, pixelB);
}

void vier() {
  pixels.setPixelColor(76, pixelR, pixelG, pixelB);
  pixels.setPixelColor(77, pixelR, pixelG, pixelB);
  pixels.setPixelColor(78, pixelR, pixelG, pixelB);
  pixels.setPixelColor(79, pixelR, pixelG, pixelB);
}

void vijf() {
  pixels.setPixelColor(83, pixelR, pixelG, pixelB);
  pixels.setPixelColor(84, pixelR, pixelG, pixelB);
  pixels.setPixelColor(85, pixelR, pixelG, pixelB);
  pixels.setPixelColor(86, pixelR, pixelG, pixelB);
}

void zes() {
  pixels.setPixelColor(47, pixelR, pixelG, pixelB);
  pixels.setPixelColor(48, pixelR, pixelG, pixelB);
  pixels.setPixelColor(49, pixelR, pixelG, pixelB);
}

void zeven() {
  pixels.setPixelColor(61, pixelR, pixelG, pixelB);
  pixels.setPixelColor(62, pixelR, pixelG, pixelB);
  pixels.setPixelColor(63, pixelR, pixelG, pixelB);
  pixels.setPixelColor(64, pixelR, pixelG, pixelB);
  pixels.setPixelColor(65, pixelR, pixelG, pixelB);
}

void acht() {
  pixels.setPixelColor(40, pixelR, pixelG, pixelB);
  pixels.setPixelColor(41, pixelR, pixelG, pixelB);
  pixels.setPixelColor(42, pixelR, pixelG, pixelB);
  pixels.setPixelColor(43, pixelR, pixelG, pixelB);
}

void negen() {
  pixels.setPixelColor(65, pixelR, pixelG, pixelB);
  pixels.setPixelColor(66, pixelR, pixelG, pixelB);
  pixels.setPixelColor(67, pixelR, pixelG, pixelB);
  pixels.setPixelColor(68, pixelR, pixelG, pixelB);
  pixels.setPixelColor(69, pixelR, pixelG, pixelB);
}

void tien() {
  pixels.setPixelColor(50, pixelR, pixelG, pixelB);
  pixels.setPixelColor(51, pixelR, pixelG, pixelB);
  pixels.setPixelColor(52, pixelR, pixelG, pixelB);
  pixels.setPixelColor(53, pixelR, pixelG, pixelB);
}

void elf() {
  pixels.setPixelColor(54, pixelR, pixelG, pixelB);
  pixels.setPixelColor(55, pixelR, pixelG, pixelB);
  pixels.setPixelColor(56, pixelR, pixelG, pixelB);
}

void twaalf() {
  pixels.setPixelColor(70, pixelR, pixelG, pixelB);
  pixels.setPixelColor(71, pixelR, pixelG, pixelB);
  pixels.setPixelColor(72, pixelR, pixelG, pixelB);
  pixels.setPixelColor(73, pixelR, pixelG, pixelB);
  pixels.setPixelColor(74, pixelR, pixelG, pixelB);
  pixels.setPixelColor(75, pixelR, pixelG, pixelB);
}

void min1() {
  pixels.setPixelColor(93, pixelR, pixelG, pixelB);
}

void min2() {
  pixels.setPixelColor(92, pixelR, pixelG, pixelB);
  pixels.setPixelColor(93, pixelR, pixelG, pixelB);
}

void min3() {
  pixels.setPixelColor(91, pixelR, pixelG, pixelB);
  pixels.setPixelColor(92, pixelR, pixelG, pixelB);
  pixels.setPixelColor(93, pixelR, pixelG, pixelB);
}

void min4() {
  pixels.setPixelColor(90, pixelR, pixelG, pixelB);
  pixels.setPixelColor(91, pixelR, pixelG, pixelB);
  pixels.setPixelColor(92, pixelR, pixelG, pixelB);
  pixels.setPixelColor(93, pixelR, pixelG, pixelB);
}
[/code]

I hope you can help me with this question. I think the library I'm using might be the problem, but I'm not sure. Please help.

Have you got a link to the library?

All you really need is...

void loop() 
{
  if (minuteButtonPressed)
    // add 1 to the minutes;
  if (hourButtonPressed)
    // add 1 to the hours;
  
  doTime();                                           // Display the updated time
}

Well to be fair this Library was downloaded about 6 years ago and I cant find it online anymore. What do you need? Maybe I can search my files and provide it. Thank you so far!

I'd recommend using something current.

Just use the DS3231 library from the IDE.

Think about using some of the epoch time functions to add time... epoch time is just a number of seconds (since 1/1/1970)... so adding 60 adds a minute, adding 3600 adds an hour.

1 Like

Is it possible to use the Library I currently used and wrote the entire code with and add the IDE library?

Most of your code has very little to do with the RTC... it's mostly about formatting the output.

It should be quite easy to use a different library for the RTC. Most of the functions are the same as the library you're already using.

1 Like

Oh that sounds great, thanks for that input, as you may notice: the code I wrote I did together with a friend of mine that is way more experienced in this than I am.

If I would use the IDE Library for the DS3231 and use the code you gave me with the setup allready in the picture I added. How should I edit the code? Don't get me wrong, It's not laziness, I've been trying to figure this out for weeks now and simply don't know anymore.

What can you "figure out" without any library in hand?

Make it less hypothetical and take some concrete action. Download a good RTC library (I like the JeeLabs one personally), and work through it, load and run the example sketches, have a good read to understand how they work.

Most libraries for the DS3231 around now are much simpler and straightforward to use, than the one you have there.

Then, post your best attempt here. It will demonstrate your willingness to undertake the burden of self learning. That is key to getting quality help here.

Allright, what I have so far is the following:

const int buttonPin = 3 + 4;   // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

and

void setup() {
pinMode(buttonPin, INPUT);                          // initialize the LED pin as an output:
}

and

void loop() {
 // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState 3 == HIGH) {
   
    setMinutes + m);                            // Add 1 minute to minute m:
  } else {
  }

  if (m == lastM) {                             // Has Minute value changed since last time?
    return;                                     // No, so return
  }
  lastM = m;                                    // Set last minute to current minute

   // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState 4 == HIGH) {
   
    setHours + h);                            // Add 1 hour to hour h:
  } else {
  }

  if (h == lastH) {                             // Has Hour value changed since last time?
    return;                                     // No, so return
  }
  lastH = H;                                    // Set last hour to current hour
}

But as said this doesn't work.

Im thinking now is, when adding the DS3231 Library:

#include <DS3231.h>

const int BUTTON1 = 2;
const int BUTTON2 = 3;

void setup() {
#define

  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
}

void loop() {
  {
    BUTTONstate1 = digitalRead(BUTTON1);
    if (BUTTONstate1 == HIGH)
    {
      Time t(+60)
    }
#delay(20)
  }
  BUTTONstate2 = digitalRead(BUTTON2);
  if (BUTTONstate2 == HIGH)
  {
    Time t(+3600)
  }
}

This is as far as I'm getting now, but : no results :frowning: . Where am I going wrong?

Also I see there's no resistors between ground and the button, I think I should add a 1K resistor between both buttons and the ground.

So, you have a pushbutton on pin 7?

1 Like

Where did you find this?

#delay(20)

and this?

No, But it wouldn't let me add both buttons on pin 3 and 4 so I figured this might work (it didn't).

And the delay was something I was trying, but also didn't work. I just dont want to push the button for 50milliseconds and add 43 hours. That is why I tried to add the delay.

What do you mean, "it wouldn't let you add both buttons"? That is very strange. What did you actually do? What happened? Please use specific language from now on. My guess, it wouldn't compile without errors?

Same thing, your second statement about the button, please explain more clearly and with more detail. I suspect you just didn't provide any state change detection, so the response happens multiple times while the button is held down.

When you "added delay" you did it with broken C/C++ language syntax, this is obviously a major problem for you because you are doing it in many places.

Allright, I'll do as good as I can, I'm not trying to be vague.

What I tried to add was:

const int buttonPin = 3;   // the number of the pushbutton pin
const int buttonPin = 4;   // the number of the pushbutton pin

the error I got said:
Exit status 1
redifinition of 'const int buttonPin'

It looks like the image below:
image

I'm not sure on what you mean by 'state change detection'. But what I was trying to achieve is that when I push the first button in port 3 the hour goes up by 1. If I hold down the button it goes up by 1 every 50 milliseconds (or at least It doesn't go up so fast it is still impossible to set the time right. For the button pinnend in port 4 I wanted the same but for minutes (so it goes up by 60 seconds), but also not to quickly so for that reason I also tried to add the delay.

Yes, it is very hard for me to understand, I'm fairly new to this and because I have a job next to school I don't program code every day. I really try to understand where I'm making the mistakes and how to do program this properly.

The error tells you exactly what is wrong. You cannot have two variables with the same name.

Try:

const int buttonPin1 = 3;   // the number of the pushbutton pin
const int buttonPin2 = 4;   // the number of the pushbutton pin
1 Like

A downside to your button approach would be that you are not able to read the time you set. You would need the serial monitor and print the new time to it. Or a added monitor. (forget about this remark as you mentioned it immediately updates the clock, good thinking here Wobbly...)

As far as I know you would also need an additional time library to keep the time on the Arduino itself (after it has retrieved it from the RTC or was set during setup for example). Check TimeLib.h.

You could use a routine during setup that allows you to input the onboard time which you then use to update the RTC. The RTC library might adjust for daylight saving time by itself.

Example routine for setting the onboard time from some of my code (it calls on TimeLib.h):

BeginInputTime:

TimeCode = ""; // this is a String that needs to be declared at the top

Millis = millis();

delay(1000);

Serial.println();
Serial.println(F("Would you like to set the internal program time using a time-code (YYYYMMDDHHMMSS)? If so press 'y' if not press 'n'..."));
Serial.println();
delay(1000);

  while (Serial.available() == 0) {
    if (millis() - Millis >= 20000) {
    delay(1000);
    Serial.println();
    Serial.println(F("20 seconds timed-out... The time was not set..."));
    Serial.println();
    Millis = millis();
    delay(1000);
    goto escapeSetupTime;
    }}
  
  if (Serial.available() > 0) {
     BootInput = Serial.read();
     if (BootInput == 'y') {
     Serial.println();
     Serial.println(F("Please input the fourteen digit time-code:"));
     Serial.println();
     Millis = millis();
     BootInput = ' ';
     delay(1000);
     
      ReInputTime:
      while (Serial.available() == 0) {
      if (millis() - Millis >= 20000) {
      delay(1000);
      Serial.println();
      Serial.println(F("20 seconds timed-out... The new time was not set..."));
      Serial.println();
      Millis = millis();
      delay(1000);
      goto escapeSetupTime;
      }}
      
      if (Serial.available() >= 1) {
      TimeCode = Serial.readString();
      delay(1000);
      Serial.println();
      Serial.println(F("Setting time, please wait..."));
      Serial.println();
      delay(1000);
      
      TimeCodeFirstDigit = TimeCode.charAt(0); // read the chars
      TImeCodeSecondDigit = TimeCode.charAt(1);
      TimeCodeThirdDigit = TimeCode.charAt(2);
      TimeCodeFourthDigit = TimeCode.charAt(3);
      TimeCodeFifthDigit = TimeCode.charAt(4);
      TimeCodeSixthDigit = TimeCode.charAt(5);
      TimeCodeSeventhDigit = TimeCode.charAt(6);
      TimeCodeEigthDigit = TimeCode.charAt(7);
      TimeCodeNinthDigit = TimeCode.charAt(8);
      TimeCodeTenthDigit = TimeCode.charAt(9);
      TimeCodeEleventhDigit = TimeCode.charAt(10);
      TimeCodeTwelvethDigit = TimeCode.charAt(11);
      TimeCodeThirteenthDigit = TimeCode.charAt(12);
      TimeCodeFourteenthDigit = TimeCode.charAt(13);

      YearString.concat(TimeCodeFirstDigit);
      YearString.concat(TImeCodeSecondDigit);
      YearString.concat(TimeCodeThirdDigit);
      YearString.concat(TimeCodeFourthDigit);
      MonthString.concat(TimeCodeFifthDigit);
      MonthString.concat(TimeCodeSixthDigit);
      DayString.concat(TimeCodeSeventhDigit); 
      DayString.concat(TimeCodeEigthDigit); 
      HourString.concat(TimeCodeNinthDigit);
      HourString.concat(TimeCodeTenthDigit); 
      MinuteString.concat(TimeCodeEleventhDigit);
      MinuteString.concat(TimeCodeTwelvethDigit);
      SecondString.concat(TimeCodeThirteenthDigit);
      SecondString.concat(TimeCodeFourteenthDigit);

      Year = YearString.toInt(); // declare all these integers like 'Year' at the top
      Month = MonthString.toInt();
      Day = DayString.toInt(); 
      Hour = HourString.toInt(); 
      Minute = MinuteString.toInt(); 
      Second = SecondString.toInt();
       
      TimeCodeFirstDigit = ' '; // declare these all these chars at the top
      TImeCodeSecondDigit = ' ';
      TimeCodeThirdDigit = ' ';
      TimeCodeFourthDigit = ' ';
      TimeCodeFifthDigit = ' ';
      TimeCodeSixthDigit = ' ';
      TimeCodeSeventhDigit = ' ';
      TimeCodeEigthDigit = ' ';
      TimeCodeNinthDigit = ' ';
      TimeCodeTenthDigit = ' ';
      TimeCodeEleventhDigit = ' ';
      TimeCodeTwelvethDigit = ' ';
      TimeCodeThirteenthDigit = ' ';
      TimeCodeFourteenthDigit = ' ';
        
      YearString = ""; // declare all these strings at the top
      MonthString  = "";
      DayString  = ""; 
      HourString  = ""; 
      MinuteString  = ""; 
      SecondString  = "";
      
      setTime(Hour,Minute,Second,Day,Month,Year); 

      delay(3000);
      
      Serial.println();
      Serial.print(F("The following time-code was received: "));
      Serial.println(TimeCode);
      Serial.println();
      Serial.print(F("Which makes the new time and date: "));
      Serial.print(hour());
      Serial.print(F(":"));
      Serial.print(minute());
      Serial.print(F(":"));
      Serial.print(second());
      Serial.print(F(" on day "));
      Serial.print(day());
      Serial.print(F(" in monthnumber "));
      Serial.print(month());
      Serial.print(F(" of the year "));
      Serial.println(year());
      Serial.println();

      Year = 0;
      Month = 0;
      Day = 0; 
      Hour = 0; 
      Minute = 0; 
      Second = 0;
      
      Millis = millis();
      delay(1000);
      goto escapeSetupTime;
      }
     }

     if (BootInput == 'n') {
      Serial.println();
      Serial.println(F("Proceeding without setting the time... "));
      Serial.println();
      Millis = millis();
      delay(1000);
      BootInput = ' ';
      goto escapeSetupTime;
      }

     if (BootInput != 'n' && BootInput != 'y') {
      Serial.println();
      Serial.println(F("Input unknown, please try again..."));
      Serial.println();
      Millis = millis();
      BootInput = ' ';
      Serial.flush();
      delay(3000);
      goto BeginInputTime;
      }

}

escapeSetupTime:

And do not forget to declare the following variables at the top of your code:

String TimeCode = "";

char TimeCodeFirstDigit = ' ';
char TImeCodeSecondDigit =  ' ';
char TimeCodeThirdDigit =  ' ';
char TimeCodeFourthDigit =  ' ';
char TimeCodeFifthDigit =  ' ';
char TimeCodeSixthDigit =  ' ';
char TimeCodeSeventhDigit = ' ';
char TimeCodeEigthDigit = ' ';
char TimeCodeNinthDigit = ' ';
char TimeCodeTenthDigit = ' ';
char TimeCodeEleventhDigit =  ' ';
char TimeCodeTwelvethDigit =  ' ';
char TimeCodeThirteenthDigit =  ' ';
char TimeCodeFourteenthDigit =  ' ';

String YearString = "";
String MonthString  = "";
String DayString  = ""; 
String HourString  = ""; 
String MinuteString  = ""; 
String SecondString  = "";

int Year = 0;
int Month = 0;
int Day = 0; 
int Hour = 0; 
int Minute = 0; 
int Second = 0;

At some events you would need to retrieve the current time from the RTC (after a power-off for instance). You would probably also do this during setup. For instance: if no time was inputted through the serial monitor during setup then the code would retrieve it automatically from the RTC (use a boolean to mark if the input of time has occured for instance).

Good luck and please do continue with the button approach if that is what you want to do (or do both).

1 Like

Oh wow, this simple, thanks that works. I did not know I can actually set numbers to buttonPin. I'm going to try to figure out if I can use this to change the time, thanks!

Hmm, this looks really complex to me. I'm also not sure how to interpret all the code you are showing me here. To give you a little more context about why I'm trying to add buttons is that the assignment is to make a product that could be sold. So it really doesn't matter what I make (or design) it should be accessible to a 'normal' consumer. If the product has to be taken apart to alter the time every time it has been too long without power or when it's summer or winter time it's not accessible enough. I think the professor might cut me some slack if I wouldn't add the buttons because I already am making it myself so much harder than I actually should, but I just want to make it as if I could actually sell it (which I won't).

Ok, you do what you need to do.

I see you want to get the time directly from the RTC each time, you can do that.

And the time you set would be immediately seen on the clock (I am no expert on Neopixels)?

Remember that if the battery dies on your RTC you can not input or read time anymore if you cut out the use of a time library like TimeLib.h, the clock would become useless.

Precicely. WS 2812B to be exact. I could show you what it looks like on a wooden plate and with the cover on (so you can see the letters) if you like.