Control of DC Motor with L293D Transistor

I want my motor to be able to turn on after I press and release a button, and to turn back off after I press and release the button. Can someone tell me the code to do this?

What the wiring looks like is in the attachment.

This is the code I have in order to make the motor go both ways:

int switchPin = 2; // switch input
int motor1Pin1 = 3; // pin 2 on L293D
int motor1Pin2 = 4; // pin 7 on L293D
int enablePin = 9; // pin 1 on L293D

void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT);

// set all the other pins you're using as outputs:
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enablePin, OUTPUT);

// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
}

void loop() {
// if the switch is high, motor will turn on one direction:
if (digitalRead(switchPin) == HIGH) {
digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D low
digitalWrite(motor1Pin2, HIGH); // set pin 7 on L293D high
}
// if the switch is low, motor will turn in the opposite direction:
else {
digitalWrite(motor1Pin1, HIGH); // set pin 2 on L293D high
digitalWrite(motor1Pin2, LOW); // set pin 7 on L293D low
}
}

l293dmontagem-ConvertImage.jpg

l293dmontagem-ConvertImage.jpg

l293dmontagem-ConvertImage.jpg

You need a few variables to remember the last switch state and the state of the motor.

void loop()
{
  // motor  state (start with off)
  static int motorstate = 0;
  // last state of switch
  static int lastSwitchstate = HIGH;

  // current state of switch
  int currSwitchstate;

  // read switch; switch is HIGH if not pressed, LOW if pressed
  currSwitchstate = digitalRead(switchPin);
  // if state changed
  if (currSwitchstate != lastSwitchstate)
  {
    lastSwitchstate = currSwitchstate;
    // if switch was released
    if (currSwitchstate == HIGH)
    {
      // change state
      motorstate ^= 1;

      if(motorstate == 1)
      {
        // do whatever it takes to run the motor
        Serial.println("Motor on");
      }
      else
      {
        // do whatever it takes to stop the motor
        Serial.println("Motor off");
      }
    }
  }
}

The static integers in the begin of the loop remember the states. Replace the Serial.println statement by what you need to do to switch the motor on and off. You might need to debounce your switch.

Do I need to change anything about void setup()?

Not as far as I can see.