Interrupts Not Firing When Expected

EDIT: The board is an Arduino Uno R4 WIFI, the interrupt pins are 2 and 3, and the buttons and the physical wiring is confirmed to be done correctly. This code was made in Visual Studio Code if that would effect this.

This code is for a class to practice using Interrupts. The below code is creating a roulette wheel effect where the first button begins the scrolling between 0-9 and the second button stops at the number it lands on.

I can confirm the scrolling effect works by setting rouletteState to HIGH in the loop so the interrupt simply isn't changing the state. I have rouletteState as volatile. Could someone explain what I have done incorrectly here?

#include <Arduino.h>

#define NUM_BUTTONS 2
#define NUM_SEGMENTS 7


#define A 8
#define B 10
#define C 11
#define D 4
#define E 5
#define F 6
#define G 7



int buttonPins[NUM_BUTTONS] = {2,3};
int ledPins[NUM_SEGMENTS] = {8, 10, 11, 4, 5, 6, 7};
int counter0 = 0;
volatile bool rouletteState = 0;

int segmentDisp[10][7] = {
// A  B  C  D  E  F  G  
  {1, 1, 1, 1, 1, 1, 0}, // 0
  {0, 1, 1, 0, 0, 0, 0}, // 1
  {1, 1, 0, 1, 1, 0, 1}, // 2
  {1, 1, 1, 1, 0, 0, 1}, // 3
  {0, 1, 1, 0, 0, 1, 1}, // 4
  {1, 0, 1, 1, 0, 1, 1}, // 5
  {1, 0, 1, 1, 1, 1, 1}, // 6
  {1, 1, 1, 0, 0, 0, 0}, // 7
  {1, 1, 1, 1, 1, 1, 1}, // 8
  {1, 1, 1, 1, 0, 1, 1}  // 9

};




void startRoulette() {
  rouletteState = true;

}

void endRoulette() {
  rouletteState = false;
}

void setup() {
  attachInterrupt(digitalPinToInterrupt(3), endRoulette, FALLING);
  attachInterrupt(digitalPinToInterrupt(2), startRoulette, FALLING);
  

  for (int i = 0; i < NUM_BUTTONS; i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
    Serial.begin(9600);
    
    

  }
  
for (int i = 0; i < NUM_SEGMENTS; i++) {
    
    pinMode(ledPins[i], OUTPUT);
    
  }
  
}


void loop() {


if (rouletteState) {
    
     Serial.println("WORKING");
  
    counter0++; 
     digitalWrite(A,segmentDisp[counter0][0]);
      digitalWrite(B,segmentDisp[counter0][1]);
       digitalWrite(C,segmentDisp[counter0][2]);
        digitalWrite(D,segmentDisp[counter0][3]);
         digitalWrite(E,segmentDisp[counter0][4]);
          digitalWrite(F,segmentDisp[counter0][5]);
           digitalWrite(G,segmentDisp[counter0][6]);
           delay(200);
        
  
  } else {
  digitalWrite(A,segmentDisp[counter0][0]);
      digitalWrite(B,segmentDisp[counter0][1]);
       digitalWrite(C,segmentDisp[counter0][2]);
        digitalWrite(D,segmentDisp[counter0][3]);
         digitalWrite(E,segmentDisp[counter0][4]);
          digitalWrite(F,segmentDisp[counter0][5]);
           digitalWrite(G,segmentDisp[counter0][6]);
  }
  
  if (counter0 > 9) {
    counter0 = 0;
}

}
#define NUM_SEGMENTS 7

const byte ledPins[NUM_SEGMENTS] = {8, 10, 11, 4, 5, 6, 7};
byte counter0 = 0;
volatile bool rouletteState = false;

byte segmentDisp[] = {
  // A  B  C  D  E  F  G
  B1111110,// 0
  B0110000,// 1
  B1101101,// 2
  B1111001,// 3
  B0110011,// 4
  B1011011,// 5
  B1011111,// 6
  B1110000,// 7
  B1111111,// 8
  B1111011 // 9
};

