variating an ouput over time

Do you know how to read the state of a switch?

yes

Do you know how to do it repeatedly? Do you know how to record the previous state and detect when the current state is different to the previous state?

no