1

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(digitalRead(button1) == HIGH){
      if (flag == false) {
        flag = true;
        countUp();  
      }
      else {
        flag = false;
        countDown();
      }
      delay(1500);
      }
}
void startStopButton(){
  if (digitalRead(button2) == HIGH) {
    if (flag == false) {
        flag = true;
        countUp();
      }
      else {
        flag = false;
        turnAllOff();
      }
      delay(1000);
      }
}

void interruptFunction() {
  if (digitalRead(button1)) {
    buttonUpDown();
  }
  if (digitalRead(button2)) {
    startStopButton();
  }
}
5
  • 1
    1. How are your buttons wired so they only use one interrupt? 2. You really don't want to be calling your main code from your interrupt - that's just backwards. The interrupt changes the mode, the main code runs from loop().
    – Majenko
    Nov 25 at 20:06
  • @Majenko yes I forgot to say it but i'm using one interrupt Nov 25 at 20:11
  • 1
    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
    – chrisl
    Nov 25 at 20:26
  • @chrisl so i should delay inside of the interrupt function ? and then call the flag inside of the main ? correct ? Nov 25 at 20:44
  • 2
    @TheDovahkiin no, you should not call delay() in the interrupt function. Especially not a 1 second delay.
    – Sim Son
    Nov 25 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.