OutputPin allways HIGH

First make your code easy to read and meaningful, so you don't confuse yourself as to what you THINK you're doing.

You have a variable called 'Pin40' and you assign it the value 0.

You then do a a digitalWrite(40,HIGH), turning Pin 40 NOT 'Pin40' (actually Pin 0) on.

Then in your serial output you go back to trying to display the state on your variable 'Pin40', which is actually Pin 0, a pin you never actually set state for!

Probably clearer to call your variables something less confusing, like signalInPin and signalOutPin.

Fix that first and see where your are :wink: