PIR sensor and DC motor

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?