Switching between 2 batteries using mosfet

On most Arduinos GPIO1 is TxD for the serial port, so using it for anything else is asking for trouble.

Battery 1 -ve is connected to the control circuit via the buck converter, which completes the circuit from +5V, OK2, Q1 gate, Q1 source.

Battery 2 -ve is not in any way connected to the control circuit so there is no path back to ground for the control signal to Q2 gate.

Your circuit cannot work like this as you are of necessity separating the 2 negative terminals of the 2 batteries, but both need to be connected to the control circuit ground, which is in contradiction to the requirement for them the be separated.

The solution would be for the output side of each opto-coupler to be fed from the battery it controls, or to use a high side switch.

You might find this helpful: Common ground and why you need one