// Requires headers for AVR defines and ISR function
#include <avr/io.h>
#include <avr/interrupt.h>
// Define pins for switch the LED, plus the chosen interrupt for reacting to
#define INTERRUPT_PIN PCINT3 // This is PB3 per the schematic
#define INT_PIN PB3 // Interrupt pin of choice: PB3 (same as PCINT3) - Pin 2
#define OUT_PIN PB1 // PB1 - Pin 6
bool FIRE_FLAG;
int delaySet =2000; // delay time
/*
* Alias for the ISR: "PCINT_VECTOR" (Note: There is only one PCINT ISR.
* PCINT0 in the name for the ISR was confusing to me at first,
* hence the Alias, but it's how the datasheet refers to it)
*/
#define PCINT_VECTOR PCINT0_vect // This step is not necessary - it's a naming thing for clarity
// FYI: Variables used within ISR must be declared Volatile.
// static volatile byte LEDState;
// The setup function runs only once when the ALU boots
void setup() {
// Code here is the key piece of configuring and enabling the interrupt
cli(); // Disable interrupts during setup
PCMSK |= (3 << INTERRUPT_PIN); // Enable interrupt handler (ISR) for our chosen interrupt pin (PCINT1/PB1/pin 6)
GIMSK |= (1 << PCIE); // Enable PCINT interrupt in the general interrupt mask
pinMode(INT_PIN, INPUT); // Set our interrupt pin as input
sei(); //last line of setup - enable interrupts after setup
digitalWrite(OUT_PIN, HIGH);
delay(1000);
digitalWrite(OUT_PIN, LOW);
delay(1000);
}
void loop() {
// put your main code here, to run repeatedly:
if (FIRE_FLAG == true){
digitalWrite(OUT_PIN, HIGH);
delay(delaySet);
digitalWrite(OUT_PIN, LOW);
delay(2000);
FIRE_FLAG = false;
}
}
ISR(PCINT_VECTOR)
{
FIRE_FLAG = true;
}