Hello Tomas,
You have to use an external interruption. Look at http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1147164546, link is in french but you can understand code. In summary, the SIGNAL(SIG_INTERRUPT0) function is called each time the state of the digital digital change.