Setting DS3232 time

Is there a piece of code I can put into an if statement to make the current hour of the RTC go up by 1. The current way to set the clock is via serial with this:

if (Serial.available() >= 12) {
        // note that the tmElements_t Year member is an offset from 1970,
        // but the RTC wants the last two digits of the calendar year.
        // use the convenience macros from the Time Library to do the conversions.
        int y = Serial.parseInt();
        if (y >= 100 && y < 1000)
            Serial << F("Error: Year must be two digits or four digits!") << endl;
        else {
            if (y >= 1000)
                tm.Year = CalendarYrToTm(y);
            else    // (y < 100)
                tm.Year = y2kYearToTm(y);
            tm.Month = Serial.parseInt();
            tm.Day = Serial.parseInt();
            tm.Hour = Serial.parseInt();
            tm.Minute = Serial.parseInt();
            tm.Second = Serial.parseInt();
            t = makeTime(tm);
            RTC.set(t);        // use the time_t value to ensure correct weekday is set
            setTime(t);
            Serial << F("RTC set to: ");
            printDateTime(t);
            Serial << endl;
            // dump any extraneous input
            while (Serial.available() > 0) Serial.read();
        }
    }

But I don't know what that means

It waits until you have 12 characters in the serial buffer and when you do tries to decode something formatted like this: YYYY MM DD HH MM SS or YY MM DD HH MM SS for example [color=blue]2019 11 30 10 35 33[/color]

This is a poor way to handle the serial buffer (for example here i need more than 12 characters, so the code tries to second guess the timing or will have to wait), I would suggest to study Serial Input Basics to handle this

What I need is something to put inside an if statement to just increment the hour without affecting the rest of the set time.

You read the time data you increment the hour and you put the data back in...

I got it working now the solution was to add this.

setTime((hour(t) + 1), minute(t), second(t), day(t), month(t), year(t));

TuckTin32:
I got it working now the solution was to add this.

setTime((hour(t) + 1), minute(t), second(t), day(t), month(t), year(t));

What happens if you increment:

  • From 2300 to 0000?
  • Past the end of the month?
  • Past the end of the year?

Instead, I'd add 3600 seconds to the Epoch Time.

I use the functions below in a few of my different clock projects.

handleInput() reads the serial port for a command.

The ChangeYear(), ChangeMonth(), etc... functions adjust the date/time parameters.

And, showMenu() sends a list of the commands to the serial port.

Hope this helps.

#include <DS3232RTC.h>        // http://github.com/JChristensen/DS3232RTC
#include <Time.h>

const byte daysInMonth[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};


void handleInput()
{
  char c;

  if (Serial.available() > 0)
  {
    c = Serial.read();

    if (c > 0)
    {
      Serial.println(c);

      switch (c)
      {
         case '\r':
          return;
          break;
                   
         case '\n':
          return;
          break;

         case '!':
          dumptime();
          break;
                  
         case 'M':
          ChangeMonth(true);
          Serial.println(F("Month UP:"));
          break;

         case 'm':
          ChangeMonth(false);
          Serial.println(F("Month DOWN:"));
          break;          

        case 'Y':
          ChangeYear(true);
          Serial.println(F("Year UP:"));
          break;

        case 'y':
          ChangeYear(false);
          Serial.println(F("Year DOWN:"));
          break;

        case 'D':
          ChangeDay(true);
          Serial.println(F("Days UP:"));
          break;  

        case 'd':
          ChangeDay(false);
          Serial.println(F("Days DOWN:"));
          break;   

        case 'H':
          ChangeHour(true);
          Serial.println(F("Hours UP:"));
          break; 

        case 'h':
          ChangeHour(false);
          Serial.println(F("Hours DOWN:"));
          break; 

        case 'N':
          ChangeMinute(true);
          Serial.println(F("Minutes UP:"));
          break; 

        case 'n':
          ChangeMinute(false);
          Serial.println(F("Minutes DOWN:"));
          break; 

        case 'S':
          ChangeSeconds(true);
          Serial.println(F("Seconds UP:"));
          break;

        case 's':
          ChangeMinute(false);
          Serial.println(F("Seconds DOWN:"));
          break;

        case 'O':
          localOffset += 3600;
          Serial.print(F("Offset up one hour:  "));
          PrintOffset();
          break;

        case 'o':
          localOffset -= 3600;
          Serial.print(F("Offset down one hour:  "));
          PrintOffset();
          break;                                                                                                                   
            
        default:
          Serial.println(F("Invalid Command:"));
          ShowMenu();          
          break;
      }

      Serial.println("");
      Serial.println(F("Clock is ready for input:"));
      Serial.print(F("> "));
    }
  }
}


