Changing Variables via external button to trigger code

I try to solve the following topic:
I want that the integer Minute goes up by 1 if I press an external button, while the whole project is running. So for example the integer contains the value 31 (Minute = 31), I want to be able to press the button and the integer should go up by 1, so now it contains the value 32 (Minute = 32). Because the action should trigger at 11:32. I want that it works without restarting everything.

void loop() {
int Minute = 31;
DateTime now = rtc.now();
if (now.hour() > 11 || (now.hour() == 11 && now.minute() == Minute)) {
 //action ......
    }

I hope you can understand my question, I already tried it with a simple if clause, but it seems that the integer does not goes up or the code does not update:

void loop(){
   int Minute = 31;
   if (buttonState == HIGH){
    Minute = Minute + 1;
}
 DateTime now = rtc.now();
    if (now.hour() > 11 || (now.hour() == 11 && now.minute() == Minute)) {
//action...
       }
}

PS: Of course i declared everything in the void setup etc.

void loop(){
   int Minute = 31;
   if (buttonState == HIGH){
    Minute = Minute + 1;
}

Each time through loop() Minute is set to 31. No wonder it does not change. Mind you, as you do not read the state of the button anyway it won't matter. Fix that first.

Making Minute a global variable or declaring it as static will help but it will bring with it other problems. You need to detect when the button becomes pressed, not when it is pressed. See the StateChangeDetection example in the IDE.

I don't have a lot of experience, so i read everything about StateChangeDirection, but would you be so kind to just quickly type the if clause ?

thank you very much

but would you be so kind to just quickly type the if clause ?

What if clause?

Your code will eventually contain something like:

int currSwitchState = digitalRead(switchPin);
if(currSwithState != prevSwithState)
{
   if(currSwitchState == HIGH) // Or LOW, if using pullup resistors
   {
      // The switch just became pressed
      Minute++;
   }
}

Everything works now, thank you both :slight_smile: