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;
}
}