Hi everyone, I need your help, I made a very simple circuit with attiny85, a button and LED, I wanted the circuit to go to sleep when I turn it off and wake up when I press the button again to turn on the LED, I made this code but it doesn't work I can't understand what I'm wrong, you correct me where it is wrong please
#include <avr/sleep.h> #include <avr/interrupt.h> const int Pulsante = 1; // 6 sarà il pin dove andrà collegato il pulsante const int ledPin = 0; // 5 sarà il pin dove andrà collegato l'anodo del led // Variabili int ledState = LOW; // Variabile su cui andrà memorizzato lo stato del led (HIGH o LOW) int buttonState; // Variabile su cui andrà memorizzato il valore del pulsante int lastButtonState = HIGH; // Variabile su cui andrà memorizzato l'ultimo valore del pulsante long lastDebounceTime = 0; // Variavile su cui andrà memorizzato il tempo dell'ultima volta che il pulsante è stato premuto long debounceDelay = 50; // Tempo di antirimbalzo (Aumentare se si vede uno sfarfallio nel led) void setup() { pinMode(Pulsante, INPUT); // Impostiamo il pin 6 come pin di INPUT (PULSANTE) pinMode(ledPin, OUTPUT); // Impostiamo il pin 5 come pin di OUTPUT (LED) digitalWrite(ledPin, ledState); // Impostiamo lo stato iniziale del led, in questo caso è LOW. } void loop() { int reading = digitalRead(Pulsante); // Con questa istruzione leggiamo il valore del pulsante // e lo salviamo nella variabile reading if (reading != lastButtonState) { // Se reading è diverso di lastButtonState lastDebounceTime = millis(); // lastDebounceTime sarà uguale a mills() } if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; if (buttonState == LOW) { ledState = !ledState; } } } digitalWrite(ledPin, ledState); lastButtonState = reading; if (buttonState == HIGH) { enterSleep(); } } void enterSleep() { GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts PCMSK |= _BV(PCINT1); // Use PB6 as interrupt pin ADCSRA &= ~_BV(ADEN); // ADC off set_sleep_mode(SLEEP_MODE_PWR_DOWN); // replaces above statement sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT) sei(); // Enable interrupts sleep_cpu(); // sleep sleep_disable(); } ISR(PCINT_vect) { } ISR(BADISR_vect) { cli(); // Disable interrupts GIMSK &= ~(1<<PCIE); // Disable Pin Change Interrupts PCMSK &= ~_BV(PCINT1); // Turn off PB6 as interrupt pin sleep_disable(); ADCSRA |= _BV(ADEN); // ADC on sei(); // Enable interrupt }