//https://docs.wokwi.com/parts/wokwi-attiny85 simulate ATTINY85 on wokwi
//https://wokwi.com/projects/317140124437578305 get interrupts working
//https://www.arduino.cc/reference/en/language/functions/time/millis/ millis() in arduinoland.
// https://wokwi.com/projects/318885209198035539 debounce function.
// https://www.gadgetronicx.com/attiny85-external-pin-change-interrupt/ 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 clockin PB2 //use digital I/O pin PB1 (pin 6 of IC) for input
#define INT0_VECTOR INT0_vect
#define LED PB0
#define POT A3
#define TRIGGER_TIME_WANTED_PIN A2
volatile byte x = 0;
byte oldx = 0;
int timer; // for outputting trigger of timer ms.
int potValue = 0; //the right pot sets MODE for the MCU
int triggerValue = 0; // the left pot traps time of trigger (MS)
void setup() {
pinMode(clockin, INPUT_PULLUP); // Set our interrupt pin as input with a pullup
pinMode(LED, OUTPUT); //PB0 is GPIO output
cli(); // Disable interrupts during setup
PCMSK = 0b00000100; //enable PB2/PCINT2 as our interrupt pin.
GIMSK = 0b01000000; // enable ext pin interrupts; enable pin changes to be treated as interrupts.
MCUCR = 0b00000010; // int rising fall only. In wiwok unclick "bounce" DEFAULT
sei();
Debug.begin();
//Debug.println("you are beginning the sketch");
}
void loop() {
//read pot value -- what mode are we in.
potValue = analogRead(POT);
//display value and light LED when pot is < 901
if (x != oldx){
oldx = x;
if (potValue < 901){
Debug.println(x);
digitalWrite(LED, x % 2);
}
if (potValue >=901){
MCUCR = 0b00000010; // Interrupt on falling edge only.
timer = analogRead(TRIGGER_TIME_WANTED_PIN);
digitalWrite(LED,LOW); //Turn LED off in case its on from previous
digitalWrite(LED,HIGH); //Turn LED on
Debug.println(x);
delay(timer);
digitalWrite(LED,LOW); //Turn LED off
}
if (x == 4) {
x = 0;
}
} //end main if.
//trap values of pot and do things.
//Debug.print("Value of pot is: ");
//Debug.println(potValue);
if (potValue < 300) {
MCUCR = 0b00000011; // Interrupt on rising edge only.
}
if (potValue >= 301 && potValue < 500) {
MCUCR = 0b00000010; // Interrupt on falling edge only.
}
if ((potValue > 501) && (potValue < 901)) {
MCUCR = 0b00000001; // Interrupt on change.
}
} //end main loop.
ISR(INT0_VECTOR) {
x++;
}