Hi everyone,
I'm working on a project with an ATtiny85 where I want to use the OneButton library to handle button presses and implement a sleep mode for power saving. The basic functionality I want is:
- A single click turns the LED on.
- A double click turns the LED off and puts the ATtiny85 to sleep after 3 seconds.
- While in sleep mode, a single click should wake the ATtiny85 and turn the LED on.
Here's the code I have so far:
#include <Arduino.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <OneButton.h>
#define LED_PIN PB2 // PB2 (Pin 7)
#define INTERRUPT_BUTTON PB1 // PB1 (Pin 6)
OneButton button(INTERRUPT_BUTTON, true, true);
volatile bool ledState = LOW;
void setup() {
// Set up the LED pin
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Set up the button with the OneButton library
button.attachClick(singleClick);
button.attachDoubleClick(doubleClick);
// Enable pin change interrupt for INTERRUPT_BUTTON (PB1)
GIMSK |= (1 << PCIE); // Enable Pin Change Interrupts
PCMSK |= (1 << PCINT1); // Enable Pin Change Interrupt for PB1
// Enable global interrupts
sei();
// Enable sleep mode
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
}
void loop() {
button.tick(); // Check the status of the button
if (ledState == LOW) {
delay(3000); // Wait for 3 seconds
if (ledState == LOW) {
enterSleep();
}
}
}
ISR(PCINT0_vect) {
// Wake up from sleep
// Do nothing here, just wake up
}
void singleClick() {
// Handle single click - turn LED on and stay awake
ledState = HIGH;
digitalWrite(LED_PIN, HIGH);
}
void doubleClick() {
// Handle double click - turn LED off and go to sleep
ledState = LOW;
digitalWrite(LED_PIN, LOW);
}
void enterSleep() {
// Disable ADC and other modules to save power
ADCSRA &= ~(1 << ADEN);
// Sleep until an interrupt occurs
sleep_enable();
sei();
sleep_cpu();
sleep_disable();
// Re-enable ADC
ADCSRA |= (1 << ADEN);
}
The Issue: The button functions (single click to turn on the LED, double click to turn off the LED) work perfectly without the sleep mode. However, when I include the sleep mode, the button press does not always wake up the ATtiny85 as expected.
Question: How can I correctly implement the OneButton library with the sleep mode on the ATtiny85 to achieve the desired functionality? Any help or pointers would be greatly appreciated!
Thank you!