//https://docs.wokwi.com/parts/wokwi-attiny85 simulate ATTINY85 on wokwi
//https://wokwi.com/projects/317140124437578305 get interrupts working
// https://wokwi.com/projects/318885209198035539 debounce function.
// https://thewanderingengineer.com/2014/08/11/pin-change-interrupts-on-attiny85/ datasheet attiny85 ints.
//https://electronics.stackexchange.com/questions/264434/attiny85-interrupt-confusion "only PB2 on attiny85 can be used to trap edge changes"
#include <TinyDebug.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define INTERRUPT_PIN PCINT2
#define clockin PB2 //use digital I/O pin PB1 (pin 6 of IC) for input
#define INT_VECTOR INT0_vect
#define LED PB0
volatile byte x = 0;
const int debounceDelay = 10; // milliseconds to wait until button input stable *
void setup() {
pinMode(LED, OUTPUT); //PB0 is GPIO output
cli(); // Disable interrupts during setup
PCMSK = 0b00000100; //enable PB2/PCINT2 as our interrupt pin.
GIMSK = 0b01100000; // enable ext pin interrupts; enable pin changes to be treated as interrupts.
MCUCR = 0b00000011; // int rising edge only. In wiwok unclick "bounce"
pinMode(clockin, INPUT_PULLUP); // Set our interrupt pin as input with a pullup
sei();
Debug.begin();
//Debug.println("you are beginning the sketch");
}
void loop() {
Debug.print("value of X is: ");
Debug.println(x);
}
ISR(INT_VECTOR) {
if (debounce(clockin))
{
x++;
delay(1000);
}
}
boolean debounce(int pin)
{
boolean state;
boolean previousState;
previousState = digitalRead(pin); // store switch state
for (int counter = 0; counter < debounceDelay; counter++)
{
delay(100); // wait for 1 millisecond
Debug.println("you are in debounce function.");
state = digitalRead(pin); // read the pin
if ( state != previousState)
{
counter = 0; // reset the counter if the state changes
previousState = state; // and save the current state
}
}
// here when the switch state has been stable longer than the debounce period
return state;
}