Arduino is blocked or reset please help

trying to make a multifunction button.
When you press the switch, shortly after it blocked or automatically reset

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

#include <LiquidCrystal_I2C.h>

int x = 0;
int gumb = 0;

const int buttonPin = 9;

int buttonState = 0;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
Serial.begin(9600);

lcd.begin();
Serial.println(“start”);

}

void loop() {

lcd.setCursor(0, 1);
tmElements_t tm;
RTC.read™;

lcd.print(tm.Hour);
lcd.print(’:’);
lcd.print(tm.Minute);
lcd.print(’:’);
lcd.print(tm.Second);
lcd.print(" “);
lcd.print(tm.Day);
lcd.print(’/’);
lcd.print(tm.Month);
lcd.print(” ");

gumbo();
preklop();

}
void preklop(){

if(gumb == 1) {
osnova();
}

if(gumb == 2) {
tehtnica();
}

}

void gumbo(){

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {

x++;

Serial.println(x);

}
else
{
if (16 < x){
gumb = 1;
}
if (16 > x && x > 1){
gumb = 2;
}
if (x != 0){
Serial.println(x);
}
x=0;
}

}

void osnova(){

lcd.setCursor(0, 0);
lcd.print(“test 1”);
// Serial.print(“test 1”);
Serial.println(gumb);

lcd.setCursor(0, 1);
tmElements_t tm;
RTC.read™;

lcd.print(tm.Hour);
lcd.print(’:’);
lcd.print(tm.Minute);
lcd.print(’:’);
lcd.print(tm.Second);
lcd.print(" “);
lcd.print(tm.Day);
lcd.print(’/’);
lcd.print(tm.Month);
lcd.print(” ");

gumbo();
preklop();
}

void tehtnica(){
lcd.setCursor(0, 0);
lcd.print(“test 2”);
// Serial.print(“test 2”);
Serial.println(gumb);

lcd.setCursor(0, 1);
tmElements_t tm;
RTC.read™;

lcd.print(tm.Hour);
lcd.print(’:’);
lcd.print(tm.Minute);
lcd.print(’:’);
lcd.print(tm.Second);
lcd.print(" “);
lcd.print(tm.Day);
lcd.print(’/’);
lcd.print(tm.Month);
lcd.print(” ");

gumbo();
preklop();
}

void ura(){
// lcd.setCursor(0, 0);
// lcd.print(" ");

lcd.setCursor(0, 1);
tmElements_t tm;
RTC.read™;

lcd.print(tm.Hour);
lcd.print(’:’);
lcd.print(tm.Minute);
lcd.print(’:’);
lcd.print(tm.Second);
lcd.print(" “);
lcd.print(tm.Day);
lcd.print(’/’);
lcd.print(tm.Month);
lcd.print(” ");

}

Hi podgana -

Put these calls into a function, which will make your code shorter and easier to read .
You must check the type of variable tm, which I assume is an Integer, and I could be wrong.

void printUraLCD (int tm)
{
lcd.print(tm.Hour);
lcd.print(’:’);
lcd.print(tm.Minute);
lcd.print(’:’);
lcd.print(tm.Second);
lcd.print(" “);
lcd.print(tm.Day);
lcd.print(’/’);
lcd.print(tm.Month);
lcd.print(” ");
}

Then make a call to it using its name and the tm parameter from within your other functions :
printUraLCD™;

Finally throw out blanks an blank lines.
Now it is much easier to overlook and read :

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <LiquidCrystal_I2C.h>

int x = 0;
int gumb = 0;
int buttonState = 0;
const int buttonPin = 9;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(9600);
  lcd.begin();
  Serial.println("start");
}

void loop() {
  lcd.setCursor(0, 1);
  tmElements_t tm;
  RTC.read(tm);
  printUraLCD(tm);
  gumbo();
  preklop();
}

void preklop() {
  if (gumb == 1) osnova();
  if (gumb == 2) tehtnica();
}

void gumbo() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    x++;
    Serial.println(x);
  }
  else {
    if (16 < x) gumb = 1;
    if (16 > x && x > 1) gumb = 2;
    if (x != 0) Serial.println(x);
    x = 0;
  }
}

void osnova() {
  lcd.setCursor(0, 0);
  lcd.print("test 1");
  //  Serial.print("test 1");
  Serial.println(gumb);
  lcd.setCursor(0, 1);
  tmElements_t tm;
  RTC.read(tm);
  printUraLCD(tm);
 }

void tehtnica() {
  lcd.setCursor(0, 0);
  lcd.print("test 2");
  //  Serial.print("test 2");
  Serial.println(gumb);
  lcd.setCursor(0, 1);
  tmElements_t tm;
  RTC.read(tm);
  printUraLCD(tm);
}

void ura() {
  //  lcd.setCursor(0, 0);
  //    lcd.print("              ");
  lcd.setCursor(0, 1);
  tmElements_t tm;
  RTC.read(tm);
  printUraLCD(tm);
}

void printUraLCD (int tm) {
  lcd.print(tm.Hour);
  lcd.print(':');
  lcd.print(tm.Minute);
  lcd.print(':');
  lcd.print(tm.Second);
  lcd.print("  ");
  lcd.print(tm.Day);
  lcd.print('/');
  lcd.print(tm.Month);
  lcd.print("   ");
 }
void preklop(){

      if(gumb == 1) {
    osnova();
     }
    
         if(gumb == 2) {
    tehtnica();
     }

}

preklop calls osnova().

Then:

void osnova(){
  

   lcd.setCursor(0, 0);
    lcd.print("test 1"); 
  //  Serial.print("test 1");
Serial.println(gumb);



  lcd.setCursor(0, 1);
 tmElements_t tm;
 RTC.read(tm);
  
    lcd.print(tm.Hour);
    lcd.print(':');
    lcd.print(tm.Minute);
    lcd.print(':');
    lcd.print(tm.Second);
    lcd.print("  ");
    lcd.print(tm.Day);
    lcd.print('/');
    lcd.print(tm.Month);
    lcd.print("   ");
    
    gumbo(); 
    preklop();
}

osnova calls preklop.

Same issue with the other function that could be called, tehtnica().

This is called infinite recursion. It causes the stack to run into the heap and the program to crash. This whole thing needs to be rewritten so those functions can return.

As I see it, the calls to gumbo & preklop, from within osnova & tehtnica - is a mistake - as they need not be called from here. if you delete these calls, you will end up calling them again from the loop(), which is correct.

thank you all for your help.
I’m new and I am learning slowly.
Anders53 I used your code.
could I have a little change, now works fine

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <LiquidCrystal_I2C.h>

int x = 0;
int gumb = 0;
int buttonState = 0;
const int buttonPin = 9;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(9600);
  lcd.begin();
  Serial.println("start");
}

void loop() {
  lcd.setCursor(0, 1);
//  tmElements_t tm;
//  RTC.read(tm);
 printUraLCD();
  gumbo();
  preklop();
}

void preklop() {
  if (gumb == 1) osnova();
  if (gumb == 2) tehtnica();
}

void gumbo() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    x++;
    Serial.println(x);
  }
  else {
    if (16 < x) gumb = 1;
    if (16 > x && x > 1) gumb = 2;
    if (x != 0) Serial.println(x);
    x = 0;
  }
}

void osnova() {
  lcd.setCursor(0, 0);
  lcd.print("test 1");
  //  Serial.print("test 1");
  Serial.println(gumb);
  lcd.setCursor(0, 1);
  tmElements_t tm;
  RTC.read(tm);
//  printUraLCD(tm);
 }

void tehtnica() {
  lcd.setCursor(0, 0);
  lcd.print("test 2");
  //  Serial.print("test 2");
  Serial.println(gumb);
  lcd.setCursor(0, 1);
  tmElements_t tm;
  RTC.read(tm);
//  printUraLCD(tm);
}

//void ura() {
//  //  lcd.setCursor(0, 0);
//  //    lcd.print("              ");
//  lcd.setCursor(0, 1);
//  tmElements_t tm;
//  RTC.read(tm);
////  printUraLCD(tm);
//}

void printUraLCD () {
  
   tmElements_t tm;
  RTC.read(tm);
  
  lcd.print(tm.Hour);
  lcd.print(':');
  lcd.print(tm.Minute);
  lcd.print(':');
  lcd.print(tm.Second);
  lcd.print("  ");
  lcd.print(tm.Day);
  lcd.print('/');
  lcd.print(tm.Month);
  lcd.print("   ");
 }