Why won't this work?

Try moving the line inside setup() to above setup(). Then val will be global scope.