void startRoulette() {
  if(!rouletteState)rouletteState = true;
}

void endRoulette() {
  if(rouletteState)rouletteState = false;
}

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  for (int i = 0; i < NUM_SEGMENTS; i++)
    pinMode(ledPins[i], OUTPUT);

  attachInterrupt(digitalPinToInterrupt(3), endRoulette, FALLING);
  attachInterrupt(digitalPinToInterrupt(2), startRoulette, FALLING);
}

void loop() {
  if (rouletteState) {
    ++counter0 %= 10;
    for (int i = 0; i < NUM_SEGMENTS; i++)digitalWrite(ledPins[i], segmentDisp[counter0] & (1 << i));
    delay(200);
  }
}

Thank you for the reply!

Unfortunately, when I upload your code it runs correctly, but it doesn't display correctly on my 7-segment display.

The scrolling effect displayed correctly when using my code before.

Does your code require the 7-segment display to be wired differently?

In what way does it not work?

The issue seems to be doing the attachInterrupts before setting the pinMode on the buttonPins.

Do a single Serial.begin at the start up of setup, don't call it once for each button setup.

Your else in the main loop doesn't seem to serve any purpose.

You can shorten all those digitalWrites using a loop and your existing ledPins array.

You should range check counter0 before using it to display your digit.

Using short all capital #defines is asking for trouble.

The below works with a common cathode display, 470R resistors in series with the segments.

#include <Arduino.h>

#define NUM_BUTTONS 2
#define NUM_SEGMENTS 7


#define pinA 8
#define pinB 10
#define pinC 11
#define pinD 4
#define pinE 5
#define pinF 6
#define pinG 7



int buttonPins[NUM_BUTTONS] = { 2, 3 };
int ledPins[NUM_SEGMENTS] = { pinA, pinB, pinC, pinD, pinE, pinF, pinG };
int counter0 = 0;
volatile bool rouletteState = 0;

int segmentDisp[10][7] = {
   // A  B  C  D  E  F  G
   { 1, 1, 1, 1, 1, 1, 0 },  // 0
   { 0, 1, 1, 0, 0, 0, 0 },  // 1
   { 1, 1, 0, 1, 1, 0, 1 },  // 2
   { 1, 1, 1, 1, 0, 0, 1 },  // 3
   { 0, 1, 1, 0, 0, 1, 1 },  // 4
   { 1, 0, 1, 1, 0, 1, 1 },  // 5
   { 1, 0, 1, 1, 1, 1, 1 },  // 6
   { 1, 1, 1, 0, 0, 0, 0 },  // 7
   { 1, 1, 1, 1, 1, 1, 1 },  // 8
   { 1, 1, 1, 1, 0, 1, 1 }   // 9

};

void startRoulette() {
   rouletteState = true;
}

void endRoulette() {
   rouletteState = false;
}

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

   for( int i = 0; i < NUM_BUTTONS; i++ ) {
      pinMode(buttonPins[i], INPUT_PULLUP);
   }
   attachInterrupt(digitalPinToInterrupt(3), endRoulette, FALLING);
   attachInterrupt(digitalPinToInterrupt(2), startRoulette, FALLING);

   for( int i = 0; i < NUM_SEGMENTS; i++ ) {
      pinMode(ledPins[i], OUTPUT);
   }
}


void loop() {


   if( rouletteState ) {

      Serial.println("WORKING");

      if( ++counter0 > 9 ) {
         counter0 = 0;
      }
      
      for( int i = 0; i < NUM_SEGMENTS; i++ ) {
         digitalWrite(ledPins[i], segmentDisp[counter0][i]);
      }
      delay(200);
   }
}

The actual numbers on the display were not displaying correctly with kolaha's code; the display was not turning on all of the segments correctly.

show?
and i changed bit order. try now. and show again

Thank you for the thorough advice!

I can't believe I've been butting my head against a wall for so long because I put the interrupts above the pinMode. Thank you.

These are what display with my setup. This is with your edited code, but your previous code displayed similarly.


This should be outside the if brackets {}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.