-->
BLANTERWISDOM101

How to use keypad matrix 3x4 and oled display 1.3in with ESP32 in arduino IDE

Tuesday, July 3, 2018



Part:
- ESp32 
- OLED 1.3in 128x64 
- U8g2 lib 
- keypad 4x3 ex. PLN

IDE:
- Arduino

OS:
- Linux Peppermint

Description:
- In this case wi sill use keypad matrix as input to ESP32, and (next) show it in oled display



/*  HelloWorld.ino - ESp32 - OLED 1.3in 128x64 - U8g2 lib - keypad 4x3 ex. PLNwww.yogyatechno.com | taufkmas@gmail.com*/
#include <Keypad.h>
#include <SPI.h>
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
//#include "pictures.h"         // Picture resources in the sketch directory
char oledTxt[25]      = "";
uint8_t draw_state = 0;

int LED_BUILTIN = 2;
int kCol1 = 12;
int kCol2 = 14;
int kCol3 = 27;
int kCol4 = 26;

int kRow5 = 25;
int kRow6 = 33;
int kRow7 = 32;

//for keypad PLN 4x3
const byte numRows = 3; //number of rows on the keypad
const byte numCols = 4; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char customKey;
char keymap[numRows][numCols] =
{
  {'3', '6', '9', '='},
  {'2', '5', '8', '0'},
  {'1', '4', '7', 'C'}
};
byte rowPins[numRows] = {kRow5, kRow6, kRow7}; // Rows 0 to 2 //https://prnt.sc/how8tm
byte colPins[numCols] = {kCol4, kCol3, kCol2, kCol1}; //Columns 0 to 3

Keypad customKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

int menu = 0;
int submenu = 0;

long totalValue;
long remainValue;
long densityValue;
long volumeValue;

long totalValueTemp;
long remainValueTemp;
long densityValueTemp;
long volumeValueTemp;

String txtTotCapVal, txtRemVal, txtDenVal, txtVolVal;
int ttkAwal = 0;
char capBuf[8];
char remBuf[8];
char denBuf[8];
char volBuf[8];
float fVol ; char curfVol[10];

long first = 0;
long second = 0;
long third = 0;

long remainSatu = 0;
long remainDua = 0;
long remainTiga = 0;

double total = 0;
String deretKedua;
int cekricek = 0;

int cancel = 0;
int done = 0;

unsigned long previousMillis = 0;

U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); //Display OLED I2C 1.3inc OKE


void setup(void) {
  Serial.begin(9600);
  u8g2.begin();
  u8g2.clearBuffer();          // clear the internal memory
  OLED_Screen1() ;
  delay(2000);
  Serial.println("zona setup.");
}

void loop(void) {
  //  Serial.println(menu);
  //   Serial.println(submenu);
  customKey = customKeypad.getKey();
  switch (customKey)
  {
    case '0':
      Serial.println("0");

      break;
    case '1':
      Serial.println("1");

      break;

    case '2':
      Serial.println("2");
      if (menu != 3) {
        menu = menu - 1;
        if (menu < 0) {
          menu = 2;
        }
        if (menu > 2) {
          menu = 0;
        }
      }



      break;

    case '3':
      Serial.println("3");
      break;

    case '4':
      Serial.println("4");
      break;

    case '5':
      Serial.println("5");
      break;

    case '6':
      Serial.println("6");
      break;

    case '7':
      Serial.println("7");

      break;

    case '8':
      Serial.println("8");


      if (menu != 3) {
        menu = menu + 1;
        if (menu < 0) {
          menu = 2;
        }
        if (menu > 2) {
          menu = 0;
        }


      }

      break;

    case '9':
      Serial.println("9");

      break;

    case 'C':
      Serial.println("C");

      break;

    case '=':
      Serial.println("=");

      if (menu == 0) {
        Serial.println("pilih CALCULATION gan..");
        menu = 3;  submenu = 1;
      }
      if (menu == 2) {
        Serial.println("pilih LAST DATA gan..");
        menu = 4;
      }
      break;
  }


  if ((menu == 0) && (submenu == 0)) { //Menu utama,LASTDATA ditampilkan totCap, totRemain, density, dan volume

    u8g2.setFont(u8g2_font_7x14B_tr);       // choose a small font for status dispay
    u8g2.clearBuffer();
    u8g2.drawStr(0, 11, ">>");
    u8g2.drawStr(18, 11, "Calculation");
    u8g2.drawStr(18, 27, "Configuration");
    u8g2.drawStr(18, 27 + 16, "Last Data");
    u8g2.sendBuffer();

  }
  if ((menu == 1)) {
    u8g2.setFont(u8g2_font_7x14B_tr);       // choose a small font for status dispay
    u8g2.clearBuffer();

    u8g2.drawStr(18, 11, "Calculation");
    u8g2.drawStr(0, 27, ">>");
    u8g2.drawStr(18, 27, "Configuration");
    u8g2.drawStr(18, 27 + 16, "Last Data");
    u8g2.sendBuffer();
  }
  if ((menu == 2)) {
    u8g2.setFont(u8g2_font_7x14B_tr);       // choose a small font for status dispay
    u8g2.clearBuffer();

    u8g2.drawStr(18, 11, "Calculation");
    u8g2.drawStr(18, 27, "Configuration");
    u8g2.drawStr(0, 27 + 16, ">>");
    u8g2.drawStr(18, 27 + 16, "Last Data");
    u8g2.sendBuffer();
  }


}



void OLED_Screen1() {                       // Wellcome screen
  u8g2.setFont(u8g2_font_7x14B_tr);         // choose a small font for status dispay
  u8g2.clearBuffer();
  u8g2.drawStr(0, 11, "    Wellcome to");
  u8g2.setFont(u8g2_font_profont22_tf);
  u8g2.drawStr(6, 32, "Calculator");
  u8g2.setFont(u8g2_font_7x14B_tr);
  u8g2.drawStr(0, 48, "   Loading...");
  u8g2.setFont(u8g2_font_6x10_tf);
  u8g2.drawStr(0, 64, "v1.3");  u8g2.sendBuffer();
}
 

Share This :

0 comments