#include <TinyDebug.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define INTERRUPT_PIN PCINT1 // This is PB1 per the schematic
#define INT_PIN PB1 // Interrupt pin of choice: PB1 (same as PCINT1) - Pin 6
#define LED_PIN PB4 // PB4 - Pin 3
#define PCINT_VECTOR PCINT0_vect // This step is not necessary - it's a naming thing for clarit
bool previousInterruptPinState = false; // false - low, true - high
int myLineCounter = 0;
void myDebugPrint(String message)
{
Debug.print(String(myLineCounter));
Debug.println(" :" + message);
myLineCounter++;
}
void setup() {
Debug.begin();
myDebugPrint("Hello, TinyDebug!");
pinMode(LED_PIN, OUTPUT);
cli(); // Disable interrupts during setup
PCMSK |= (1 << 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_PULLUP); // Set our interrupt pin as input with a pullup to keep it stable
sei(); //last line of setup - enable interrupts after setup
}
void loop() {
/* Sprinkle some magic code here */
if ( previousInterruptPinState == HIGH ) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}
// This is the interrupt handler called when there is any change on the INT_PIN
// ISR is defined in the headers - the ATtiny85 only has one handler
ISR(PCINT_VECTOR)
{
myDebugPrint("ISR");
// measure pin
if (previousInterruptPinState != digitalRead(INT_PIN))
{
previousInterruptPinState = digitalRead(INT_PIN);
}
}