use interrupt with input code example Example: interrupt arduino attachInterrupt(digitalPinToInterrupt(interruptPin), someFuntion, CHANGE);