Why does DS1307 Resets

Dear Arduino community,

I am new to Arduino and learning the hardware and software, recently I made a clock with Arduino Nano, DS1307 and SSD1306 display using standard Wire, RTC1307, Time and U8glib. Although the display and DS1307 work flawlessly, however, when I added the button code the clock display starts at 30 seconds and then resets to 30 seconds again when it reaches 59 seconds. The code is mentioned below, and I would appreciate if experts here could suggest how to fix it.

#include "U8glib.h"  // OLED
#include <Wire.h>  // I2C
#include "Time.h"  // Time Manipulation
#include "DS1307RTC.h"  // DS1307 RTC
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527

char timebuf[10];  // Time
char datebuf[10];  // Date
int year2digit;  // 2 digit year
int year4digit;  // 4 digit year

const int buttonPin = 3;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status

void setDateTime(){

  byte second =      30; //0-59
  byte minute =      35; //0-59
  byte hour =        19; //0-23
  byte weekDay =     2; //1-7
  byte monthDay =    16; //1-31
  byte month =       8; //1-12
  byte year  =       16; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start 

  Wire.endTransmission();
}

byte decToBcd(byte val){
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
  return ( (val/16*10) + (val%16) );
}

//U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9, 8);  // D0=13, D1=11, CS=10, DC=9, Reset=8
U8GLIB_SSD1306_128X64 u8g(10, 9); // MRA's Settings


void draw(void) {
  u8g.setFont(u8g_font_fur11);  // select font
  u8g.setPrintPos(5, 30);  // set position for displaying time
  u8g.print(timebuf);  // display time
  u8g.setPrintPos(5, 55);  // set position for displaying date
  u8g.print(datebuf);  // display date
  
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

void setup(void) {
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  while (!Serial) ; 
  delay(200);
  Serial.println("DS1307RTC Read Test");
  Serial.println("-------------------");
}

void loop(void) {

  tmElements_t tm;
    if (RTC.read(tm)) {
     year2digit = tm.Year - 30;  // 2 digit year variable
    Serial.print("Ok, Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
      
      sprintf(timebuf, "%02d:%02d:%02d",tm.Hour, tm.Minute, tm.Second); // format time
      sprintf(datebuf, "%02d/%02d/%02d",tm.Day, tm.Month, year2digit);  // format date
  
      u8g.firstPage();  // Put information on OLED
      do {
        draw();
      } while( u8g.nextPage() );
  
  }
  else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
  delay(9000);  // Delay of 1sec
}
/*
 {
   buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
     setDateTime();
  } else {buttonState==HIGH;
  }
}
*/
 delay(1000);
}

When I un-check this code from the loop the clock resets.

/*
{
   buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
     setDateTime();
  } else {buttonState==HIGH;
  }
}
*/

Buttons "bounce". You need to check whether the button is pressed (LOW) and wait till it is released (HIGH) for a some short period, before taking any action.

This:

else {buttonState==HIGH;
  }

doesn't do anything useful.

I tried the suggestion by un-checking below mentioned code, adding delya, removing else statement and now I get a new problem, code resets to 30 seconds after every two seconds:

{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    delay(500);
    setDateTime();
  } 
}
pinMode(buttonPin, INPUT);
if (buttonState == LOW)

How is the buttonPin wired? If you are not using an external pullup, I think it may be floating.

I suggest that you use

pinMode(buttonPin, INPUT_PULLUP);[/code
and wire diagonally across the button, one wire to buttonPin and the other to GND.

If you are not using an external pullup, I think it may be floating.

Thanks a lot. Your suggestion has solved my problem; in conclusion I would say it is better to use internal / external pullup otherwise the input pin will keep floating.