Lately, I tried to use my Arduino Uno (AtMega328) board to detect the press of a series of three switches by an interrupt service routine.
If have three switches for called R, G and B. Whenever,at least one of these switches is pressed once a RGB-Led should toggle its state for red, green or blue.
Now, it was no problem for just two switches R and G, as the Arduino Uno board has two interrupt capable pins (2 and 3).
But, for the switch B I'm lacking another interrupt pin to detect the press of at least one of the three switches.
Is there a possible circuit, that easily allows one to detect the press of at least one of the three switches, so that I can just use one interrupt capable pin to detect the press of any switch?
The code for two leds using the Arduino IDE was super easy for just two switches:
const int buttonRed = 2; // the number of the pushbutton pin
const int ledRed = 13; // the number of the LED pin
const int buttonGreen= 3;
const int ledGreen=12;
// variables will change due to ISR
volatile int redState = 0;
volatile int greenState=0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledRed, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonRed, INPUT);
pinMode(ledGreen, OUTPUT);
pinMode(buttonGreen, INPUT);
// Attach an interrupt to the ISR vector
attachInterrupt(digitalPinToInterrupt(buttonRed), redButton_ISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(buttonGreen), greenButton_ISR, CHANGE);
}
void loop() {
// Nothing to do here
}
void greenButton_ISR() {
greenState=digitalRead(buttonGreen);
digitalWrite(ledGreen, greenState);
}
void redButton_ISR() {
redState = digitalRead(buttonRed);
digitalWrite(ledRed, redState);
}