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 readPin2;
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(){
readPin1=digitalRead(inputPin1); //read button 1
//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();
}
before = readPin1;
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() {
readPin2=digitalRead(inputPin2); //read button 2
//debouncing
if (readPin2 == LOW && before == HIGH && (millis() - time)>debounce && a==0) {
a==1;
}
}