Not enough InterruptIn pins

Hi! I have a problem that I’m not quite sure how to act on.

In my use case I want to have 18 buttons. (0-9), next, previous, free-play and so on, for a digital jukebox.

When I “press a button” I want to send a serial command to a Windows computer, in my case its a very small json string.

I have some issues here.
1: If i use DigitalIn and sends a message if a pin is high in a loop I will send “a lots of” messages to the PC. I have also tried to set a variable to prevent running the “send-method” as long as the pin is high, but I cant get it to work in a good way

2: If I use InterruptIn I can use only pins from A, and D bank and then I can use only 11 or 12 pins since I ues a KL25Z. And that is not pins enough since I need 18 pins.

So, what other options do I have? I want to sense when a pin changes state and then send a printf message on the serial bus.

Hello,

For that you probably need also a debouncing solution (hardware/software) for every button.

You can use a GPIO expander or make your own matrix keyboard (something like this -check description not that piece of hardware) that saves a lot of pins.

BR, Jan

Hello Magnus,

You can try to use a similar approach as in this MIDI keyboard project. However, you should replace the MIDI messages with json strings and send them to the PC using the USBSerial class.

Or maybe you can use the USBKeyboard.

Zoltan, I have tried using the usb keyboard or usbserial class but I only get the error message that “This board does not have a hardware USB driver” therefore I use the SDA port even though I wanted to use the “real” usb port so I can use the SDA port for debugging purpouse.

Actually, you don’t have to use a USB connection. It was just a tip to make things simpler. Keep only the “key scan” part from the MIDI keyboard. It handles 32 keys.

Hello Magnus,
Please try my Keypad library.

You can use 4x5 type (use 9 GPIO’s).

If you have a mode switch which continues ON/ OFF condition, please use diodes with each button and switches as described in the notebook here (sorry Japanese but Google can help).
https://os.mbed.com/users/kenjiArai/notebook/keypadkey-matrix--control/

1 Like

After checking all your fine solutions I went for a more brute force version doing this for each button:

if (NUM_0.read() == 0 && NUM_0_HIGH == 0) {

  NUM_0_HIGH = 1;

  NUM_0_TICK = 0;

  AddNumberToSongNumber(0);

}

if (NUM_0.read() == 1 && NUM_0_HIGH == 1) {

  if (NUM_0_TICK == TICK_COUNT) {

    NUM_0_HIGH = 0;

  }

  NUM_0_TICK++;

}