Momentary Push Button - One Push for Entire Code

Edited
.
.
.
.
.
.
.
.
.

Start by reading this page: https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection.

You might have to "debounce" the button.

There are libraries that have both the "debounce" and the State Change Detection.

I think I have to add a variable to change when the button is pushed. I think this is close, but it is still not working.

#include<Servo.h>
Servo Myservo;
bool DoAction1=false;

void setup()
{
pinMode(2,INPUT);
Myservo.attach(3);
}

void loop()
{
if(digitalRead(2)==LOW)
{
DoAction1=true;
{
if(DoAction1==true)
{
Myservo.write(180);
delay(500);
Myservo.write(100);
delay(5000);
Myservo.write(180);
delay(500);
Myservo.write(130);
DoAction1=false;
}
else
{
DoAction1=false;
}
}
}
else
{
Myservo.write(130);
}
}

A better way to use momentary switches is to wire them to ground and use the internal pullup resistor (pinMode(pin INPUT_PULLUP);). My stated change tutorial shows how to do state change with active low (wired to ground) switches.

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

That is a useless description of the problem. The code does something, what is it? How is that different from what you want.

the code stops when the button is released. I want it to finish running even when the button is released.

Here is example to show how to so that using the state change detection method. This performs the action when the button becomes pressed (the transition from high to low) instead of when the button is pressed (the level). The next challenge is to get rid of delays. Here the code is posted properly in code tags and indented for readability with the IDE auto format tool. See post #5.

#include<Servo.h>
Servo Myservo;
//bool DoAction1 = false;

const byte buttonPin = 2;
const byte servoPin = 3;

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

void setup()
{
   Serial.begin(9600);
   pinMode(buttonPin, INPUT_PULLUP);
   Myservo.attach(servoPin);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 50;  // check switch 20 times per second
   if (millis() - timer >= interval)
   {
      timer = millis();
      buttonState = digitalRead(buttonPin);
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            Serial.print("go to 180");
            Myservo.write(180);
            delay(500);
            Serial.print("   go to 100");
            Myservo.write(100);
            delay(5000);
            Serial.print("   go to 180");
            Myservo.write(180);
            delay(500);
            Serial.print("   go to 130");
            Myservo.write(130);
            delay(500);
            Serial.print("finished");
         }
         lastButtonState = buttonState;
      }
   }
}

With a state machine:

// Version    YY/MM/DD  Description
// 1.00       21/08/06

#include<Servo.h>
Servo myServo;

#define switchIsClosed              LOW
#define switchIsOpen                HIGH
#define enabled                     true
#define disabled                    false

bool timingFlag                   = disabled;

const byte mySwitch               = 2;   //+5V----[50k internal pull-up]----[switch]----GND
const byte servoPin               = 3;
const byte heartbeatLED           = 13;

byte lastSwitchState              = switchIsOpen;

int mState                        = 0;

//timing stuff
unsigned long heartbeatMillis;
unsigned long switchMillis;
unsigned long commonTimerMillis;

//************************************************************************
void setup()
{
  pinMode(heartbeatLED, OUTPUT);

  pinMode(mySwitch, INPUT_PULLUP);

  myServo.attach(servoPin);
  myServo.write(130);

} //END of setup()


//************************************************************************
void loop()
{
  //***********************
  //time to toggle the heartbeatLED (every 1/2 second) ?
  if (millis() - heartbeatMillis >= 500)
  {
    //restart the TIMER
    heartbeatMillis = millis();

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //***********************
  //time to check the switches (every 50ms) ?
  if (millis() - switchMillis >= 50)
  {
    //restart the TIMER
    switchMillis = millis();

    checkSwitches();
  }

  //***********************
  //if we are timing, check the state machine ?
  if (timingFlag == enabled)
  {
    stateMachine();
  }

  //***********************
  //other non-blocking code
  //***********************

} //END of loop()


//************************************************************************
void stateMachine()
{
  switch (mState)
  {
    case 0:
      //do nothing
      break;

    case 1:
      if (millis() - commonTimerMillis >= 500)
      {
        //move servo        
        myServo.write(100);

        //next state
        mState = 2;

        //restart the TIMER
        commonTimerMillis = millis();
      }

      break;

    case 2:
      if (millis() - commonTimerMillis >= 5000)
      {
        //move servo
        myServo.write(180);

        //next state
        mState = 3;

        //restart the TIMER
        commonTimerMillis = millis();
      }

      break;

    case 3:
      if (millis() - commonTimerMillis >= 500)
      {
        //move servo
        myServo.write(130);

        //next state
        mState = 0;

        //disable the TIMER
        timingFlag = disabled;
      }

      break;
  }

} //END of   stateMachine()


//************************************************************************
void checkSwitches()
{
  byte state;

  //*****************************************       mySwitch
  state = digitalRead(mySwitch);

  //***********************
  //has the switch changed state ?
  if (lastSwitchState != state)
  {
    //update to the new state
    lastSwitchState = state;

    //************
    //if we are not timing, was the switch closed ?
    if (timingFlag == disabled && state == switchIsClosed)
    {
      //enable the TIMER
      timingFlag = enabled;

      //start the servo sequence
      mState = 1;

      //move servo
      myServo.write(180);

      //start the TIMER
      commonTimerMillis = millis();
    }

  } //END of  if (lastSwitchState != state)

} //END of  checkSwitches()

//************************************************************************