Didn't you notice any bits I may have forgotten to add? Like a Serial.begin()...
Remember that this untested, not even compile-tested, typed out in a few minutes directly in the comment, primarily meant to illustrate how it can be done. That Serial bit was something I added as a bonus, thought it may be helpful to give you yet another state-change-detection example.
Don't just load pieces of code from here and there unless you understand what the code is doing. There are great help pages on this site for all Arduino specific commands, plus the huge resources on C++ programming all over the Internet.