Need help with a Boolean operation and simplification on LED Keypad

Hi All, i am contructing a CAN based keypad for a motorsport vehicle,

Currently i am merging some code to read a keypad and switch on a corresponding LED above it

I am having trouble with the Boolean “i” ststement as it requires 2 pushes of a keypad to extinguish or illuminate an led, if the same operation has been perfomed on a diffrerent key

I understand why it is doing it, as it needs to switch from i = 1 to i = 0 before it can switch an LED on again, i just cant seem to get my head round doing it, also i would like to simplify it a little but not sure how.

Hope you are able to help

Many thanks


#include <Keypad.h> // Include Keypad library
#include <Ucs1903.h> // Include LED Controller Library

#define NUM_LEDS 4  // Defines Number of LED's
#define PIN 13      // Defines Data Pin for LED's

Ucs1903 ledStrip(PIN, NUM_LEDS);

boolean i = 0; //Defines Switch State

int j = 0;   // Variable to Define which LED to Switch

const byte ROWS = 5; //defines number of rows
const byte COLS = 3; //defines number of columns

char keys[ROWS][COLS] = {  //define the symbols on the buttons of the keypads as an array

byte rowPins[ROWS] = {9, 8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {12, 11, 10,}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){

keypad.addEventListener(keypadEvent); //Listen for a keypad Press
void loop(){
  char key = keypad.getKey();
  if (key) {
     if (i==1){
  ledStrip.setLed(j, 0, 0, 75);  // Turn on LED if Putton has been Pressed
    ledStrip.setLed(j, 0, 0, 0);   // Else Turn LED Off
;   // Send Data to LED Controller

void keypadEvent(KeypadEvent key){         //What to do when a particular Key is pressed
  switch (keypad.getState()){
    case PRESSED:
      switch (key){
        case '1':  i = !i, j= 0; break;
        case '2':  i = !i, j= 1; break;
        case '3':  i = !i, j= 2; break;
        case '4':  i = !i, j= 3; break;
        case '5':  break;
        case '6':  break;
        case '7':  break;
        case '8':  break;
        case '9':  break;

your indenting makes the program very hard to read. Then you miss code flaws like

if (key) { }

if a key is pressed do nothing otherwise nothing. Probably not what you meant

Reformat your code so it better reflect the structure you want. TIP use CTRL-T to auto format your code in the IDE.