/* Program 6-2: Counting Interrupts while the main program toggles an LED continuously * SW1 is connected to pin 3 (interrupt 2) of Arduino * count up every time SW1 interrupts the main program */ const byte SW1 = 3; //SW1 is connected to interrupt pin#3 volatile byte count = LOW; void setup() { pinMode(13, OUTPUT); /* set pin 13 as output */ pinMode(SW1, INPUT); //pin as input Serial.begin(9600); attachInterrupt(digitalPinToInterrupt(SW1), myISR, RISING); } //main loop toggles LED continuously void loop() { digitalWrite(13,HIGH); /* turn on the LED */ delay(500); /* delay 500 ms */ digitalWrite(13,LOW); /* turn off the LED */ delay(500); /* delay 500 ms */ } //Interrupt Service Routine(ISR) //Count up every time SW1 interrupts the main program void myISR() { count++; //count up upon pressing SW1 Serial.println(count); // Display count on Serial Monitor }