wvmarle:
I'm afraid you have to start explaining again what you're even trying to accomplish here.I see a series of two optocouplers that do nothing but pull high or low another pin on the same Arduino (some 8-36 µs later due to the delay in the optocouplers), which doesnt' seem to have much if any purpose.
The code in post 1 is too unreadable - you'll have to strip it down to the shortest possible that demonstrates the problem you actually have.
So essentially I would like to get off the arduino board and run it straight from the ATmega2560.
The code in its simplest form is to check if the outputs = inputs (automated cable continuity test). The optocouplers are there for electrical isolation of the cable which will be connected between the optocouplers