/*attiny sleep interruption
  Exemple avec trois “Pin Change INTerrupt” (PCINT, à distinguer des INT)
 16/11/2023
  liens: https://ressources.labomedia.org/attiny_atmega_verylowpower
         https://wokwi.com/projects/381563155520612353
  

          Brochage ATtiny85

            =|1  U  8|= VCC
            =|2     7|= 2<-- BP--GND
   Led <--4 =|3     6|= 1<-- BP--GND
        GND =|4     5|= 0<-- BP--GND
*/

#include <avr/interrupt.h>
#include <avr/sleep.h>

ISR(PCINT0_vect) {
  if (digitalRead(0) == LOW)            // PB0 = pin 5 enfoncé => on allume la LED
    digitalWrite(4, HIGH);

  else if (digitalRead(1) == LOW)       // PB1 = pin 6 enfoncé => on éteint la LED
    digitalWrite(4, LOW);

  else if (digitalRead(2) == LOW)       // PB2 = pin 7 enfoncé => on allume la LED
    digitalWrite(4, HIGH);
}

void setup() {
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(4, OUTPUT); // LED
  ADCSRA = 0; // ADC disabled
  GIMSK = 0b00100000;  // General Interrupt Mask Register, / Bit 5 – PCIE: Pin Change Interrupt Enable / When the PCIE bit is set (one) and the I-bit in the Status Register (SREG) is set (one), pin change interrupt is enabled. Any change on any enabled PCINT[5:0] pin will cause an interrupt. The corresponding interrupt of Pin Change Interrupt Request is executed from the PCI Interrupt Vector. PCINT[5:0] pins are enabled individually by the PCMSK0 Register. / see https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet.pdf
  PCMSK = 0b00000111;
} // fin setup

void loop() {
  sleep_enable();
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_cpu();
} // fin loop
ATTINY8520PU