Function does not work

Hello people

I am making an arduino watch with an LCD 20x4, I am having problems with the setds3231 function of setting the clock, it does not work the code part to increment or decrement the date and time.
I’ve done several tests but it still doesn’t work, I don’t know what I’m doing wrong so it doesn’t work
I thank you for your help

#include <Wire.h>
#include <TimerOne.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include "RTClib.h"
RTC_DS3231 RTC;
#define  up 53 
#define  doow 51 
#define  set 12 
#define  set1 49 
#define  ok 10 
 int year1=0,month1=0,day1=0,hour1=0,minute1=0,second1=0;
 
char x[]={">"};
 
byte buttonState = 0;         
int lastButtonState;
byte flag=0;

 
String zero(int n) { 
  if (n < 10) {            
    return "0" + String(n);
  } else {
    return String(n);
  }
}

void Print_lcd();
void set_DS3231();

void setup() {
  lcd.backlight();
  lcd.init();
  RTC.adjust(DateTime(__DATE__, __TIME__));

  pinMode(up, INPUT_PULLUP);
  pinMode(doow, INPUT_PULLUP);
     // initialize the pushbutton pin as an INPUT_PULLUP:
  pinMode(ok, INPUT_PULLUP);
  pinMode(set, INPUT_PULLUP);
  pinMode(set1, INPUT_PULLUP);
}

void loop() 
{  
 //DateTime nowr = RTC.now();
 // year1= nowr.year(),month1= nowr.month(),day1 = nowr.day(),hour1 = nowr.hour(),minute1 = nowr.minute(),second1 = nowr.second();

  buttonState = digitalRead(set1);
  
   if (!buttonState && lastButtonState ) 
  {
    flag = flag + 1; 
    if (flag > 1) {
      flag = 0; 
      lcd.clear();  
    }
  }
  lastButtonState=buttonState;
  
  if (flag==0)
  { 
   Print_lcd();
    } 
    else 
    {  
     set_DS3231(); 
  }
}

void set_DS3231()// Clock Set Function
{
  static unsigned int menu2 = 0;
  static unsigned int presse = 0; 
  int presse1=1,presse2=1;

  int year1=0,month1=0,day1=0,hour1=0,minute1=0,second1=0;
 
  DateTime nowr = RTC.now();
  
  year1= nowr.year(),month1= nowr.month(),day1 = nowr.day(),hour1 = nowr.hour(),minute1 = nowr.minute(),second1 = nowr.second();

  unsigned int  ds3231[6]={year1,month1,day1,hour1,minute1,second1};
  
  lcd.setCursor(0,0);
  lcd.print("                    ");
  
  lcd.setCursor(5,0); // 
  lcd.print("Set.Ds3231"); //
  
  
  lcd.setCursor(0,1); // 
  lcd.print("Year");  //
  lcd.setCursor(0,2);
  lcd.print("Mon");  //
  lcd.setCursor(0,3); 
  lcd.print("Day");

  lcd.setCursor(10,1);// 
  lcd.print("Hour");  //
  lcd.setCursor(10,2);
  lcd.print("Min");   //
  lcd.setCursor(10,3); 
  lcd.print("Sec");
  
  presse2 = (digitalRead(set)); 
  if (!presse2 && presse1 ) 
  {
    menu2 = menu2 + 1; 
    if (menu2 > 6) {
      menu2 = 0;   
    }
  }   
  presse1 = presse2;// 
  
  switch (menu2) {
  case 0:
     lcd.setCursor(4,1);
     lcd.print(x[0]);
     lcd.setCursor(5,1);
     lcd.print(ds3231[menu2]);//year
     break;
  case 1:
     lcd.setCursor(4,1);
     lcd.print(" ");
     lcd.setCursor(4,2);
     lcd.print(x[0]); 
     lcd.setCursor(7,2);
     lcd.print(zero(ds3231[menu2])); //month
     break;
    case 2:
     lcd.setCursor(4,2);
     lcd.print(" ");
     lcd.setCursor(4,3);
     lcd.print(x[0]);
     lcd.setCursor(7,3);
     lcd.print(zero(ds3231[menu2]));//day
     break;
    case 3:
       lcd.setCursor(4,3);
       lcd.print(" ");
       lcd.setCursor(15,1);
       lcd.print(x[0]);
       lcd.setCursor(17,1);
       lcd.print(zero(ds3231[menu2]));//hour
     break;
    case 4:
       lcd.setCursor(15,1);
       lcd.print(" ");
       lcd.setCursor(15,2);
       lcd.print(x[0]);
       lcd.setCursor(17,2);
       lcd.print(zero(ds3231[menu2]));//minute
     break;
    case 5:
       lcd.setCursor(15,2);
       lcd.print(" ");
       lcd.setCursor(15,3);
       lcd.print(x[0]);
       lcd.setCursor (17,3);
       lcd.print(zero(ds3231[menu2]));//second
    break;
   default:  
       lcd.setCursor(15,3);
       lcd.print(" ");
       menu2=0;
   break;
    }

    if (digitalRead(up)==LOW )
      {
      presse=ds3231[menu2];
      presse+=1;
      ds3231[menu2]=presse;
      }
      else
     if (digitalRead(doow)==LOW )
       {
      presse=ds3231[menu2];
      presse-=1;
      ds3231[menu2]=presse;
        }

    if (digitalRead(ok)==LOW)
    {
    RTC.adjust(DateTime(ds3231[0],ds3231[1],ds3231[2],ds3231[3],ds3231[4],ds3231[5]));  
    flag=0;
    menu2=0;
    lcd.clear();
    } 
delay(100);
  }

void Print_lcd()
{
  DateTime nowr = RTC.now(); 

  
  lcd.setCursor(0, 0);
  
  //lcd.print("date:");
  lcd.print(zero(nowr.day()) + "/" + zero(nowr.month()) + "/" + nowr.year());
  
  lcd.setCursor(12, 0);
  //lcd.print("time:");
  lcd.print(zero(nowr.hour()) + ":" +  zero(nowr.minute()) + ":" + zero(nowr.second()));
  
 if (digitalRead(ok)==LOW)
    {
    lcd.clear();
    } 
  delay(10);
}

I don't have an answer to your issue and "it doesn't work" is not helpful.

I will say that, in C and C++, comma can be an operator so

  year1= nowr.year(),month1= nowr.month(),day1 = nowr.day(),hour1 = nowr.hour(),minute1 = nowr.minute(),second1 = nowr.second();

probably does not work as you expect and should be broken up into about six lines.

vaj4088:
I don't have an answer to your issue and "it doesn't work" is not helpful.

I will say that, in C and C++, comma can be an operator so

  year1= nowr.year(),month1= nowr.month(),day1 = nowr.day(),hour1 = nowr.hour(),minute1 = nowr.minute(),second1 = nowr.second();

probably does not work as you expect and should be broken up into about six lines.

Hi

Thanks for the comment

Maybe I did not clarify well in the first post!
The problem with the function DS_3231 is when I want to set the date and time manually in the set_DS3231 function, the increment and decrement buttons don't work.

I think it will be this part of the code below that has some error but I don't know what is wrong to not work

    if (digitalRead(up)==LOW )
      {
      presse=ds3231[menu2];
      presse+=1;
      ds3231[menu2]=presse;
      }
      else
     if (digitalRead(doow)==LOW )
       {
      presse=ds3231[menu2];
      presse-=1;
      ds3231[menu2]=presse;
        }

Sorry ,but I understand what you mean here!

probably does not work as you expect and should be broken up into about six lines.

I appreciate all your help.

Hello
Can someone help me find where is the errors in this code to not work I already tested all my knowledge and can not find the defect.
Thanks

the increment and decrement buttons don't work.

Please post an accurate, hand drawn wiring diagram, with pins and parts labeled and component values stated.
Image posting guide

Before starting a clock project, practice with the Button tutorial that comes with Arduino, and make sure you can read the buttons.

And add some debug prints.

Hi
Here is the pin circuitry that sets up the Arduino watch as you asked me.
I changed from MEGA arduino to UNO arduino to see if it was arduino problem, but with arduino uno the problem continues, as I wrote in the posts above

here is the code with the pin changes

#include <Wire.h>
#include <TimerOne.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include "RTClib.h"
RTC_DS3231 RTC;
#define  up 6 
#define  doow 7 
#define  set 9  

#define  ok 8 
 int year1=0,month1=0,day1=0,hour1=0,minute1=0,second1=0;
 
char x[]={">"};
 
byte buttonState = 0;         
int lastButtonState;
byte flag=0;

 
String zero(int n) { 
  if (n < 10) {            
    return "0" + String(n);
  } else {
    return String(n);
  }
}

void Print_lcd();
void set_DS3231();

void setup() {
  lcd.backlight();
  lcd.init();
  RTC.adjust(DateTime(__DATE__, __TIME__));

  pinMode(up, INPUT_PULLUP);
  pinMode(doow, INPUT_PULLUP);
     // initialize the pushbutton pin as an INPUT_PULLUP:
  pinMode(ok, INPUT_PULLUP);
  pinMode(set, INPUT_PULLUP);
  pinMode(set1, INPUT_PULLUP);
}

void loop() 
{  
 //DateTime nowr = RTC.now();
 // year1= nowr.year(),month1= nowr.month(),day1 = nowr.day(),hour1 = nowr.hour(),minute1 = nowr.minute(),second1 = nowr.second();

  buttonState = digitalRead(set1);
  
   if (!buttonState && lastButtonState ) 
  {
    flag = flag + 1; 
    if (flag > 1) {
      flag = 0; 
      lcd.clear();  
    }
  }
  lastButtonState=buttonState;
  
  if (flag==0)
  { 
   Print_lcd();
    } 
    else 
    {  
     set_DS3231(); 
  }
}

