Interrupts not calling their corresponding function

Board type: Arduino UNO

Hello. I am trying to make something with 3 buttons: two to cycle through different numbers(options) and a third to lock in the answer. I've tried using interrupts but they don't call the function that they are assigned to.

#include <SevSeg.h>

#define SELECT_PIN 13    //selects(comfirms) the desired number on the sevseg
#define DOWN_PIN 12    //moves the selection down
#define UP_PIN 11    //moves the selection up
int selection;
int maxOptions;
SevSeg sevseg;

void setup() {
  Serial.begin(9600);

  //Sets up pins
  pinMode(SELECT_PIN, INPUT_PULLUP);
  pinMode(DOWN_PIN, INPUT_PULLUP);
  pinMode(UP_PIN, INPUT_PULLUP);

  //sets up SevSeg
  byte numDigits = 1; //number of digits in the display
  byte digitPins[] = {}; //defines ground pins, leave blank if numDigits = 1
  byte segmentPins[] = {8, 9, 3, 4, 5, 7, 6, 2};
  // An array defining the defines arduino pins to the 7 seg display
  // (A, B, C, D, E, F, G, DP)
  bool resistorsOnSegments = true;
  //true if resistors are placed in series to the segment pins
  //false if resistors are placed in series to the digit pins
  byte hardwareConfig = COMMON_CATHODE;   //Defines type of Sevseg display is being used.        //(COMMON_CATHODE/COMMON_ANODE)
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments); //Configuration    of the sevseg
  sevseg.setBrightness(90);  //Brightness setting

  //interrupts my beloved
  attachInterrupt(digitalPinToInterrupt(SELECT_PIN), confirm, FALLING);
  attachInterrupt(digitalPinToInterrupt(DOWN_PIN), subtract, FALLING);
  attachInterrupt(digitalPinToInterrupt(UP_PIN), add, FALLING);

  selection = 1;
  maxOptions = 5;
}

void loop() {
  // put your main code here, to run repeatedly:
  if (maxOptions == -1) {
    sevseg.blank();
  } else {
    sevseg.setNumber(selection, selection%2);
    sevseg.refreshDisplay();
  }

}

void confirm() {
  if (maxOptions != -1) {
    Serial.println(selection);
    maxOptions = -1;
    selection = 1;
  }
}

void add() {
  Serial.print("Add ");
  if (maxOptions != -1) {
    Serial.print(0);
    selection++;
    if (selection > maxOptions) selection = 1;
  }
  Serial.println(selection);
}

void subtract() {
  Serial.print("Subtract ");
  if (maxOptions != -1) {
    Serial.print(0);
    selection--;
    if (selection <= 0) selection = maxOptions;
  }
  Serial.println(selection);
}

1 Like

Welcome to the forum

The Uno only supports hardware interrupts on pins 2 and 3

For starters you are trying to use 3 interrupts

  attachInterrupt(digitalPinToInterrupt(SELECT_PIN), confirm, FALLING);
  attachInterrupt(digitalPinToInterrupt(DOWN_PIN), subtract, FALLING);
  attachInterrupt(digitalPinToInterrupt(UP_PIN), add, FALLING);

and none of those use pins 2 or 3

Can you see the problem ?

1 Like

Ah, I see! Thank you very much. What would you recommend I use instead of interrupts, at least for the third button(likely the select/confirm button)?

The Arduino State Change Detection example shows you how to poll buttons. Files>Examples>02.Digital>StateChangeDetection

1 Like

Are Pin Change Interrupts "hardware interrupts"?

Yes, but don't let's confuse things any more than they are already. More formally I should have referred to them as external interrupts, ie they each have their own interrupt vector unlike pin change interrupts,

Bear in mind that @cowmillesse almost certainly does not need to use interrupts of any kind in the first place

1 Like

@jremington has already pointed to an example in the IDE. That's a good start to learn.

I recommend to use the library OneButton by Matthias Hertel - you can install it via the library manager.
You can very easily reuse your callback functions with that library. Just check the examples.

You can use PCINT (Pin Change Interrupt) for the 3rd Button.

As Is said previously

2 Likes

Is not PCINT a way of solving a problem?

It could solve a problem that he doesn't have

3 Likes

It could cause problems he doesn't need.

a7

1 Like