Servo motor, led and button (always on, always off type)

Hi there!
I’m struggling a bit with a button that should control the behaviour of a servo motor.
The code below basically should make the servo move at specific times (this part works fine) and also then I press a button.

I would like to press the button (it’s an always on and always off button/switch) and make the servo move (it should move at a certain angle and then return back), and have the same behaviour when I de-press the button.

The code now simply makes the servo move at a specific angle when I press the button and it stays there, the servo will move back to it’s original position once I depress the button which is not what I want, I want all this cycle happening when I press or depress the button.

Obviously if I just put another position in the “if” statements the servo will continuously move back and forth because the button is and always on and always off button.

Is there a way to just say “if the state of the button changes (so to have the same behavior when I press or depress the button) move the servo in this pattern one time, otherwise don’t move”.
For the led I would like it to blink at a certain speed when nothing is happening (to signal that there’s power), and blink faster when the servo is moving (to signal it’s action).
Thank you!

#include <DS1302.h> // RTC library
#include <Servo.h> // Servo library

Servo myservo; // Servo initialization

Time t;
int Hour;
int Min;
int Sec;
DS1302 rtc(5, 6, 7); // Pin for DS1302

int button = 2; // Pin for button
int led = 4; // Pin for led

void setup() {
  //Settaggio servo
  myservo.attach(9); // Pin that comands servo
  myservo.write(90); // Initial servo position

  //Led and button settings
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);

  // Set clock in run-mode end disable write protection
  rtc.halt(false);
  rtc.writeProtect(false);
  Serial.begin(9200);
  // Comment out the following lines after the date and time is correcly set for the first time (values are saved in DS1302)
  //rtc.setDOW(WEDNESDAY); // Day of the week
  //rtc.setTime(19, 30, 00); // Time in 24hr format
  //rtc.setDate(2, 12, 2020); // Date in the following format: MONTH-DAY-YEAR
}

void loop() {

  t = rtc.getTime();
  Hour = t.hour;
  Min = t.min;
  Sec = t.sec;
  Serial.print(Hour);
  Serial.print(":");
  Serial.print(Min);
  Serial.print(":");
  Serial.println(Sec);

  // Timer for servo movements
  /* if ((Hour== 19 && Min== 31 && Sec== 00)||(Hour== 19 && Min== 31 && Sec== 30)||(Hour== 19 && Min== 32 && Sec== 00)||(Hour== 19 && Min== 32 && Sec== 30)) {
    myservo.write(180);
    delay (1000);
    myservo.write(90);
    }*/
    
  // Move servo when button is pressed (and change led behaviour)
  if (digitalRead(button) == LOW) {
    myservo.write(90);
    delay(500);
    myservo.write(180);
  }
  if (digitalRead(button) == HIGH) {
    digitalWrite(led, HIGH);
    delay(100);
    digitalWrite(led, LOW);
    delay(100);
    myservo.write(180);
  }

}

When you want to do it just once, you can use a variable for comparison.

if(inputA != inputA_old) {
  inputA = inputA_old;
}

Thanks, now it’s working as intended:

#include <DS1302.h> // RTC library
#include <Servo.h> // Servo library

Servo myservo; // Servo initialization

Time t;
int Hour;
int Min;
int Sec;
DS1302 rtc(5, 6, 7); // Pin for DS1302

int button = 2; // Pin for button
int led = 4; // Pin for led

int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  //Settaggio servo
  myservo.attach(9); // Pin that comands servo
  myservo.write(90); // Initial servo position

  //Led and button settings
  pinMode(led, OUTPUT);
  pinMode(button, INPUT); // set the internal pull up resistor, unpressed button is HIGH

  // Set clock in run-mode end disable write protection
  rtc.halt(false);
  rtc.writeProtect(false);
  Serial.begin(9200);
  // Comment out the following lines after the date and time is correcly set for the first time (values are saved in DS1302)
  //rtc.setDOW(WEDNESDAY); // Day of the week
  //rtc.setTime(19, 30, 00); // Time in 24hr format
  //rtc.setDate(2, 12, 2020); // Date in the following format: MONTH-DAY-YEAR
}

void loop() {
  t = rtc.getTime();
  Hour = t.hour;
  Min = t.min;
  Sec = t.sec;
  Serial.print(Hour);
  Serial.print(":");
  Serial.print(Min);
  Serial.print(":");
  Serial.println(Sec);

  // Servo's timer
  /* if ((Hour== 19 && Min== 31 && Sec== 00)||(Hour== 19 && Min== 31 && Sec== 30)||(Hour== 19 && Min== 32 && Sec== 00)||(Hour== 19 && Min== 32 && Sec== 30)) {
    myservo.write(180);
    delay (1000);
    myservo.write(90);
    }*/

  buttonState = digitalRead(button);
  if (buttonState != lastButtonState) {
    // if the state has changed, move servo
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      myservo.write(180);
      delay(500);
      myservo.write(90);
    }
    else {
      // if the current state is LOW then the button went from on to off:
      myservo.write(180);
      delay(500);
      myservo.write(90);
    }
  }
  // save the current state as the last state for next time through the loop
  lastButtonState = buttonState;
}

You're welcome, maybe you can add [Solved] in the topic title, thanks!