#include <Keypad.h>

const uint8_t LEDS = 12;
const uint8_t ROWS = 4;
const uint8_t COLS = 4;

char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};

// Pins connected to LED1, LED2, LED3, ...LED12
uint8_t ledPins[LEDS] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 28, 27 };
uint8_t rowPins[ROWS] = { 26, 22, 21, 20 }; // Pins connected to R1, R2, R3, R4
uint8_t colPins[COLS] = { 19, 18, 17, 16 }; // Pins connected to C1, C2, C3, C4

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

bool state[LEDS] = { false };

void toggleWrite(uint8_t pos)
{
  bool value = state[pos - 1] = !state[pos - 1];
  digitalWrite(ledPins[pos - 1], value ? HIGH : LOW);
}

void rangeWrite(uint8_t i, uint8_t e, bool high)
{
  for (uint8_t l = i; l <= e; l++) {
    state[l - 1] = high;
    digitalWrite(ledPins[l - 1], high ? HIGH : LOW);
  }
}

void setup() {
  for (uint8_t l = 0; l < LEDS; l++) {
    pinMode(ledPins[l], OUTPUT);
    digitalWrite(ledPins[l], LOW);
  }
}

void loop() {
  char key = keypad.getKey();

  if (key != NO_KEY) {
    switch (key) {
      case 'A': toggleWrite(9);
        break;
      case 'B': toggleWrite(10);
        break;
      case 'C': toggleWrite(11);
        break;
      case 'D': toggleWrite(12);
        break;
      case '0': rangeWrite(1, 8, false);
        break;
      case '9': rangeWrite(1, 8, true);
        break;
      case '*': rangeWrite(9, 12, true);
        break;
      case '#': rangeWrite(9, 12, false);
        break;
      default: toggleWrite(atoi(&key));
        break;
    }
  }
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
pico:GP0
pico:GP1
pico:GND.1
pico:GP2
pico:GP3
pico:GP4
pico:GP5
pico:GND.2
pico:GP6
pico:GP7
pico:GP8
pico:GP9
pico:GND.3
pico:GP10
pico:GP11
pico:GP12
pico:GP13
pico:GND.4
pico:GP14
pico:GP15
pico:GP16
pico:GP17
pico:GND.5
pico:GP18
pico:GP19
pico:GP20
pico:GP21
pico:GND.6
pico:GP22
pico:RUN
pico:GP26
pico:GP27
pico:GND.7
pico:GP28
pico:ADC_VREF
pico:3V3
pico:3V3_EN
pico:GND.8
pico:VSYS
pico:VBUS
rp1:1
rp1:2
rp2:1
rp2:2
rp3:1
rp3:2
rp4:1
rp4:2
keypad1:R1
keypad1:R2
keypad1:R3
keypad1:R4
keypad1:C1
keypad1:C2
keypad1:C3
keypad1:C4
led1:A
led1:C
r1:1
r1:2
led2:A
led2:C
r2:1
r2:2
led3:A
led3:C
r3:1
r3:2
led4:A
led4:C
r4:1
r4:2
led5:A
led5:C
r5:1
r5:2
led6:A
led6:C
r6:1
r6:2
led7:A
led7:C
r7:1
r7:2
led8:A
led8:C
r8:1
r8:2
led9:A
led9:C
r9:1
r9:2
led10:A
led10:C
r10:1
r10:2
led11:A
led11:C
r11:1
r11:2
led12:A
led12:C
r12:1
r12:2