# Binary Counter with Interrupt

Hi, this is a class project in which we create a binary counter using 4 LEDs and a button. The first button triggers the lights, as you hit the button each time it counts up by one. When you hit the second button it acts a switch and the Interrupt is activated. This interrupt now changes it so when you use the first button it counts down each time instead of up.

This is my code and I can’t figure out how to get the interrupt to work. Any suggestions would be great. I’ve tried like five different ideas and still can’t get it.

``````int ledPins[] = {4,5,6,7};  //Pins in 1-4 array
int inputPin1 = 2; //First button
int inputPin2 = 3; //Second button for interrupt
int readPin1;     //creates variable for the input of button 1
int before = LOW; //the previous reading from the input pin
volatile int a = 0;

long time = 0;  //time since button 1 was pressed
long debounce = 400; //debounce time, increase if the output flickers

byte data = 0; //Used with buttonCount
byte vary = 1;
int buttonCount = 0; //increment when the button is pushed
int x = 0;          //creates index

void setup() {
for(int i=0;i++;i<4) pinMode(ledPins[i],OUTPUT);  //LEDs are now outputs
pinMode(inputPin1, INPUT);  //button to count up
pinMode(inputPin2, INPUT);  //button to trigger interrupt
attachInterrupt(1,ReverseCount,RISING);
}

void loop(){
//debouncing
if (readPin1 == LOW && before == HIGH && (millis() - time)>debounce && a==0) {
buttonCount++; //incrementing by +1
time = millis();
//interrupt
}
else if (readPin1 == LOW && before == HIGH && (millis() - time)>debounce && a!=0){
buttonCount--;
time = millis();
}
data = byte(buttonCount);
x = 0;
for (vary = 00000001;vary>0;vary<<=1){
if(data & vary){
digitalWrite(ledPins[x],HIGH);
}
else digitalWrite(ledPins[x],LOW);
x++;
}
}

void ReverseCount() {
//debouncing
if (readPin2 == LOW && before == HIGH && (millis() - time)>debounce && a==0) {
a==1;
}
}
``````

To get help, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.

You can still edit your post and use the proper posting technique.

BTW when doing calculations using millis() use: unsigned long types.

How are your switches wired?

a == 1 is not the same as a = 1 Thanks! Changing a==1 to a=1 worked If you have time, would you mind explaining why that's important? Also, why the millis() thing should be unsigned? Is it just less like to get errors?

== is for comparisons = is for assignments
Edit:
https://www.arduino.cc/en/Reference/Assignment