4x4 keyboard with 7-segment display

Hi Class,

After trying a lot I can't do the basics. I want to type 123456 on the keyboard and see the digits 'move' to the left showing 123456 as I type. That simple.

//https://jasonacox.github.io/TM1637TinyDisplay/

#include <TM1637TinyDisplay6.h>
#include <Keypad.h>

// TM1637 - 6 pinos
const int CLK = 3;
const int DIO = 2;

TM1637TinyDisplay6 display(CLK, DIO);

#define ROW_NUM     4 //4 linhas
#define COLUMN_NUM  4 //4 colunas

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

byte pin_rows[ROW_NUM] = {8, 9, 10, 11};
byte pin_column[COLUMN_NUM] = {12, 13, 14, 15};

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
char key = keypad.getKey();

void setup() {

  Serial.begin(115200);
  delay(10);
  Serial.println();

  display.begin();   
}
//------------------------------------------------------------------------------------------
void loop() {

  char key = keypad.getKey();

  if (key)  {
    uint8_t x = key;
    display.showNumberDec(x); 
    Serial.println(x);
  }

  if (key)  {
    Serial.println(key);
  }
}

I've already tried String(key) but the display library doesn't seem to accept it.

Type 1, show 1, type 2, show 12, type 3, show 123 and so on, until 123456.

Can anyone 'adjust' the code to make this happen ?

Grateful

Do You start from a blank display?
First press shown at the very left?
What to do after 6 digits pressed and displayed?

You have to build up the number yourself. Every time you get a new key, you have to multiply the current display number by 10 and then add the key.

Also, why are you using .showNumberDec() which takes a floating point number? You are using integers. Just use .showNumber()

Give it a try and post your best effort and people can help you.

Here's a hint - key is not a number, but a char, such as '1' which you will need to turn into a number. The easiest way it to subtract '0' from key.

byte x = key - '0';

You will also have to deal with all the non-numeric keys that could be pressed.

Which Arduino (UNO/NANO/MEGA) you are using?

If I press the 1 key I see 1 on the serial monitor, and the same happens with all the others.

But on the display I see 55. The 2 key shows 56, the * key shows 48. It doesn't matter ShowNumber or ShowNumberDec.

I've tried many things with x = key. For x I have already done int, unsigned long, char, etc. and I've already done x = atoi(key), x = key.toInt();

I would like to at least press 1 and see 1 on the display. 2 version 2, 3 version 3, and so on. Do 123456 later I will know (num110+num2100...etc).

Ops, UNO. Please see post#5. Thanks.

CORRECTING:

1 = 49
2 = 50
3 = 51
A = 65
4 = 52
5 = 53
6 = 54
B = 66
7 = 55
8 = 56
9 = 57
C = 67
'*' = 42
0 = 48
'#' = 35
D = 68

Why that ? How do I see the correct numbers for each key on the display ?

The serial monitor can interpret this well:
char key = keypad.getKey();

But the display does not

How many digits are there on your display module -- 4 or 6? Mine one has 4 digits. Can you post a picture of your display?

HOURS, many hours after so much research, the display shows the value of the key:

if (key)  {    
    int x = key - '0'; 
    display.showNumberDec(x);
    Serial.println(x);
  }

I will not use the * and # keys, nor the letters. Only the numeric ones.

SEE ALSO POST#15

you could have a look

for some ideas

The variable key holds an 8-bit data (the character code: 0x30 - 0x39) for a pressed down numeric key. You have subtracted 0x30 (0011000) from it and the result remains as an 8-bit data. Why have you declared an int type variable x insted of a byte type x?

I'm trying to total the 5 digits. Maximum value 99999. But it does not show the real result on the serial monitor. I've already used several types of variables for totalnum (unsigned lon, long, unsigned int, uint16_t....etc).

if(key == '#')  {
    long totalnum = num1*10000+num2*1000+num3*100+num4*10+num5;
    Serial.println(totalnum);
  }

Does anyone have a 'try this' ? (num1 to num5 are just int). Arduino UNO.

Looks plausible with no context.

Please post a complete sketch that does compile and produce an incorrect result.

a7

It worked by putting UL in num1*10000UL

if(key == '#')  {
    long totalnum = num1*10000UL+num2*1000+num3*100+num4*10+num5;
    Serial.println(totalnum);
  }

Do you see why?

Are you not using the TM1637 based 7-segment display unit anymore?
image

I have tested your simulation in real harddware, and it is working like charm. It is a program that deserves studying.
image

Your Sketch (a little change in hardware definitions to suit my setup and in formatting to suit for my understanding):

// Includes
#include <Keypad.h>
#include "TM1637TinyDisplay.h"

const uint8_t CLK = 3;//A0;
const uint8_t DIO = 4;//A1;
TM1637TinyDisplay display(CLK, DIO);

const uint8_t NB7SEG = 4;
char message[NB7SEG + 1];
uint8_t messagePos = 0;

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

uint8_t colPins[COLS] = {12, 13, 14, 15};//{ 4, 3, 2 }; // Pins connected to C1, C2, C3
uint8_t rowPins[ROWS] = {8, 9, 10, 11};//{ 8, 7, 6, 5 }; // Pins connected to R1, R2, R3, R4

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

void setup() 
{
  for (uint8_t i = 0; i < NB7SEG; i++) 
  {
      message[i] = ' ';
  }
  message[NB7SEG] = '\0';
  Serial.begin(115200);
  display.begin();
}

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

  if (key != NO_KEY) 
  {
    for (uint8_t i = 0; i < NB7SEG-1; i++) 
    {
        message[i] = message[i + 1];
    }
    message[NB7SEG - 1] = key;
    display.showString(message);
    Serial.println(message);
  }
}

Working Principle of the Sketch:
TM1367-2

1. Initially all locations occupy ASCII codes for blanks except message[4] location which permanently holds null-character.

2. The contents of the message[] array is transferred to the display unit by excuting this function: display.showString(message);.

3. Contents of the locations of the message[] array are shifted to the left by one location except location message[4].

4. ASCII code of a pressed down key of the Keypad is always placed at location message[3].

5. Goto Step-2.

Question:
The method showString() (as shown below) has four arguments. What are the meanings/values of the last three arguments that are missing in your sketch. I have gone a little bit over the Library but could not get the information.

void TM1637TinyDisplay::showString(const char s[], uint8_t length, uint8_t pos, uint8_t dots)

1 Like

What about the documentation in the .h file ?

1 Like

You have referred me to the right document -- the *.h file. Before, I looked at the *.cpp file. Thank you for sharing the experience.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.