Question.7
What will happen after executing the below code on Arduino UNO?
Note: Consider no noise is created from button press.
Code
#define LED_PIN 13
#define SWITCH1 4
#define SWITCH2 5
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(SWITCH1, INPUT_PULLUP);
pinMode(SWITCH2, INPUT_PULLUP);
// Configure Timer1 in CTC (Clear Timer on Compare Match) mode
TCCR1A = 0; // Ensure no settings in TCCR1A (Normal mode)
TCCR1B = (1 << WGM12) | (1 << CS12) | (1 << CS10); // CTC mode, Prescaler = 1024
OCR1A = 7812; // Set compare match value for approximately 500 ms delay
TIMSK1 |= (1 << OCIE1A); // Enable Timer1 compare match interrupt
sei();
}
void loop() {
if (digitalRead(SWITCH1) == LOW) cli();
if (digitalRead(SWITCH2) == LOW) sei();
}
// Interrupt Service Routine (ISR) for Timer1 Compare Match A
ISR(TIMER1_COMPA_vect) {
digitalWrite(LED_PIN, !digitalRead(LED_PIN)); // Toggle the LED state on every timer interrupt
}