program help

Basically it does work except when the motor is reversing the gate's open again before the motor finishes returning all the way.

Names like MRELAY1, MRELAY2, MRELAY3, MRELAY4, and MRELAY5 do not help when writing/debugging code. Names like gateRelay1, gateRelay2, groundRelay, forwardRelay, and reverseRelay mean that I don't need to continually look back at the comments to see that does what.

const int MRELAY = 8;

No clue what this is for.

int val2 = 0;

There's no val1, so why is there a val2? Why is it global?

if(digitalRead(InfraredSensorPin) == LOW);
Rarely a good idea.