0

I'm learning how to write code interrupt code. I just experimented with my first toy example that I got from here:

const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

void loop() {
  digitalWrite(ledPin, state);
}

void blink() {
  state = !state;
}

Now I'm trying to incorporate interrupts into my own project. I have a circuit that looks like this using an Arduino Uno Wifi Rev2, Membrane 3x4 Matrix Keypad, a buzzer, some resistors and an RGB LED:

Can I write code such that it interrupts any time any of the keys is pressed (and I can still tell which key is pressed)? How would I do that?

  • 1
    you could use diodes to connect all the columns to an interrupt pin ... keep all the rows low .... when any button is pressed, an interrupt is generated ... the interrupt would activate a keyboard scan to determine which key caused the interrupt – jsotola May 4 at 16:21
  • Uno WiFi Rev 2 supports external interrupt on every pin, but that is not useful here – Juraj May 4 at 16:24
  • Interrupt are great for counting things, but taking human input via interrupt can lead to some really confusing code. You have to start protecting against things getting corrupted because the input comes in the middle of some other part that’s acting on input. The classic example is a button press. This line if(buttonState != oldButtonState && buttonState == LOW) is just fine in most code. But if you use an interrupt to read the button and set buttonState then that line is a huge bug. – Delta_G May 4 at 16:32
  • see the schematic here ...arduino.stackexchange.com/questions/34054/… .... since you are using Uno WiFi, you do not need a separate interrupt pin .... just bring all rows low ... pressing any key will interrupt on column pins – jsotola May 4 at 18:27
  • Interrupts are for things that happen too fast to sample with normal code. That doesn’t include a human pressing a button. Interrupts are NOT a cure for the fact that you’ve coded some other part of the code with blocking functions like delay. It seems at first like a good idea, but you’ll find out quickly that it would have been much easier to just write non blocking code for things. – Delta_G Oct 1 at 19:38
0

No, you can't. Owing to the fact that you have to manually cycle through each row (or column, depending on which way you are holding your head at the time) in order to read which keys are pressed there is no way to relate a button to an interrupt.

The only possible way would be to use an external keypad interface chip (you could roll your own with another Arduino) which has an interrupt signalling facility in it.

| improve this answer | |
  • Why can't I do what Jsotola mentioned in the comment above? – Saqib Ali May 4 at 16:42
  • @SaqibAli, even though it is doable, it is more trouble than it is worth ... but it may be a good learning experience – jsotola May 4 at 17:40
  • Yes, that might work, but as jsotola says, it's not worth it. Unless you have a specific reason to require an interrupt just use polling. – Majenko May 4 at 17:41
  • "Can't" is too strong. You can if you use diodes, as jsoltola says. "You can, if you wire it correctly using diodes, but coding interrupts is tricky and probably not worth the trouble" would be more accurate. – Duncan C Jun 4 at 1:04
  • 1
    You “can” buy dozens of high powered lasers to heat up a pan to make cookies. It would be cool but terribly expensive and hard to do. But you “can” in the same sense. Actually I think laser cookies is probably a better idea. – Delta_G Oct 1 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.