I'm learning G Code and I was able to setup my first CNC structure and move axis, its basically not a CNC machine but I'm taking advantage of it and building a machine with robotic movements.

I have some Relays, and MOSFETs on my Arduino (or could be Arduino Mega) board is there some way that through G Code commands I can turn on/off some of them? or read switch state? or do some action in response to a trigger?

I know, I have some options like using resources for spindle, coolant etc.

But I have greater hardware needs to turn on/off and get input like switches is there some options something like custom G Code or some extra of additional commands those can be used for different scenarios like input/output.