UPDATE: Still no clue how interrupts work, but I've removed the delay in my interrupt
Hey all,
I'm switching a project from an attiny85 to an attiny84 and I'm totally lost on how to translate the interrupts.
I've scoured the internet for any advice or guidance but they either point to the datasheet (two pages of assembly code I don't understand yet) or a guide for the 85 series which is completely different. So I am asking for guidance here on where I can learn about creating a single external interrupt pin on the Attiny84 for a button.
Using advice from this post, I tried to create a simple blink sketch that will blink an LED when the microcontroller starts and again when the button connected to the interrupt pin is pressed. The LED blinks at start, but that's it.
// INTERRUPT STUFF
#include <avr/io.h>
#include <avr/interrupt.h>
#define INTERRUPT_PIN PCINT0
#define INT_PIN PA0
#define PCINT_VECTOR PCINT0_vect
//LED
int LedPin = 0;
void setup() {
// INTERRUPT STUFF
cli(); // Disable interrupts during setup
GIMSK |= (1 << PCIE0);
PCMSK1 |= (1 << PCINT0);
pinMode(INT_PIN, INPUT_PULLUP); // Set our interrupt pin as input with a pullup to keep it stable
sei(); //last line of setup - enable interrupts after setup
//BLINK
pinMode(LedPin, OUTPUT);
digitalWrite(LedPin, HIGH);
delay(1000);
digitalWrite(LedPin, LOW);
}
void loop() {
delay(5000); // just some random delay. if I hit the button a bunch the led should come on at some point. but it doesn't
digitalWrite(LedPin, LOW);
}
ISR(PCINT_VECTOR) {
digitalWrite(LedPin, HIGH);
}
Let it be known I'm very new to interrupts and Bit manipulation (I also read through a brief guide on that). Whenever someone says in a forum or datasheet to "Enable PCINT" for example I have no clue how that translates to Arduino IDE code.
Any advice on where to learn about this subject or at least start to understand the vocabulary would be greatly appreciated. Thanks for your time.
You cannot call delay() from an interrupt since interrupts are disabled during an interrupt!!! delay() relies on interrupts. Also, you do not want to spend 1 second in an interrupt.
You shouldn't need an interrupt for a button anyhow. Just monitor the button input in loop().
Ah right, rookie mistake with the delays haha, tried to whip up a quick example and forgot about that entirely.
But yes I still do need interrupts, in the final project they will be used to perform other things the loop can't really do so this is my only option
Granted, for this particular problem, use of a pin change interrupt is appropriate and has a small power saving advantage over the watchdog approach.
I don't have an '84 to test, but your program may actually be working. This is because the call to delay() is simply skipped when used in an interrupt (at least, it is on an Arduino Uno), so in the ISR the LED is turned on, then off again a microsecond later. You will never see the flash.
Best to avoid doing I/O of any sort within an interrupt routine, and in this particular case there is no point in doing anything at all in the ISR. Just use it to wake the processor and let loop() do the work.
Noted, thanks. I've since rewritten the code so that it just turns on the LED when you press the button and the main loop has a huge delay to turn it off. Still unfortunately doesn't work.
The main issue is I'm not sure if the code I wrote for enabling the interrupt is actually correct. I've just gathered info from random forums online like the one mentioned in the post and tried putting that together. I'm not sure where people are getting the info to know how to make them from scratch.
Sorry, I don't see an obvious problem with the code.
The processor data sheet is the best source of information, then you need to consult the Arduino reference to figure out how to handle relatively obscure features like interrupts.
Make sure that the processor is correctly identified for whatever Arduino core you are using.
You might try another core, or consult the developer of the one you are using, as it seems likely that there could be a problem there.