Notifications
Clear all

Multiple interrupt question

8 Posts
3 Users
1 Likes
1,246 Views
(@garnold)
Member
Joined: 5 years ago
Posts: 81
Topic starter  

Have you ever seen one of those LED light strip controls? It has a bunch of buttons that can send an IR signal to a receiver. I'm trying to figure out how I can get that many interrupts to work. I had a guess and wanted to ask your thoughts. Let's say I have two buttons that trigger the same interrupt. When either button fires it triggers the same interrupt but it also is connected to a resistor and passes a specific voltage into a GPIO. So once the Arduino wakes up it measures that voltage and knows what button is pressed. Think this would work? I can't believe that having multiple interrupt buttons is something not worked out already so I'm just seeing if this is the way to do it or am I'm completely wrong and should be following a different path.

 

Thank you!


   
Quote
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1075
 

That might work, as long as the signal is still present on the I/O when the Arduino wakes up.

Another suggestion - use Pin Change Interrupts instead.

Every I/O pin on the Arduino is assigned to one of three groups of interrupts, which trigger on any change in signal. You then write an ISR (interrupt service routine) to determine which of the pins caused the interrupt.

Nick Gammon has a good reference for just about every type of Interrupt the Arduino supports.

Hope that helps.

😎

Bill

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1121
 

@garnold

This code snippet may be of interest.  It fires up an interrupt when a button is pressed that is attached to any of the pins in the buttonPin array and prints out what button was pressed. 

 

 const int commonPin = 2;
const int buttonPins[] = {4,5,6,7,8,9,10,11,12,13};

unsigned long lastFire = 0;

void setup() {
  configureCommon(); // Setup pins for interrupt

  attachInterrupt(digitalPinToInterrupt(commonPin), pressInterrupt, FALLING);

  Serial.begin(9600);
}

void loop() {
  // Empty!
}

void pressInterrupt() { // ISR
  if (millis() - lastFire < 400) { // Debounce
    return;
  }
  lastFire = millis();

  configureDistinct(); // Setup pins for testing individual buttons

  for (int i = 0; i < sizeof(buttonPins) / sizeof(int); i++) 
  { // Test each button for press
    if (!digitalRead(buttonPins[i])) 
    {
      press(buttonPins[i]);
    }
  }

  configureCommon(); // Return to original state
}

void configureCommon() {
  pinMode(commonPin, INPUT_PULLUP);

  for (int i = 0; i < sizeof(buttonPins) / sizeof(int); i++) {
    pinMode(buttonPins[i], OUTPUT);
    digitalWrite(buttonPins[i], LOW);
  }
}

void configureDistinct() {
  pinMode(commonPin, OUTPUT);
  digitalWrite(commonPin, LOW);

  for (int i = 0; i < sizeof(buttonPins) / sizeof(int); i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }                                                                                                  
}

void press(int button) { // Our handler
  Serial.println(button);
}

   
ReplyQuote
(@garnold)
Member
Joined: 5 years ago
Posts: 81
Topic starter  

And this is exactly why I enjoy this forum and the support it offers a new electronics guy like myself! Thank you for the advice and direction. I agree the solution provided is a much better way to go and one I did not know about. I'll try this out 😉 Again, thank you for the kindness and support!


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1121
 

@garnold

Just for completeness I should have made it clear in the example code on buttons above that each button is wired one side to the common pin2 and the other side to one of the pins in the buttonPin array.  

Actually this lack of explanation in my code snipped example jumped into my mind as I woke up this morning and was idly musing about how my current project mutated into having push buttons to having virtual push buttons in a GUI screen.  This was done in python on a Rpi with its small Rpi screen add-on.  The virtual buttons saves wiring up a bunch of physical buttons, you can have as many as you can fit onto the screen, and can make the eventual project 'box' easy to construct. 😀  

And that got me thinking about whether it is feasible to use one of those small touch screens one can get these days and connect it to the likes of an arduino.  Maybe instead of virtual clicky types of buttons different coloured areas of the small screen can be used to record a touch in that area as a button press.   Probably not so good if you want to wake up the microprocessor from a deep sleep though.   Maybe this is something I will have a go at sometime in the future.  Well thats enough musing for today I'd better get on with real life 😎 


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1121
 

Ha, I see that dronbotworkshp covered this very topic last year showing some virtual buttons on a touch lcd screen using an arduino, so no need for me to muse no more. 😀  


   
ReplyQuote
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1075
 

@byron

Yes, I was going to mention that LOL.

😎

Bill

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
ReplyQuote
(@garnold)
Member
Joined: 5 years ago
Posts: 81
Topic starter  

Thanks again my friends!


   
ReplyQuote