You have a variable that holds the state of the PIR pin - val.
You have a variable, state, that holds what? Why do you need two global variables that appear to hold the same thing?
You have a variable that holds the state of the PIR pin - val.
You have a variable, state, that holds what? Why do you need two global variables that appear to hold the same thing?