Don’t wire 3.3 volt or 0 volt supply directly to the pin. If at some point in the code it accidentally get’s activated as an output then it can short out and get damaged. Make sure you have a resistor (1 k ohm or something) in the path to limit the current.
I would add Serial statements to Setup() and Loop() so it sends the measured state of val to the PC. To make sure that the digital pin state is actually registered. In the above code you assume that the digitalRead statement returns different values. I can’t see anything wrong with the code, as it looks so simple. But your experience suggests something is wrong. So I would start to second-guess anything and make it proof that it works.