JChristensen / DS3232RTC

Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks
GNU General Public License v3.0
392 stars 135 forks source link

set date with 3 button #54

Closed fatemenaseri closed 6 years ago

fatemenaseri commented 6 years ago

hi we have 3 push buttons which set the number of day,month.these push buttons increase and decrease the values and then third push button set the value. by setting the month values with pushing third push button(set),then we want to set the day value but by pushing increase and decrease buttons there is no change on day value ,please help us understand where is our mistake on our sketch,thank you.

my code: include

include

const int buttonINC = 2; const int buttonDEC = 3; const int buttonSET = 4;

LiquidCrystal_I2C lcd (0x27, 16, 2); int buttonState1 = 0; int buttonState2 = 0; int d=1 , m=1; void setup() { pinMode ( buttonINC , INPUT_PULLUP); pinMode ( buttonDEC , INPUT_PULLUP); pinMode ( buttonSET, INPUT_PULLUP); Wire.begin(); lcd.begin(16,2); lcd.backlight(); lcd.clear(); }

void loop() { lcd.setCursor(0,0); lcd.print("SET DATE: M / D"); lcd.setCursor(10,1);

lcd.print(m); lcd.print(" / "); lcd.print(d); //////////////////////////////////////////////set month//////////////////////////// if (digitalRead(buttonINC) == 0){ d=0; m=m+1; if(m>12) m=1; delay(300);} else if (digitalRead(buttonDEC) == 0 ){

m=m-1; if(m<=0) m=1;

delay(500); }

buttonState1 = digitalRead(buttonINC); buttonState2 = digitalRead(buttonDEC);

/////////////////////////////////////////////setting date///////////////////////////// if (digitalRead(buttonSET ) == 0 && buttonState1==HIGH && buttonState2==HIGH){
if (digitalRead(buttonINC) == 0){ d=d+1; if(d>=31) d=1;}

else   if (digitalRead(buttonDEC) == 0) 
    d=d-1;

 }

}

JChristensen commented 6 years ago

Greetings.

I prefer that issues submitted here be actual problems with the library, not with user code. From what I can tell, I would want a better approach to debouncing and I would eliminate the calls to delay(). Maybe have a look at my Button library.

Cheers and good luck with your project.

JChristensen commented 6 years ago

PS: I would also consider using a finite state machine to move through the different states, i.e. setting the month, setting the day, etc. Here is a sketch that may be instructive. It's a bit different from your clock, it uses NeoPixels and only has two buttons, but note how a state machine is used to set the clock.