Search code examples
c++carduinoattiny

ATTiny85 Interrupts in Arduino IDE


I have an ATTiny85 which I program using a sparkfun programmer (https://www.sparkfun.com/products/11801) and the ATTiny Board Manager I am using is: https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json

Below is my code, I am having trouble getting the interrupt to work when I ground Pin 2. I have tested the LED does work outside of the interrupt (inside the loop). Any suggestions are welcome.

#include "Arduino.h"

#define interruptPin 2

const int led = 3;
bool lastState = 0;

void setup() {
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(interruptPin, pulseIsr, CHANGE);
  pinMode(led, OUTPUT);
}

void loop() {

}


void pulseIsr() {
    if (!lastState) {
      digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
      lastState = 1;
    }
    else {
      digitalWrite(led, LOW);  // turn the LED off by making the voltage LOW
      lastState = 0;
    }
}

Solution

  • I was way off.

    Here is how to set up an interrupt on the ATTiny85 using the Arduino IDE (this example uses digital pin 4 (pin 3 on the chip):

    #include "Arduino.h"
    
    const byte interruptPin = 4;
    const byte led = 3;
    bool lastState = false;
    
    ISR (PCINT0_vect) // this is the Interrupt Service Routine
    {
      if (!lastState) {
        digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
        lastState = true;
      }
      else {
        digitalWrite(led, LOW);  // turn the LED off by making the voltage LOW
        lastState = false;
      }
    }
    
    void setup() {
      pinMode(interruptPin, INPUT_PULLUP); // set to input with an internal pullup resistor (HIGH when switch is open)
      pinMode(led, OUTPUT);
    
      // interrupts
      PCMSK  |= bit (PCINT4);  // want pin D4 / pin 3
      GIFR   |= bit (PCIF);    // clear any outstanding interrupts
      GIMSK  |= bit (PCIE);    // enable pin change interrupts 
    }
    
    void loop() {
    
    }