void ChangeYear(boolean b)
{
  time_t t;
  tmElements_t tm;
  uint16_t y;

  RTC.read(tm);

  y = tm.Year;
  y = tmYearToCalendar(y);

  if (b)
  {
    y++;

    if (y > 2099)
    {
      y = 2000;
    }
  }
  else
  {
    if (y > 2000)
    {
      y--;
    }
    else
    {
      y = 2099;
    }
  }
 
  tm.Year = CalendarYrToTm(y);
  t = makeTime(tm);
  RTC.set(t);
  setTime(t);  
}


void ChangeMonth(boolean b)
{
  time_t t;
  tmElements_t tm;  
  byte m;

  RTC.read(tm);

  m = tm.Month;

  if (b)
  {
    m++;

    if (m > 12)
    {
      m = 1;
    }
  }
  else
  {
    if (m > 1)
    {
      m--;
    }
    else
    {
      m = 12;
    }
  }

  tm.Month = m;
  t = makeTime(tm);
  RTC.set(t);
  setTime(t);
}


void ChangeDay(boolean b)
{
  time_t t;  
  tmElements_t tm;
  byte d;

  RTC.read(tm);

  d = tm.Day;

  if (b)
  {
    d++;

    if (d > daysInMonth[tm.Month-1])
    {
      d = 1;
    }
  }
  else
  {
    if (d > 1)
    {
      d--;
    }
    else
    {
      d = daysInMonth[tm.Month-1];
    }
  }

  tm.Day = d;
  t = makeTime(tm);
  RTC.set(t);
  setTime(t);
}


void ChangeHour(boolean b)
{
  time_t t;    
  tmElements_t tm;
  byte h;

  RTC.read(tm);

  h = tm.Hour;

  if (b)
  {
    h++;

    if (h > 23)
    {
      h = 0;
    }
  }
  else
  {
    if (h > 0)
    {
      h--;
    }
    else
    {
      h = 23;
    }
  }
  
  tm.Hour = h;
  t = makeTime(tm);
  RTC.set(t);
  setTime(t);
}


void ChangeMinute(boolean b)
{
  time_t t;  
  tmElements_t tm;
  byte n;

  RTC.read(tm);

  n = tm.Minute;

  if (b)
  {
    n++;

    if (n > 59)
    {
      n = 0;
    }
  }
  else
  {
    if (n > 0)
    {
      n--;
    }
    else
    {
      n = 59;
    }
  }

  tm.Minute = n;
  t = makeTime(tm);
  RTC.set(t);
  setTime(t);
}


void ChangeSeconds(boolean b)
{
  time_t t;    
  tmElements_t tm;
  byte s;

  RTC.read(tm);

  s = tm.Second;

  if (b)
  {
    s++;

    if (s > 59)
    {
      s = 0;
    }
  }
  else
  {
    if (s > 0)
    {
      s--;
    }
    else
    {
      s = 59;
    }
  }

  tm.Second = s;
  t = makeTime(tm);
  RTC.set(t);
  setTime(t);
}


void ShowMenu(void)
{
  Serial.println(F(""));
  Serial.println(F(""));
  PrintOffset();
  Serial.println(F(""));   
  Serial.println(F("To set the time, use the following keys:"));
  Serial.println(F(""));
  Serial.println(F("   Y = Increase Year"));
  Serial.println(F("   y = Decrease Year"));
  Serial.println(F(""));
  Serial.println(F("   M = Increase Month"));
  Serial.println(F("   m = Decrease Month"));
  Serial.println(F(""));
  Serial.println(F("   D = Increase Day"));
  Serial.println(F("   d = Decrease Day"));
  Serial.println(F(""));
  Serial.println(F("   H = Increase Hour"));
  Serial.println(F("   h = Decrease Hour"));
  Serial.println(F(""));
  Serial.println(F("   N = Increase Minute"));
  Serial.println(F("   n = Decrease Minute"));
  Serial.println(F(""));
  Serial.println(F("   S = Increase Seconds"));  
  Serial.println(F("   s = Decrease Seconds"));
  Serial.println(F(""));
  Serial.println(F("   O = Increase Offset one hour"));  
  Serial.println(F("   o = Decrease Offset one hour"));  
  Serial.println(F(""));
  Serial.println(F(""));
  Serial.println(F("   ! = Break out time into time structure elements"));
  Serial.println(F(""));
  Serial.println(F(""));    
  Serial.println(F("============================================="));  
  Serial.println(F(""));
}