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.