user41573: Hi I was wondering if a digitalRead state change detection could be saved in a variable like counter++, just like you can do with an interrupt function.
arent these two operations almost the same?
like have an external circuit with a  reed closed by a magnet, to take the place of a momentary push button, 

user20245: The 2 techniques described in the question are commonly referred to as "polling" and "interrupt".  Either will work.  Polling is suggested for beginner programmers and when time is not important. Such as scanning an array of human controlled buttons. Whereas interrupts are usually reserved for when time is of the essence.  Such as synchronizing a camera's release switch with a strobe flash.
Also, consider polling as a simple to understand, easy to test sequence of events that will always happen in the expected order.  While interrupts can happen at any time and all interruptible code has to be written to tolerate being interrupted.  
Often, debouncing is the more difficult problem when combining mechanical switches and microprocessors.  A good fundamental exercise is to write your own debounce program.  However, an Arduino debounce library is already available.

41573: <p>Hi I was wondering if a digitalRead state change detection could be saved in a variable like counter++, just like you can do with an interrupt function.</p>

<p>arent these two operations almost the same?</p>

<p>like have an external circuit with a  reed closed by a magnet, to take the place of a momentary push button, </p>
 20245: <p>The 2 techniques described in the question are commonly referred to as "<a href="https://en.wikipedia.org/wiki/Polling_(computer_science)" rel="nofollow noreferrer">polling</a>" and "<a href="https://en.wikipedia.org/wiki/Interrupt" rel="nofollow noreferrer">interrupt</a>".  Either will work.  Polling is suggested for beginner programmers and when time is not important. Such as scanning an array of human controlled buttons. Whereas interrupts are usually reserved for when time is of the essence.  Such as synchronizing a camera's release switch with a strobe flash.</p>

<p>Also, consider polling as a simple to understand, easy to test sequence of events that will always happen in the expected order.  While interrupts can happen at any time and all interruptible code has to be written to tolerate being interrupted.  </p>

<p>Often, debouncing is the more difficult problem when combining mechanical switches and microprocessors.  A good fundamental exercise is to <a href="https://playground.arduino.cc/Learning/SoftwareDebounce" rel="nofollow noreferrer">write your own debounce program</a>.  However, an <a href="https://playground.arduino.cc/Code/Bounce" rel="nofollow noreferrer">Arduino debounce library</a> is already available.</p>
