RTC Changing Time

I want to add a button to my project that changes the time (add 1 hour). I am using a DS3231 RTC. Thanks for helping

Don't you think also that we should see your code? And don't forget the code tags (the </> button in the editor).

Are you stuck reading the button or setting the clock time ?

I have the button set up and working but I just don't know how to make it add 1 hour.

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

RTC_DS1307 RTC;             


#define LED_Loop 60
#define PIN 8    


Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_Loop, PIN, NEO_GRB + NEO_KHZ800);
const int ButtonPin1 = 9;
int LED[LED_Loop];
int THREE = (LED_Loop / 4);
int SIX = (THREE * 2);
int NINE = (THREE * 3);
int TWELVE = (LED_Loop-1);

int HR_Fade = 7;
int MN_Fade;

long HR_Colour;
long SE_Colour = 0x000055;

long THIS_LED;
int Led_Flag;
int argh;

int HR_R;
int HR_G;
int HR_B;

int HR1_R = 0x55;
int HR1_G = 0;
int HR1_B = 0;

int HR2_R = 0x0D;
int HR2_G = 0;
int HR2_B = 0x0D;

int MN_R = 0;
int MN_G = 33;
int MN_B = 0;

//int SE_R = 0;
//int SE_G = 0;
//int SE_B = 0x55;

int hour_led;
int minute_led;
int second_led;
int new_minute;

void setup() 
{
  pinMode (ButtonPin1, INPUT);
  
  delay(2000);          
  Serial.begin(9600);
  Serial.println("-------------------------------");
  Serial.println("Setting up");
  
  Wire.begin();
  strip.begin();
  strip.show();


  if (! RTC.isrunning()) 
  {
    Serial.println("RTC is NOT running!");
    RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  Serial.println("Done setting up");
  Serial.println("-------------------------------");
}

void loop() 
{
  int ButtonState = 0;
  

  ButtonState = digitalRead(ButtonPin1);
   DateTime now = RTC.now();
   
   strip.show();
   
   int hr = now.hour();
  
      
      
    
    
 
   if (hr < 12)
   {
     HR_R = HR1_R;
     HR_G = HR1_G;
     HR_B = HR1_B;
   }
   else
   {
     HR_R = HR2_R;
     HR_G = HR2_G;
     HR_B = HR2_B;
   }
   
   int mins = now.minute();        
   second_led = now.second();


   hour_led = (((LED_Loop/12) * hr) + (mins / (LED_Loop/5)))%LED_Loop;
   if (hour_led == 60)
   {
     hour_led = 59;
   }

   minute_led = mins;


   strip.setPixelColor(second_led-1,SE_Colour/2);
   strip.setPixelColor(second_led-2,SE_Colour/4);
   strip.setPixelColor(second_led-3,SE_Colour/8);
   strip.setPixelColor(second_led-4,SE_Colour/16);
   strip.setPixelColor(second_led-5,0);




   if (second_led == 0)
   {
      strip.setPixelColor(LED_Loop-1, SE_Colour/2);
      strip.setPixelColor(LED_Loop-2,SE_Colour/4);
      strip.setPixelColor(LED_Loop-3,SE_Colour/8);
      strip.setPixelColor(LED_Loop-4,SE_Colour/16);
      strip.setPixelColor(LED_Loop-5,0);
      new_minute = 1;
   }
   if (second_led == 1)
   {
      strip.setPixelColor(second_led-1, SE_Colour/2);
      strip.setPixelColor(LED_Loop-1, SE_Colour/4);
      strip.setPixelColor(LED_Loop-2,SE_Colour/8);
      strip.setPixelColor(LED_Loop-3,SE_Colour/16);
      strip.setPixelColor(LED_Loop-4,0);
   }
   if (second_led == 2)
   {
      strip.setPixelColor(second_led-1, SE_Colour/2);
      strip.setPixelColor(second_led-2, SE_Colour/4);
      strip.setPixelColor(LED_Loop-1, SE_Colour/8);
      strip.setPixelColor(LED_Loop-2,SE_Colour/16);
      strip.setPixelColor(LED_Loop-3,0);
   }
   if (second_led == 3)
   {
      strip.setPixelColor(second_led-1, SE_Colour/2);
      strip.setPixelColor(second_led-2, SE_Colour/4);
      strip.setPixelColor(second_led-3, SE_Colour/8);
      strip.setPixelColor(LED_Loop-1,SE_Colour/16);
      strip.setPixelColor(LED_Loop-2,0);
   }
   if (second_led == 4)
   {
      strip.setPixelColor(second_led-1, SE_Colour/2);
      strip.setPixelColor(second_led-2, SE_Colour/4);
      strip.setPixelColor(second_led-3, SE_Colour/8);
      strip.setPixelColor(second_led-4,SE_Colour/16);
      strip.setPixelColor(LED_Loop-1,0);
   }

   strip.setPixelColor(second_led,SE_Colour);

   if (new_minute == 1)

   strip.setPixelColor(minute_led,MN_R,MN_G,MN_B);
   strip.setPixelColor(minute_led+1, MN_R,     (MN_G * (second_led*10/6)/100)      , MN_B);
   strip.setPixelColor(minute_led-1, MN_R,     (MN_G * (100-(second_led*10/6))/100)      , MN_B);
   strip.setPixelColor(hour_led,HR_R,HR_G,HR_B);
   strip.setPixelColor((hour_led-1)%LED_Loop,HR_R/HR_Fade,HR_G,HR_B/HR_Fade);
   strip.setPixelColor((hour_led+1)%LED_Loop,HR_R/HR_Fade,HR_G,HR_B/HR_Fade);

   if (second_led > minute_led)
   {
     new_minute = 0;
   }



  
 
  
  strip.show();
  
  
}

1. Poll the Button in every 200 - 250 ms in the middle of displaying the RTC time.
2. If the Button is active: Read BCD formatted Hours from the Hours Register (address 02h) of RTC; convert it into normal decimal (natural binary); add 1 with it; convert it to BCD; write the new value into Hours Register of the RTC.

3. This link may be helpful for you.

if (ButtonState == <the state you expect>) { // might need some debouncing
  now += TimeSpan(3600);
  RTC.adjust(now);
}

The code is incomplete because I don't know your wiring and most buttons need debouncing to work correctly.

Library is good; it solves the problem in a minute. Someday, Adafruit or other respected individuals might discontinue the supplies of the Library Functions. Therefore, based on Arduino's basic functions (like Wire.beginTransmission(), Wire.write(), Wire.endTransmission(), Wire.requestFrom(), Wire.read()), we may try writing our own codes standing on one level lower platform. This way, we may slowly acquire the ability of writing Register Level instructions that might open opportunity to understand the 'System'.

Someday, Adafruit or other respected individuals might discontinue the supplies of the Library Functions.

Someday Arduino might discontinue the supply of the Arduino IDE. Is that a reason not to use the function because we don't learn to use the registers? With the same arguments you can force the abandonment of the compiler and tell people to hand code the machine language bytes.

If Adafruit discontinues the supply of the library there are thousands of other developers out there that might take the maintainer job for it. As long as we have the source code I don't see any reason not to use a library that is doing what it should.

One question at you GolamMostafa: Are you using Windows or MacOS X? What if Microsoft discontiues the supply of Windows or Apple discontinues the supply of MacOS X? Do you see why your sentence is definitely wrong?

GolamMostafa:
Library is good; it solves the problem in a minute. Someday, Adafruit or other respected individuals might discontinue the supplies of the Library Functions. Therefore, based on Arduino's basic functions (like Wire.beginTransmission(), Wire.write(), Wire.endTransmission(), Wire.requestFrom(), Wire.read()), we may try writing our own codes standing on one level lower platform. This way, we may slowly acquire the ability of writing Register Level instructions that might open opportunity to understand the 'System'.

Well, the RTC is a relatively easy piece of hardware to play with, however it requires getting used to understanding the registers and the datasheet thoroughly. Sometimes you have to do things in a particular order. Also to be complete you should have at least some conversion routines to and from epoch time. At least, if you are expecting to do complex calendar time calculations.

I keep and archive all the libraries I have downloaded and used! :slight_smile:

My intentions are objective and not subjective at all. When I have said 'we', I have wanted to mean those learners who are at my level of 'Learning Phase'. I am conversant on the programming expertise level of pylon, Delta_G, aarg, and others; there is absolutely not a bit of thought to undermine their dignities.

The criticisms/comments have been very very strong/pleasant; but, I have learnt to consume them as there is nothing such as personal in a Forum!

Thanks for the replies. I tried pylon's code and I get the error: no match for 'operator+=' (operand types are 'DateTime' and 'TimeSpan')

Not sure what to do here?

tsouthwell:
I have the button set up and working[...]

Not in that code.

Thanks for the replies. I tried pylon's code and I get the error: no match for 'operator+=' (operand types are 'DateTime' and 'TimeSpan')

Excuse me, I expected the compiler to do a better job :).

Replace the line

now += TimeSpan(3600);

by

now = now + TimeSpan(3600);

which is the same but here the compiler has the exact operator for matching.