// Attiny85 sleep, interrupt with servo
// https://forum.arduino.cc/t/attiny85-sleep-interrupt-with-servo/1399429
/*
TinyDebug usage example: ATtiny85 debug prints in the Wokwi Simulator.
*/
#include "TinyDebug.h"
// #include <avr/io.h>
// #include <avr/interrupt.h>
#define servoPin PB0
#define LED PB1
#define piezoSensor PB2
#define LED_ON_DURATION 200
volatile uint32_t switchLedOffNow = 0;
// Interrupt Service Routine for INT0
ISR(PCINT0_vect) {
GIMSK &= ~0b00100000; // Turn off pin change interrupts
// sleep_disable();
digitalWrite(LED, HIGH);
switchLedOffNow = millis() + LED_ON_DURATION;
}
void setup() {
Debug.begin();
Debug.println(F("Hello Tiny!"));
pinMode(servoPin, OUTPUT);
digitalWrite(LED, LOW);
pinMode(LED, OUTPUT);
// Turn on pin change interrupts
GIMSK |= 1 << PCIE;
PCMSK |= 1 << piezoSensor ;
// Enable global interrupts
sei();
}
uint32_t i = 0;
void loop() {
static uint32_t nextSecond = millis() + 1000;
i++;
if (millis() > nextSecond) {
Debug.print(i);
Debug.println(F(" loops/s"));
nextSecond += 1000;
i = 0;
}
if (digitalRead(LED) && millis() > switchLedOffNow) {
digitalWrite(LED, LOW);
GIMSK |= 1 << PCIE; // Turn pin change interrupts back on
}
}