# Counting UP/DOWN, START/STOP problem using interrupt

so to keep it simple, I'm using Arduino UNO and I have a circuit with two push buttons, one for UP/DOWN and the other for START/STOP, wired to one interrupt and a seven segment display, at each press of UP/DOWN, the direction of counting should change to UP or DOWN, and at each press of START/DOWN, stops counting or continues from the last counting value. when the number exceed 9 we use hex(A, B, C, D, E and F). So I tried this following code, but the problem is when I press the UP/DOWN, it starts counting down but it never changes direction, and when I press the START/STOP it doesn't stop the count nor continues. this is what I did:

``````#define interruptPin 2
#define button1 8
#define button2 9

bool flag = false;//status
int outerCounter = 0;
int innerCounter = 0;
int segmentPins[] = {5, 6, 7, 10, 11, 12, 13};

//sequence of numbers starting from 1 to 15
int hexSequence[15][7] = {{12,11}, {13, 12, 10, 7, 5}, {13, 12, 11, 10, 5}, {12, 11, 6, 5},
{13, 6, 5, 11, 10}, {13, 6, 7, 10, 11, 5}, {13, 12, 11}, {13, 12, 11, 10, 7, 6, 5},
{13, 12, 11, 10, 5, 6}, {13, 12, 11, 7, 6, 5}, {11, 10, 7, 6, 5}, {13, 6, 7, 10},
{12, 11, 10, 7, 5}, {13, 10 ,7, 6, 5}, {13, 7, 6, 5}};
//a13, b12, c11, d10, e7, f6, g5

void setup() {
//Setting up the pins
for (int i = 0; i < 7; i++)
{
pinMode(segmentPins[i], OUTPUT);
}
pinMode(button1, INPUT);
pinMode(button2, INPUT);

attachInterrupt(digitalPinToInterrupt(interruptPin), interruptFunction, RISING);
}

void loop() {
while (true){
buttonUpDown();
startStopButton();
}
}

void countUp() {
for (int outerCounter; outerCounter < 15; outerCounter++){
for (int innerCounter = 0; innerCounter < 7; innerCounter++) {
digitalWrite(hexSequence[outerCounter][innerCounter], HIGH);
}
delay(1000);
turnAllOff();
}
}

void countDown() {
for (int outerCounter = 15; outerCounter > 0; outerCounter--){
for (int innerCounter = 0; innerCounter <7; innerCounter++) {
digitalWrite(hexSequence[outerCounter][innerCounter], HIGH);
}
delay(1000);
turnAllOff();
}
}
void turnAllOff() {
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], LOW);
}
}

//Buttons 1&2 functions
void buttonUpDown() {
if (flag == false) {
flag = true;
countUp();
}
else {
flag = false;
countDown();
}
delay(1500);
}
}
void startStopButton(){
if (flag == false) {
flag = true;
countUp();
}
else {
flag = false;
turnAllOff();
}
delay(1000);
}
}

void interruptFunction() {
buttonUpDown();
}
• You call `delay()` from your ISR, that doesn't work. In the ISR just read the pins and set a corresponding flag. The in the main code check that flag and do the wanted action Nov 25 at 20:26
• @TheDovahkiin no, you should not call `delay()` in the interrupt function. Especially not a 1 second delay. Nov 25 at 23:12