4 x 4 Keypad to MAX7219 7 segment LED

Hello all - sorry for the possible simplicity of this question but I've trying to get it to work for the past 2 days with little success (but some).

I have a typical 4 x 4 button keypad and a 8 x 7 segment LED (max 7219) display that I'm simply trying to display the button pressed on the display in order. (i.e I press 4 and 4 appears on the first 7 segment then I press 6 and it appears on the 2nd 7 segment). I have managed to make it that far but I can't get a series of numbers to appear at different locations on the 8 x 7 segments.

I can get the same number to appear on all 7 segments at once but not in sequence!

My sketch is attached, I'd appreciate any help you guys and gals can offer.

#include <Keypad.h>
#include "LedControl.h"
LedControl lc=LedControl(1,15,3,1); // Pin GPIO1->DIN, GPIO15->CLK, GPIO3->CS, 1 = No.of devices
const byte ROWS = 4;       //four rows
const byte COLS = 4;       //four columns
char keys[ROWS][COLS] = {
 {'1','2','3','A'},
 {'4','5','6','B'},
 {'7','8','9','C'},
 {'*','0','#','D'}
};
byte rowPins[ROWS] = {D7, D6, D5, D4};    //connect from D0 to D7 (skip gnd and 3v) with D0 the far rigth connector on the keypad and D7 the far left.
byte colPins[COLS] = {D3, D2, D1, D0};     
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
 //    Serial.begin(9600);

//int key = keypad.getKey();

  
  lc.shutdown(0,false);    // Enable display
  lc.setIntensity(0,10);   // Set brightness level (0 is min, 15 is max)
  lc.clearDisplay(0);      // Clear display register


}
void loop()
{
 char key = keypad.getKey();
 
     if (key != NO_KEY)
     {
     lc.setChar(0,6,key,false);  //display keypress on 6th 7 segment
     lc.setChar(0,5,key,false);  //display keypres sn 5th 7 segment     

     }



}

this is displaying the same symbol (whatever is stored in key) on both

     lc.setChar(0,6,key,false);  //display keypress on 6th 7 segment
     lc.setChar(0,5,key,false);  //display keypres sn 5th 7 segment

if you want to see different ones, you need to remember what was pressed

J M L - ahh yes i see thanks... I'm a bit of a novice and still learning so any help as to how i can get it to remember the key presses

Use an array to collect what is pressed

you have to define how you want to operate, if you want to display 1234 do you want to type 1234 or 4321?

When a key is pressed collect it in the array and display the new array

You could use an array, but it might not be needed. It depends what you want to do with the result once all the presses have been entered. If the goal is to enter a number, then you could use a variable to hold the total so far. Each time a digit is entered, you could multiply the total by 10 and add the new digit.

To update the display, you need a variable to record which digit position to update next. This will start at zero and be incremented after each time a digit has been entered and written to the display. But, think what happens when that variable reaches 8?

byte colPins[COLS] = {D3, D2, D1, D0};

What type of Arduino are you using? If Uno or Nano, for example, then using D0 and D1 is not a good idea!

A: Let us carry out the following tutorial to operate the setup of Fig-1 using the SPI.h Library first before we switch over to LedControl.h Library in Section-B.


Figure-1:

1. Connect the display unit as per Fig-1.
2. Upload the following sketch on UNO and check that 1 2 3 have appeared on DP0 DP1 DP2 positions of the display unit of Fig-1.

#include<SPI.h>
#define  sdReg 0x0C   //Shutdown Register = 01; Normal Mode
#define  dmReg 0x09   //Decode Mode Register=00;cc-code on no-decode format
#define  inReg 0x0A   //Intensity Register = 01; nominal intensity
#define  slReg 0x0B   //Scan Limit Register = 07; eight digit to scan
#define LOAD 10
byte registerAddress[] = {sdReg, dmReg, inReg, slReg};//0x0C, 0x09, 0x0A, 0x0B};
byte registerData[] = {0x01, 0x00, 0x01, 0x07}; //data for above registers

byte dataArray[50];  //to hold cc-codes for the digits to be displayed (no-decode format)
byte digitAddress[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};//DP0-DP7
byte lupTable[] = {0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70,
                   0x7F, 0x7B, 0x77, 0x1F, 0x4E, 0x3D, 0x4F, 0x47
                  }; //0, 1, ...., E, F (no-decode format: p, a, b, c, d, e, f, g

void setup()
{
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV128); //TX rate = 16MHz/128 = 125 kbit
  //------------------------------------------------
  digitalWrite(LOAD, LOW);      //Low at LOAD pin

  //----intialize MAX7219---------------------------------------
  for (int i = 0; i < 4; i++)
  {
    SPI.transfer(registerAddress[i]);
    SPI.transfer(registerData[i]);
    digitalWrite(LOAD, LOW);
    digitalWrite(LOAD, HIGH); //assert LH/LL on LOAD p
    digitalWrite(LOAD, LOW);
  }

  //---- example computation----------------------------------
  byte z = 0x10 + 0x02;   //z = 12
  dataArray[0] = lupTable[z >> 4];  // 1 appears on DP0 position
  dataArray[1] = lupTable[z & 0x0F]; // 2 appears on DP1 position
  dataArray[2] = 0x79;   //to show 3 on DP position

  //-- show result of above computaion on DP0, DP1 and DP2 of display uniy-------
  for (int i = 0; i < 8; i++)
  {
    SPI.transfer(digitAddress[i]); //DPX position
    SPI.transfer(dataArray[i]);   //shows 2 on DP0-position
    digitalWrite(LOAD, LOW);
    digitalWrite(LOAD, HIGH);       //assert LH/LL on LOAD pin
    digitalWrite(LOAD, LOW);
  }
}

