/*ATtiny85 sleep V2
  A chaque pression sur le bouton, l'état de la led sur la carte changera.
  Entre un changement d'état et le suivant, le système entrera en mode veille pour réduire le courant consommé.
  liens: https://www.artifex.it/interrupt-e-sleep-mode-per-scheda-digispark-attiny85/
         https://wokwi.com/projects/390984799154736129
  test: ok

    Brochage ATtiny85

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

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

const byte Led = 0;
const byte BP = 2;

volatile byte dernierEtatBP = HIGH;
volatile byte etatLed = HIGH;

// Interrupt Service Request. Executed when the state of the pin changes
ISR(PCINT0_vect)
{
  byte etatBP;
  etatBP = digitalRead(BP); // or faster: (PINB >> PINB2) & 1
  delay(20);
  if (dernierEtatBP == HIGH && etatBP == LOW) // Falling edge
  {
    etatLed = !etatLed;
  }
  dernierEtatBP = etatBP;
}

void setup() {
  pinMode (Led, OUTPUT);
  pinMode (BP, INPUT_PULLUP);

  cli(); // Disable interrupt
  // Pin change interrupt
  PCMSK  |= bit (PCINT2);  // D2 / pin 7
  GIFR   |= bit (PCIF);    // Clear any outstanding interrupts
  GIMSK  |= bit (PCIE);    // Enable pin change interrupts
  sei(); // Enable interrupt
}

void loop() {
  digitalWrite (Led, etatLed);
  enter_sleep ();
}

void enter_sleep () { // Enter sleep
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  ADCSRA = 0; // Turn off ADC
  power_all_disable (); // Power off ADC, Timer 0 and 1, serial interface
  sleep_enable();
  sleep_cpu();

  // …zzz

  // Wake up
  sleep_disable();
  power_all_enable(); // Power everything back on
}
ATTINY8520PU