I have an Arduino sketch that reports data over the serial port when a button is pressed on the board. What is reading the serial port is a Node.js server that has some interactions with the OS when the button is pressed.
The problem that I found first was that if I didn’t put a delay at the end of my loop was that the Node.js server would seem to bottleneck and start reading the next set of instructions before finishing the last set. Each loop always sends data over to the serial port.
Anyways I set my sketch to delay for 100 milliseconds at the end of each loop.
The problem that I found now is that if I hold the button down for more than that 100ms it sees it as 2 button presses and so the node.js code that runs on each button press gets doubled up. Is there a way that I can tell if the user pressing the button has released the press?
You need to add a software to debounce the switch.
Depending on your app to might want an autorepeat or send a different
code when the switch is released.
If you only want one sent, you can add code like below: