1

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;
    }
}

2 Answers 2

1

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() {

}
0

I see one possible error with your Boolean data type. Boolean data types are either true or false. I see that your using it for variable lastState. The initialization to equal 0 is something that I don't think the compiler allows. Maybe you should try setting the bool variable to the following...

bool lastState = true;

if (!lastState) {
// what you want your code to perform if lastState is FALSE
}
else {
//what else you want your code to perform if lastState is TRUE
}

OR

bool lastState = true;

if (lastState) {
// what you want your code to perform if lastState is TRUE
}
else {
//what else you want your code to perform if lastState is FALSE
}

Here is a helpful pdf on Boolean data types, available for download through my Nextcloud instance.

https://nextcloud.point2this.com/index.php/s/so3C7CzzoX3nkzQ

I know this doesn't fully fix your problem, but hopefully this will help some.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.