Is it possible to code a DPDT switch?

Yes, if you are connecting 3.7V from one or more batteries in parallel. You must not connect more than 5.5V to the 5V pin, but if you connect less than 5V there is still a good chance that it will work fine. If you connect to the Vin pin, the regulator will waste at least 0.7V, which means that there will be 3V or less for the chip and this is much less likely to work at 16MHz.