void loop()
{

}

3. Connect 4x4 Keypad with UNO as per Fig-1.
4. Upload the following sketch.

#include<SPI.h>
#include <Keypad.h>

#define  sdReg 0x0C   //Shutdown Register = 01; Normal Mode
#define  dmReg 0x09   //Decode Mode Register=00;cc-code on no-decode format
#define  inReg 0x0A   //Intensity Register = 01; nominal intensity
#define  slReg 0x0B   //Scan Limit Register = 07; eight digit to scan
#define LOAD 10
byte registerAddress[] = {sdReg, dmReg, inReg, slReg};//0x0C, 0x09, 0x0A, 0x0B};
byte registerData[] = {0x01, 0x00, 0x01, 0x07}; //data for above registers

byte dataArray[8];  //to hold cc-codes for the digits to be displayed (no-decode format)
byte digitAddress[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};//DP0-DP7
byte lupTable[] = {0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70,
                   0x7F, 0x7B, 0x77, 0x1F, 0x4E, 0x3D, 0x4F, 0x47
                  }; //0, 1, ...., E, F (no-decode format: p, a, b, c, d, e, f, g

//------------------------------------------------------------------------
char keyAscii[8];// = "";  //holds ASCII codes of a pressed down key
//byte ccCode[8];         //holds cc = {0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E};  //
int i = 0;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad; R1R2R3R4
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
//-------------------------------------------------------------------------------

void setup()
{
  Serial.begin(9600);
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV128); //TX rate = 16MHz/128 = 125 kbit
  //------------------------------------------------
  digitalWrite(LOAD, LOW);      //Low at LOAD pin

  //----intialize MAX7219---------------------------------------
  for (int i = 0; i < 4; i++)
  {
    SPI.transfer(registerAddress[i]);
    SPI.transfer(registerData[i]);
    digitalWrite(LOAD, LOW);
    digitalWrite(LOAD, HIGH); //assert LH/LL on LOAD p
    digitalWrite(LOAD, LOW);
  }

  //---- blank DP0 DP1 DP2 of display unit ----------------------------------
  dataArray[0] = 0x00;//// blank appears on DP0 position
  dataArray[1] = 0x00;//blankappears on DP1 position
  dataArray[2] = 0x00;   //blank DP2 position
  updateDisplay();
}

void loop()
{
  byte myKey = customKeypad.getKey();
  if (myKey != 0x00)
  {
    Serial.println(myKey, HEX);
    //keyAscii[i] = customKey;
    dataArray[i] = lupTable[(myKey & 0x0F)]; //cc-code for the pressed digit of Keypad is saved
    Serial.println(dataArray[i], HEX);
    updateDisplay();
    i++;
    //-------------------------------
  }
}

void updateDisplay()
{
  for (int i = 0; i < 8; i++)
  {
    SPI.transfer(digitAddress[i]); //DPX position
    SPI.transfer(dataArray[i]);   //
    digitalWrite(LOAD, LOW);
    digitalWrite(LOAD, HIGH);       //assert LH/LL on LOAD pin
    digitalWrite(LOAD, LOW);
  }
}

(1) Check that the display is blank (at least first 3-digit).
(2) Press 1 on Keypad and check that 1 has appeared on DP0 position of display.
(3) Press 4 on Keypad and check that 4 has appeared on DP1 position of display unit.
(4) Press 7 on Keypad and check that 7 has appeared on DP2 position of display unit.

B: Let us carry out the following tutorial to operate the setup of Fig-1 using the LedControl.h Library.
1. Upload the following sketch.

#include <Keypad.h>
#include "LedControl.h"
LedControl lc = LedControl(11,13,10,1); //DIN, SCK, LOAD,1xMAX7219
byte posiTion = 0;  //starts from DP0 position

const byte ROWS = 4;       //four rows
const byte COLS = 4;       //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};//D7, D6, D5, D4};    //connect from D0 to D7 (skip gnd and 3v) with D0 the far rigth connector on the keypad and D7 the far left.
byte colPins[COLS] = {6, 7, 8, 9};//D3, D2, D1, D0};
Keypad mykeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  Serial.begin(9600);
  lc.shutdown(0, false);   // Enable display
  lc.setIntensity(0, 10);  // Set brightness level (0 is min, 15 is max)
  lc.clearDisplay(0);      // Clear display register
}

void loop()
{
  char key = mykeypad.getKey();
  if (key != NO_KEY)
  {
    Serial.println(key);
    lc.setChar(0, posiTion, key, false); //displayBlank = 1, digitPosition, digit to show, no-decimal point
    posiTion++; //next digit will be shown at DP1 and so on
  }
}

2. Check that the display is blank.
3. Press 1 on Keypad and check that 1 has appeared on DP0 position of display.
4. Press 4 on Keypad and check that 4 has appeared on DP1 position of display unit.
5. Press 7 on Keypad and check that 7 has appeared on DP2 position of display unit.