motor control using H Bridge

Hi Folks, IM trying to set up a program so that when one button is pressed the motor turns one way, and when another button is pressed it will turn another. My problem is that the motor does not stay on when pressed once, the button has to stay on all the time for it to do this. Can anyone give me any advice?
My code is:

/*
sweeping motor control.
*/
int forwardswitchPin = 13;
int backswitchPin = 12;
int motor1APin = 6;     // H-bridge leg 1
int motor2APin = 7;     // H-bridge leg 2

void setup()
{
pinMode(forwardswitchPin, INPUT);
pinMode(backswitchPin, INPUT);
pinMode(motor1APin, OUTPUT);
pinMode(motor2APin, OUTPUT);
}

void loop()
{
if (digitalRead(forwardswitchPin) == HIGH)
  {
    digitalWrite(motor1APin, LOW); // set leg 1 of the H-bridge low
    digitalWrite(motor2APin, HIGH); // set leg 2 of the H-bridge high
  }
else if (digitalRead(backswitchPin) == HIGH) 
  {
    digitalWrite(motor1APin, HIGH); // set leg 1 of the H-bridge low
    digitalWrite(motor2APin, LOW); // set leg 2 of the H-bridge low
  }
}

here is how I have it wired up.

I am a technology teacher and trying to create a project where pupils will design and build a CCTV system where once movement is sensed it will start recording and sweeping around until it hits a switch, but I cant seem to get the actual motor control section figured out. Im very new to arduino and this has got me stumped. Can anyone help?

Thanks in advance.

The state change detection example in the IDE will show how to do what you want. Go to Files, Examples, Digital. You look for a change from, say, LOW to HIGH transition to set the motor forward and HIGH to LOW transition for reverse. In your diagram, on the top power rails you have +V and ground going to the same rails. Buttons are wired with pull down resistors? Better would be to have the buttons wired one side to ground and the other to the pin with internal pullups enabled by pinMode(pin, INPUT_PULLUP);. Add the cap and you have hardware debounce which works to debounce most momentary switches.

never even knew about this! thank you will have a look :slight_smile: