I’m fairly new to Arduino and I’m having a hard time getting around the interrupts. Basically I have three buttons on pins 2,3,4 which start, stop and reset a timer respectively. The timer count is shown on a double seven seg display via two shift registers. Right now I have an example where the buttons are for incrementing, decrementing and resetting the display without a timer, and I want to modify that.
This is my code. The first and most important problem is that the button interrupts no longer work (except for the third one). For some reason, the first two buttons stop the timer, but then nothing else works, not even the third button, so I’m assuming something went bad and it’s not stopping the interrupt. Why is that? And secondly the timer is running at the processor speed which is so fast that my display shows seemingly random numbers. How can I make its pace at one second?
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
const int latchPin1 = 7;
const int clockPin1 = 8;
const int dataPin1 = 9;
const int latchPin2 = 10;
const int clockPin2 = 11;
const int dataPin2 = 12;
volatile int digit1 = 0;
volatile int digit2 = 0;
const int numbers[] = {63, 6, 91, 79, 102, 109, 125, 7, 127, 111};
int nr = 0;
int start = 1;
int reset = 0;
void setup() {
//activare int0 si int1
EIMSK |= (1 << INT0);
EIMSK |= (1 << INT1);
//activare PCI2 (buton 3 - PCINT20 - PCI2)
PCICR |= (1 << PCIE2);
PCMSK2 |= (1<<PCINT20);
TIMSK1 = (1<< TOIE1);
TCCR1B |= (1<<CS10);
sei();
pinMode(latchPin1, OUTPUT);
pinMode(dataPin1, OUTPUT);
pinMode(clockPin1, OUTPUT);
pinMode(latchPin2, OUTPUT);
pinMode(dataPin2, OUTPUT);
pinMode(clockPin2, OUTPUT);
Serial.begin(9600);
}
void loop(){
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, MSBFIRST, numbers[digit1]);
digitalWrite(latchPin1, HIGH);
digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, MSBFIRST, numbers[digit2]);
digitalWrite(latchPin2, HIGH);
delay(1000);
}
ISR (TIMER1_OVF_vect){
if(start){
digit1++;
if(digit1>9){
digit1=0;
digit2++;}
if(digit2>9){
digit2=0;}
}
}
ISR(INT0_vect) {
start = 1;
Serial.println("1");
}
ISR(INT1_vect) {
start = 0;
Serial.println("2");
}
ISR(PCINT2_vect) {
digit1==0;
digit2==0;
start = 1;
Serial.println("3");
}
Thanks in advance for your time and answers!