How to code for dry contact open/close not in loop

sherzaad:
if you can't figure that out, you have need to go back to and learn how to build basic electronic circuits!

I know but because I followed the sketch before with 4 buttons and need to make sure. I connected with one pin with 5 V and pin #2 connected with control and resistor, second leg of resistor with ground!

I did this code and now I can when I press one time will write "open" one time without loop.

int Switch = 2;

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

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

  pinMode(Switch, INPUT);
  Serial.begin(9600);

}
void loop()
{
buttonState = digitalRead(Switch);

 if (digitalRead(Switch) == HIGH && buttonState != lastButtonState) {
    
  Serial.println("Open");
delay(200);

  }


 }

now I want when release the button write"close" one time every release!

Thanks for all. Now I made it like this and I got what I want from it:

int Switch = 2;  //Pin for sensor switch

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

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

  pinMode(Switch, INPUT);
  Serial.begin(9600);

}
void loop()
{
buttonState = digitalRead(Switch);

 if (digitalRead(Switch) == HIGH && buttonState != lastButtonState) {
    
  Serial.println("open");
  delay(180);

  }
      lastButtonState = buttonState;

  if  (digitalRead(Switch) == LOW && buttonState == 1){
    Serial.println("Close");
    delay(200);

  }


 }