0
votes

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);
} 
1
ATmega328 is capable to handle pin change interrupt on any IO pin. See chapter 13. External Interrupts in the datasheetAterLux

1 Answers

1
votes

As stated in the comments you can use pin change interrupts.

See https://thewanderingengineer.com/2014/08/11/arduino-pin-change-interrupts/ or

https://arduino.stackexchange.com/questions/1784/how-many-interrupt-pins-can-an-uno-handle

Alternatively you can connect all buttons to the same interrupt and each one to another input. Then in the ISR check which of the three buttons is pressed.

Or you connect all three buttons with three different resisitors to an analog input. The measured voltage tells you which of the buttons are pressed.

If your code doesn't do anyhting but this, there is actually no need for interrupts so you can just frequently poll the button states in a loop.