Using a genuine Arduino MEGA to test the plugboard logic:
All the plugboard pins are set to input with the pullup resistor activated. One at a time, each plug is set to output and low. The other plugs are read one at a time, a 0 is returned if the other end of the plug is installed, otherwise a 1 is returned.
The code is implemented as a state machine and only one I/O operation is done each time it is called. This routine needs to be called once or twice in between changing the display from one digit to the next.
Thie state machine takes 24us to run.
Here a list of the installed plugs is shown:
The code stores any plugs detected in an array. The scanning process is shown below.
Notice that column 23 changes to 6 as the plug is discovered. Column 6 was already changed to 23 as one side of the plug was discovered.
Detecting when a plug is removed is equally important. and a different logic is needed. If all the plugs are read and neither returns 0, reset the plugboard entry to itself. Notice the last row changes from 1 to 26 once the A-Z plug is removed;