int button = 2; int flag; int readbutton; void setup(){} void loop(){ readbutton = digitalRead(button); if(readbutton>flag){ //your code here flag=readbutton; } if(readbutton==0){ flag=0; }