void set_DS3231()// Clock Set Function
{
  static unsigned int menu2 = 0;
  static unsigned int presse = 0; 
  int presse1=1,presse2=1;

  int year1=0,month1=0,day1=0,hour1=0,minute1=0,second1=0;
 
  DateTime nowr = RTC.now();
  
  year1= nowr.year(),month1= nowr.month(),day1 = nowr.day(),hour1 = nowr.hour(),minute1 = nowr.minute(),second1 = nowr.second();

  unsigned int  ds3231[6]={year1,month1,day1,hour1,minute1,second1};
  
  lcd.setCursor(0,0);
  lcd.print("                    ");
  
  lcd.setCursor(5,0); // 
  lcd.print("Set.Ds3231"); //
  
  
  lcd.setCursor(0,1); // 
  lcd.print("Year");  //
  lcd.setCursor(0,2);
  lcd.print("Mon");  //
  lcd.setCursor(0,3); 
  lcd.print("Day");

  lcd.setCursor(10,1);// 
  lcd.print("Hour");  //
  lcd.setCursor(10,2);
  lcd.print("Min");   //
  lcd.setCursor(10,3); 
  lcd.print("Sec");
  
  presse2 = (digitalRead(set)); 
  if (!presse2 && presse1 ) 
  {
    menu2 = menu2 + 1; 
    if (menu2 > 6) {
      menu2 = 0;   
    }
  }   
  presse1 = presse2;// 
  
  switch (menu2) {
  case 0:
     lcd.setCursor(4,1);
     lcd.print(x[0]);
     lcd.setCursor(5,1);
     lcd.print(ds3231[menu2]);//year
     break;
  case 1:
     lcd.setCursor(4,1);
     lcd.print(" ");
     lcd.setCursor(4,2);
     lcd.print(x[0]); 
     lcd.setCursor(7,2);
     lcd.print(zero(ds3231[menu2])); //month
     break;
    case 2:
     lcd.setCursor(4,2);
     lcd.print(" ");
     lcd.setCursor(4,3);
     lcd.print(x[0]);
     lcd.setCursor(7,3);
     lcd.print(zero(ds3231[menu2]));//day
     break;
    case 3:
       lcd.setCursor(4,3);
       lcd.print(" ");
       lcd.setCursor(15,1);
       lcd.print(x[0]);
       lcd.setCursor(17,1);
       lcd.print(zero(ds3231[menu2]));//hour
     break;
    case 4:
       lcd.setCursor(15,1);
       lcd.print(" ");
       lcd.setCursor(15,2);
       lcd.print(x[0]);
       lcd.setCursor(17,2);
       lcd.print(zero(ds3231[menu2]));//minute
     break;
    case 5:
       lcd.setCursor(15,2);
       lcd.print(" ");
       lcd.setCursor(15,3);
       lcd.print(x[0]);
       lcd.setCursor (17,3);
       lcd.print(zero(ds3231[menu2]));//second
    break;
   default:  
       lcd.setCursor(15,3);
       lcd.print(" ");
       menu2=0;
   break;
    }

    if (digitalRead(up)==LOW )
      {
      presse=ds3231[menu2];
      presse+=1;
      ds3231[menu2]=presse;
      }
      else
     if (digitalRead(doow)==LOW )
       {
      presse=ds3231[menu2];
      presse-=1;
      ds3231[menu2]=presse;
        }

    if (digitalRead(ok)==LOW)
    {
    RTC.adjust(DateTime(ds3231[0],ds3231[1],ds3231[2],ds3231[3],ds3231[4],ds3231[5]));  
    flag=0;
    menu2=0;
    lcd.clear();
    } 
delay(100);
  }

void Print_lcd()
{
  DateTime nowr = RTC.now(); 

  
  lcd.setCursor(0, 0);
  
  //lcd.print("date:");
  lcd.print(zero(nowr.day()) + "/" + zero(nowr.month()) + "/" + nowr.year());
  
  lcd.setCursor(12, 0);
  //lcd.print("time:");
  lcd.print(zero(nowr.hour()) + ":" +  zero(nowr.minute()) + ":" + zero(nowr.second()));
  
 if (digitalRead(ok)==LOW)
    {
    lcd.clear();
    } 
  delay(10);
}

set_DS3231 This function seems to have its own set of year1 etc variables, in addition to the global ones.
Is this intentional?

TheMemberFormerlyKnownAsAWOL:
And add some debug prints.

The code compiles smoothly but physically testing when I want to manually set the clock to increment or decrement doesn't work

Fiasgardone:
The code compiles smoothly but physically testing when I want to manually set the clock to increment or decrement doesn’t work

“doesn’t work”
Grrr.

TheMemberFormerlyKnownAsAWOL:

set_DS3231

This function seems to have its own set of year1 etc variables, in addition to the global ones.
Is this intentional?

The global and the global variables within the function in terms of operation are distinct so I did so, I don't know if in this case it generates problems

TheMemberFormerlyKnownAsAWOL:
"doesn't work"
Grrr.

What I mean is that it does not make me the increment and decrement in the code I did.

Before starting a clock project, practice with the Button tutorial that comes with Arduino, and make sure you can read the buttons.

jremington:
Before starting a clock project, practice with the Button tutorial that comes with Arduino, and make sure you can read the buttons.

HI

I've been testing the buttons and LEDs on the function set_DS3231 and it worked fine, but to do what I want in the clock program I don't know what the problem is.
See if anyone discovers the problem that has this function!
Thanks