You can already do it... just tie the GPIO pin and a ground to the switch's input. Green is signal and black is a shared ground.
The M42 command is actually all you need in the gcode... and to adjust the G4 timing parameters R[milliseconds] and S[seconds].
So, you'll replace the G0 X3 Y3 commands with M42 P21 S1... then after the G4 pause command, issue another M42 P21 but with an S0. (Your logic might be backwards... so it might need an S0 followed by the S1... but it's just flipping the I/O pin.) Also, make sure you set the pin correctly at power up or in the start gcode with the proper "ending" M42 command.
I might add, the inelegance of the switch is a necessary evil to support every printer out there... it abstracts everything down to gcode, without regard to firmware or machine configuration.