DS3231 setting time

I’m stuck setting the time on DS3231.

I have 2 buttons (hour and minutes)
Issue, when pressing buttons, time changes and increments correctly.
When button is released time goes back to 12:00

How do I program this so DS3231 remembers time I changed. Thank you for help.

#include <Wire.h>

// I2C address
#define DS1307 B1101000

// pinout for the 7 segment display
// Starts top center, clock-wise, then
// center line, then dot
byte segment[] = {
  3, 2, 5, 6, 7, 8, 9, 4
};

byte second = 0, minute = 0, hour = 0;

int led1 = 3;
int led5 = 6;
int led8 = 9;


void updatetime();
void printtime();
void print7digit(byte number);
byte DECTOBCD(byte val);
byte BCDTODEC(byte val);

// constants won't change. They're used here to
// set pin numbers:
const int buttonPinH = 12;     // Pin 12 pushbutton pin for HOURS
const int buttonPinM = 11;     // Pin 11 pushbutton pin for MINUTES

int buttonStateH = 0;         // variable for reading the pushbutton status
int buttonStateM = 0;         // variable for reading the pushbutton status

void setup() {
  byte i;
  Wire.begin();

  for (i = 0; i < 8; i++) {
    pinMode(segment[i], OUTPUT);
    digitalWrite(segment[i], LOW);
    pinMode(buttonPinH, INPUT);
    pinMode(buttonPinM, INPUT);
  }
}


void loop() {
  updatetime();
  printtime();

  //  digitalWrite(led1, HIGH); // DASH
  // delay(300);
  // digitalWrite(led1, LOW);
  // delay(300);
  digitalWrite(led8, HIGH); // DASH
  delay(300);
  digitalWrite(led8, LOW);
  delay(300);
  //digitalWrite(led5, HIGH); // DASH
  //delay(300);
  //digitalWrite(led5, LOW);
  //delay(300);
}

// Pull current time off the DS1307
void updatetime() {
  Wire.beginTransmission(DS1307);
  Wire.write(0x00);
  Wire.endTransmission();
  Wire.requestFrom(DS1307, 3);
  buttonStateH = digitalRead(buttonPinH);
  buttonStateM = digitalRead(buttonPinM);
  // check if the hour pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonStateH == HIGH) {
    hour   = hour + 1 ;
  }
  // check if the minute pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonStateM == HIGH) {
    minute   = minute + 1 ;
  }
  else {
  // Don't actually use seconds value. Feel free to.
    second = BCDTODEC(Wire.read() & 0x7f); 
    minute = BCDTODEC(Wire.read());
    hour   = BCDTODEC(Wire.read() & 0x3f);
    }
}

// Flash each digit of the time in turn on the 7 segment
void printtime() {
  byte j;
  // Convert from 24 hour to 12 hour mode
  // Should be done on the DS1307. I'm sorry.
  byte temphour = hour % 12;
  if (temphour == 0) {
    // Not zero based, not one based, but 12 based.
    temphour = 12;
  }

  // Only display highest digit if it's nonzero
  if (temphour > 9) {
    print7digit(temphour / 10);
    delay(500); // Repeated magic values in my code? Never!
    for (j = 0; j < 8; j++) {
      digitalWrite(segment[j], LOW);
    }
    delay(150);
  }
  print7digit(temphour % 10);
  // Light the dot on last hour digit
  digitalWrite(segment[7], HIGH);
  delay(500);
  for (j = 0; j < 8; j++) {
    digitalWrite(segment[j], LOW);
  }
  delay(500);

  print7digit(minute / 10);
  delay(500);
  for (j = 0; j < 8; j++) {
    digitalWrite(segment[j], LOW);
  }
  delay(150);
  print7digit(minute % 10);
  delay(500);
  for (j = 0; j < 8; j++) {
    digitalWrite(segment[j], LOW);
  }
  delay(2000);
}

// Display a single digit on the 7 segment
void print7digit(byte number) {
  // Bitfield for digits 0-9
  const byte numtable[] = {
    B00111111,
    B00000110,
    B01011011,
    B01001111,
    B01100110,
    B01101101,
    B01111101,
    B00000111,
    B01111111,
    B01100111
  };
  byte litpins = numtable[number];
  byte i;
  for (i = 0; i < 8; i++) {
    digitalWrite(segment[i], (litpins >> i) & 1);
  }
}

byte DECTOBCD(byte val) {
  return ((val / 10) << 4) + (val % 10);
}
byte BCDTODEC(byte val) {
  return (val >> 4) * 10 + (val & 0xf);
}

Your code is referring to a DS1307. As it might make a difference, first question is what you're using?

Using a library will make life easier; if this is an exercise to learn about I2C, I would download the datasheet of the DSxxxx that you're using. I would also download a library and check how it's done.

Both DS1307 and DS3231 use same coding.

I think this part is put in the wrong place. I am new to arduino and coding and have no idea where or how this function should be used.

if (buttonStateH == HIGH) {
    hour   = hour + 1 ;
  }

the buttons work, adjust time, problem is that after adjusting time DS3231 goes back to it's initial setting and forgets new time.

the buttons work, adjust time, problem is that after adjusting time DS3231 goes back to it's initial setting and forgets new time.

That because there is nothing in your code to write the adjusted time back to the registers of the DS3231.

Using a library will make life easier; if this is an exercise to learn about I2C, I would download the datasheet of the DSxxxx that you're using. I would also download a library and check how it's done.

There are also plenty of examples/tutorials on the internet of how to read and write to an DS3231/DS1307 RTC without any library other than Wire.h.

http://bildr.org/2011/03/ds1307-arduino/
works with DS3231. No RTC library required.