To pin 39 is an Opto-isolator connected. So in Serial i always get 0. But if i change the pin to 41 (just an led connected) the Output is right. So it go 1 0 1 0 1 0 ....
Why it doesn't work with an Opto-isolator?
That is probably why. The input of the opto-isolator is the anode of a led. The cathode is connected to ground. You must set the current when the state of the pin is HIGH. May I suggest a 470-500 ohm resistor between the Arduino pin and the opto-isolator input. Hopefully, you have not torched the output on that pin.
You write to the control register, and read from the pin. At least that is the way I remember it.