Code for counter

Yes, I realized after my input, that I have to connect the signal cable, also to the GND of the Mega.... but thanks very much....