Changing an Interrupt

I am trying to make a 4 bit binary counter with two buttons using interrupts. The first button should count up until the second button is pressed, then the first button should count down. I have the code written for changing the LED's used in the counter, but I am not sure how to change the interrupts. I'm not sure if they can be changed and if they can be changed, can you have two interrupts in the same function. Any help would be appreciated.

Part_2.ino (2.53 KB)

It's better if you post your code, rather than attach it. And it must be posted between code tags. </> in the "Reply" dialog.
When it's just attached, we have to first create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the "Temp" folder and manually remove it. It's much easier to just view the code in your post.

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int buttonPin2 = 3;
const byte numPins = 4;
byte pins[] = {4,5,6,7};
int presses = 0;
long time = 0;
long debounce = 100;
int state;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);

for(int i = 0; i < numPins; i++) {
    pinMode(pins[i], OUTPUT);
  }
  /* use pin 2 which has interrupt 0 on Arduino UNO */
  attachInterrupt(0, count, LOW);
  attachInterrupt(1, count, LOW);

}


void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);

if (presses < 0) {
    presses = 15;
       String binNumber = String(presses, BIN);
  /* get the length of the string */
  int binLength = binNumber.length(); 
                // here is the scary code
    for(int i = 0, x = 1; i < binLength; i++, x+=2) { 
      if(binNumber[i] == '0') state = LOW;
      if(binNumber[i] == '1') state = HIGH;
      digitalWrite(pins[i] + binLength - x, state);
    } 
    digitalWrite(pins[1], HIGH);
    digitalWrite(pins[2], HIGH);
    digitalWrite(pins[3], HIGH);
    digitalWrite(pins[4], HIGH);
}


/* convert presses to binary and store it as a string */
  if(presses <= 15 && presses >= 1) {  // if we have less or equal to 15 presses
   String binNumber = String(presses, BIN);
  /* get the length of the string */
  int binLength = binNumber.length(); 
                // here is the scary code
    for(int i = 0, x = 1; i < binLength; i++, x+=2) { 
      if(binNumber[i] == '0') state = LOW;
      if(binNumber[i] == '1') state = HIGH;
      digitalWrite(pins[i] + binLength - x, state);
    } 
  }
else {
    presses = 0;
       String binNumber = String(presses, BIN);
  /* get the length of the string */
  int binLength = binNumber.length(); 
                // here is the scary code
    for(int i = 0, x = 1; i < binLength; i++, x+=2) { 
      if(binNumber[i] == '0') state = LOW;
      if(binNumber[i] == '1') state = HIGH;
      digitalWrite(pins[i] + binLength - x, state);
    } 
    digitalWrite(pins[1], LOW);
    digitalWrite(pins[2], LOW);
    digitalWrite(pins[3], LOW);
    digitalWrite(pins[4], LOW);
}
}


/* function to count the presses */

void count() { 
  // we debounce the button and increase the presses
if (buttonPin2 = HIGH) {

}
}
  
  if(millis() - time > debounce)  presses++;
  time = millis();
  
}

Two questions,

  1. are you doing this just to learn something about interrupts?
  2. will the buttons get pressed at human or machine rates?

If not #1 and #2 is human rates then you really don't need to use interrupts even to run 16 or more buttons along with a status led and some serial output. That might take more than 1% or 2% of the capability of an Uno if the buttons get a lot of use.

There is ways as long as you don't use delays!

I understand that this can be done without interrupts but I don't really understand binary well enough to write code for it. Interrupts are being used later on in the class and this project is an introduction to them. The buttons are being pressed at human rates.

  attachInterrupt(0, count, LOW);
  attachInterrupt(1, count, LOW);

You are using the function "count" for two different purposes. And also you should use FALLING and not LOW.

The second interrupt handler should be a function that sets a flag (eg. countUp). If true, the "count" function counts up, otherwise it counts down.

I'm not going to do your homework for you, but that should give you some ideas.

SolarSailer:
I understand that this can be done without interrupts but I don't really understand binary well enough to write code for it. Interrupts are being used later on in the class and this project is an introduction to them. The buttons are being pressed at human rates.

Understood, you do need to do this for your education.

Come back later and learn what's behind event-driven (real time) code, the principle is simple.

If you want, I have 3 addresses to very good, commonsense and complete tutorials on the subject (not my site, I just appreciate that work) in my signature space below. Maybe share them with your teacher?

I got the counter to work but realized that there is a problem counting down. For example the counter will display 0100 (4) and then 0111 (7) when it should display 0011 (3). It will go from 0000 (0) to 1111 (15) without any problem so I think the problem is that the led's are not turning off when they should. Based on this I think the problem is in this part of the code.

   String binNumber = String(presses, BIN);
  /* get the length of the string */
  int binLength = binNumber.length(); 
                // here is the scary code
    for(int i = 0, x = 1; i < binLength; i++, x+=2) { 
      if(binNumber[i] == '0') state = LOW;
      if(binNumber[i] == '1') state = HIGH;
      digitalWrite(pins[i] + binLength - x, state);
   String binNumber = String(presses, BIN);
  /* get the length of the string */
  int binLength = binNumber.length(); 
                // here is the scary code
    for(int i = 0, x = 1; i < binLength; i++, x+=2) { 
      if(binNumber[i] == '0') state = LOW;
      if(binNumber[i] == '1') state = HIGH;
      digitalWrite(pins[i] + binLength - x, state);

Where to begin? You're getting at bits by a route that tells me you need some learning. What you do there is done easier and far far far faster directly.

A lesson that will save you hours and hours (especially from the path you're on) and give you programming power you haven't imagined.
http://playground.arduino.cc/Code/BitMath

A byte is an array of bits. Use bit logic to read/set/operate on any and all of them in 1 or very few cycles.