Hi! I'm working on a project and I have some issues with the interrupts. I have 4 buttons connected to A2 on the ATtiny85. When one of them is pressed, it should trigger an interrupt and do stuff specific to the button pressed. The problem is that kinda works with the below code but after 5 minutes it only responds to a single button (button4). The buttons generate different voltages between pin A2 and ground, I've built a voltage divider using resistors.
#include <avr/interrupt.h>
// Pin definitions
const int buttonPin = A2; // Analog pin A2 (physical pin 3)
const int ledPin = 3; // PB3 (physical pin 2)
// Voltage levels corresponding to button presses
const int button1Threshold = 463; // Threshold for Button 1 (1.96V) og 500 1k ohm 1.96
const int button2Threshold = 320; // Threshold for Button 2 (1.30V) og 330` 2k ohm 1.30
const int button3Threshold = 209; // Threshold for Button3 (0.90V) resistor 3K 0.90
const int button4Threshold = 637; // Threshold for Button3 (2.60) resistor 500 ohm 2.60
volatile bool button1Pressed = false;
volatile bool button2Pressed = false;
volatile bool button3Pressed = false;
volatile bool button4Pressed = false;
volatile bool sleepMode = false; // Flag to indicate sleep mode status
void setup() {
// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
// Enable pin change interrupt on PB4
GIMSK |= (1 << PCIE); // Enable Pin Change Interrupts
PCMSK |= (1 << PCINT2);
PCMSK |= (1 << PCINT4);
PCMSK |= (1 << PCINT1);
PCMSK |= (1 << PCINT0);
PCMSK |= (1 << PCINT5);
//PCMSK |= (1 << PCINT3);
sei(); // Enable global interrupts
// Put the ATtiny85 to sleep
MCUCR = (1 << SM1) | (1 << SE); // Set sleep mode to power down and enables the MC to be put in sleep mode
asm("sleep");
}
void loop() {
// If sleep mode is set, put the ATtiny85 to sleep
if (sleepMode) {
// Enable sleep mode
MCUCR |= (1 << SE);
// Enter sleep mode (Ensure we are in sleep mode until an interrupt wakes us up)
asm("sleep");
// Disable sleep mode after waking up
MCUCR &= ~(1 << SE);
// The program continues from here after waking up
// Clear sleep mode flag
sleepMode = false;
}
if (button1Pressed) {
// Debounce the button press by adding a small delay
delay(50); // 50 ms delay
// Check if the button is still pressed after the delay
if (analogRead(buttonPin) >= button1Threshold && analogRead(buttonPin) < button4Threshold) {
// Perform the action for button 1
// Send command
}
// Reset the button pressed flag
button1Pressed = false;
// Set the sleep mode flag
sleepMode = true;
}
if (button2Pressed) {
// Debounce the button press by adding a small delay
delay(50); // 50 ms delay
// Check if the button is still pressed after the delay
if (analogRead(buttonPin) >= button2Threshold && analogRead(buttonPin) < button1Threshold) {
// Perform the action for button 2
// Send command
}
// Reset the button pressed flag
button2Pressed = false;
// Set the sleep mode flag
sleepMode = true;
}
if (button3Pressed) {
// Debounce the button press by adding a small delay
delay(50); // 50 ms delay
// Check if the button is still pressed after the delay
if (analogRead(buttonPin) >= button3Threshold && analogRead(buttonPin) < button2Threshold) {
// Perform the action for button 2
// Send command
}
// Reset the button pressed flag
button3Pressed = false;
// Set the sleep mode flag
sleepMode = true;
}
if (button4Pressed) {
// Debounce the button press by adding a small delay
delay(50); // 50 ms delay
// Check if the button is still pressed after the delay
if (analogRead(buttonPin) >= button4Threshold) {
// Perform the action for button 2
// Send command
}
// Reset the button pressed flag
button4Pressed = false;
// Set the sleep mode flag
sleepMode = true;
}
}
// Interrupt Service Routine (ISR) for pin change interrupt
ISR(PCINT0_vect) {
//ADCSRA |= (1 << ADEN);
// Read the analog value of the button pin
int analogValue = analogRead(buttonPin);
if (analogValue >= button4Threshold) {
// Set the button 4 pressed flag
button4Pressed = true;
}
if (analogValue >= button1Threshold && analogValue < button4Threshold) {
// Set the button 1 pressed flag
button1Pressed = true;
}
if (analogValue >= button2Threshold && analogValue < button1Threshold) {
// Set the button 2 pressed flag
button2Pressed = true;
}
if (analogValue >= button3Threshold && analogValue < button2Threshold) {
// Set the button 3 pressed flag
// Send command
button3Pressed = true;
}
}
I've enabled Pin Change interrupts on so many pins because if I enable only the PCINT4 which is tied to A2 only button4 is working . If I enable only PCINT2 for some reason the buttons only work for a one second after the mc powers up, then only the button4 is working.
Any idea how I can fix this?