Arduino as a button monitor for computer

Zápisník experimentátora

From one project I chose an example of monitoring button presses using Arduino. The program monitors the push of a button with a interrupt and sends information about the pressed button via the serial port to the computer. In the example, you can find the interrupt setting using the function attachInterrupt and the macro digitalPinToInterrupt.

Because you can not communicate via a serial port when you are in interrupt, you need to have information about push the button in the main program. All used variables must have flag volatile so that the compiler does not optimize them and are also accessible in the main program and in interrupt.

And there's also an example of how to handle the pushbuttons. In this case, these are long intervals, because in this project has been controlled pressing a button in the long interval. Buttons are connected via internal pull-up.

Source code

There are only selected parts of the project. To use the source code, you need to add these functions to the functions setup and loop.

const uint8_t interrupt_pin1 = 2;
const uint8_t interrupt_pin2 = 3;
volatile bool signal_interrupt_pin1 = false;
volatile bool signal_interrupt_pin2 = false;
volatile unsigned long debounce_interrupt_pin1 = 0;
volatile unsigned long debounce_interrupt_pin2 = 0;

void button_handler1() {
  if((millis()-debounce_interrupt_pin1) > 5000) {
    signal_interrupt_pin1 = true;
    debounce_interrupt_pin1 = millis();

void button_handler2() {
  if((millis()-debounce_interrupt_pin2) > 5000) {
    signal_interrupt_pin2 = true;
    debounce_interrupt_pin2 = millis();

void loop_interrupts() {
  if(signal_interrupt_pin1) {
    signal_interrupt_pin1 = false;
  if(signal_interrupt_pin2) {
    signal_interrupt_pin2 = false;

void setup_interrupts() {
  pinMode(interrupt_pin1, INPUT_PULLUP);
  pinMode(interrupt_pin2, INPUT_PULLUP);