4x4 keypad OLED project

Hi folks! :sunglasses:

At first I wish You all a happy new year!

I have a project with 4x4 keypad and OLED. I'm tried to create password project. Everything works fine, but I have one issue. I have a question how to move pressed numbers on OLED I2C display in to my code?

#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <Keypad.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
#define SCREEN_ADDRESS 0x3C

#define passwordLengt 5

char data[passwordLengt];
char master[passwordLengt] = {"1234"};

byte dataCount = 0;

char customKey;

const byte ROWS = 4;
const byte COLS = 4;

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

byte rowPins[ROWS] = {6, 7, 8, 9};
byte colPins[COLS] = {2, 3, 4, 5};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup()
{

  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  //  display.display();
  //  delay(2000); // Pause for 2 seconds
  display.clearDisplay();

  Serial.begin(9600);
}

//void loop()
//{
//  char customKey = customKeypad.getKey();
//
//  if (customKey) {
//    Serial.println(customKey);
//    display.clearDisplay();
//    display.setTextSize(2);
//    display.setTextColor(WHITE);
//    display.setCursor(0, 0);
//    display.println(customKey);
//    display.display();
//
//  }
//}

void loop()
{
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Password:");
  display.display();

  customKey = customKeypad.getKey();

  if (customKey) {
    Serial.println(customKey);
    data[dataCount] = customKey;
    display.setCursor(0, 30);
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.println(data[dataCount]);
    display.display();
    dataCount++;
  }

  if (dataCount == passwordLengt - 1) {

    display.clearDisplay();

    if (!strcmp(data, master)) {

      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.println("Correct!");
      display.display();
      delay(3000);

    }
    else
    {
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.println("Incorrect!");
      display.display();
      delay(3000);
    }

    display.clearDisplay();
    clearData();
  }
}

void clearData() {

  while (dataCount != 0) {

    data[dataCount--] = 0;

  }
  return;
}

I guess if you have pressed the numbers too hard on the oled-display the display will be damaged. And neither will the numbers appear in your code.

Except maybe if the display is glued to your finger typing the keyboard.
But maybe you mean something different. If yes describe it much more precise

best regards Stefan

Does this print out the key that is pressed?

Serial.println(customKey);

Then you could use that:

display.println(customKey);

I did it as I expected. I tried to do it with for, but without success. I did it with if else.

#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <Keypad.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
#define SCREEN_ADDRESS 0x3C

#define passwordLengt 5
#define startPos 0

char data[passwordLengt];
char master[passwordLengt] = {"1234"};

byte dataCount = 0;

char customKey;

const byte ROWS = 4;
const byte COLS = 4;

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

byte rowPins[ROWS] = {6, 7, 8, 9};
byte colPins[COLS] = {2, 3, 4, 5};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup()
{

  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  //  display.display();
  //  delay(2000); // Pause for 2 seconds
  display.clearDisplay();

  Serial.begin(9600);
}

void loop()
{
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Password:");
  display.display();

  customKey = customKeypad.getKey();

  if (customKey) {
    data[dataCount] = customKey;
    //    display.setTextSize(2);
    //    display.setTextColor(WHITE);
    //    display.println(data);
    //    display.display();

    if (dataCount == 0) {
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.println("*");
      display.display();
    } else if (dataCount == 1) {
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.println("**");
      display.display();
    } else if (dataCount == 2) {
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.println("***");
      display.display();
    } else if (dataCount == 3) {
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.println("****");
      display.display();
    }
    delay(500);
    dataCount++;
  }

  if (dataCount == passwordLengt - 1) {

    display.clearDisplay();

    if (!strcmp(data, master)) {

      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.println("Correct!");
      display.display();
      delay(3000);

    }
    else
    {
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.println("Incorrect!");
      display.display();
      delay(3000);
    }

    display.clearDisplay();
    clearData();
  }
}

void clearData() {

  while (dataCount != 0) {

    data[dataCount--] = 0;

  }
  return;
}