ISR(TIMER2_COMPA_vect) {
slow_counter++;
if (slow_counter >= 125) { // Adjust this value to control the speed of chasing
slow_counter = 0;
if (current_pattern == 0) {
chase_leds(); // Call chase_leds if current pattern is chase_leds
} else {
stay_lit_chase_leds(); // Call stay_lit_chase_leds otherwise
}
}
}
ISR(INT1_vect) {
// Toggle between regular chase and stay-lit chase patterns
if (current_pattern == 0) {
current_pattern = 1; // Change to stay-lit chase pattern
} else {
current_pattern = 0; // Change to regular chase pattern
}
}
void setup() {
DDRD = 0x3F; // Set PD0-2, PD4-5 as output (0b00111111)
DDRB = 0x07; // Set PB0-PB2 as output
PORTD = 0x08; // Enable pull-up resistor on PD3
InitTimer();
adc_init();
// Enable global interrupts
sei();
// Configure external interrupt INT0 for the button at PD4
EICRA |= (1 << ISC00); // Trigger on any logic change
EIMSK |= (1 << INT1); // Enable INT1 interrupt
// Initialize serial communication
Serial.begin(57600);
}
int main(void) {
setup();
while (1) {
// Main loop
}
return 0;
}
I’m really clueless in this Timer and Interrupt topic. So i really don’t know how to do it.
So I’m trying to add a button to change the pattern. I’m only permitted to use PD2 and 3 for my button. I don’t know why is not registering any button press right now and it wont change my pattern.
Ben Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
Given the amount of unseen code here there may be multiple issues, but lack of switch debounce is certainly one of them.
In this case, ignoring interrupts until the timer interrupt occurs is a simple method of debounce. You could achieve that by using a flag sti in one handler and cleared in the other or more simply by disabling the button interrupt until the timer interrupt thus:
ISR(TIMER2_COMPA_vect)
{
// Enable button interrupt
EIMSK |= (1 << INT1);
...
}
ISR(INT1_vect)
{
// Disable button interrupt
EIMSK &= ~(1 << INT1);
...
}
In that way, the pattern flag cannot be toggled repeatedly between pattern updates, through either switch-bounce or multiple activations.
Additionally:
EICRA |= (1 << ISC00); // Trigger on any logic change
Is probably wrong. You do not want to toggle on press, then again on release, you end up with no change. Suggest:
EICRA |= (1 << ISC00) | (1 << ISC01) ; // Trigger on rising edge.
… or falling-edge, depending on how the button is wired and whether you want the change in button press or button release.