1

For a project of mine I have always wanted to build an electronic load and I am mostly done. However I have found myself stuck at one part that I thought would be simple but has thus only caused headache.

The problem is that I have a keypad connecting along with a TFT display to an Arduino. I want to enter for example my current for the electronic load and display the value that I set on my display. I have tried the simple code for keypads that is available on tutorials all over the web but what happens is that it displays the key I press but if I press another one it erases the first and displays the second in the same spot. I want to be able to have 4 or 5 digits that can all show up in order. I have found some different code and modified it. I can now display multiple digits but the issue is that if I press for example 4 a 4 will show up but if I press a 5 another 4 will show up and then if I press a 5 again then a 5 will show up on and on.

Additionally it resets the screen even though I don't have a reset function in my code. So every time I press a key the screen will reset to white and then show the numbers. If anyone has time to help me out it would be greatly appreciated.

EDIT: Thank you for everyone's help and I have solved the problem. However I haven't found a way to incorporate a decimal point into it. This code is the code that works except for the decimal point.

  #include <Keypad.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

String v1;
String num;

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

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

byte rowPins[ROWS] = {31, 22, 23, 24};
byte colPins[COLS] = {25, 26, 27, 28};

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

#define BLUE    0x001F
#define WHITE   0xFFFF

#define DATAOUT 11//MOSI
#define DATAIN 12//MISO 
#define SPICLOCK  13//sck
#define SLAVESELECT0 10//ss

void setup()
{
  uint16_t id = tft.readID();
  tft.begin(id);
  tft.setRotation(1);
  tft.fillScreen(WHITE);
  tft.setTextColor(BLUE, WHITE);
}


void loop() {
  v1 = GetNumber();
  v1.toInt();
}
String GetNumber()
{
  char key = kpd.getKey();
  // while (key != '#')
  {
    switch (key)

    {
      case NO_KEY:
        break;

      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9':
        num = num + key;
        tft.setTextSize(5);
        tft.setCursor(10, 50);
        tft.print(num);
        break;
    }
    key = kpd.getKey();
  }
  return num;

}
6
  • Print your number tft.print(num); AFTER you append the key num=num + key, not before.
    – hcheung
    Commented May 22, 2020 at 0:36
  • Your String num; is a local variable within the function, it get out of the scope when exit the function, you want to keep the value, declare it as a global variable.
    – hcheung
    Commented May 22, 2020 at 0:38
  • do not combine keypress handling with display handling ... pressing a key should only update the value of a variable, nothing else ... the display function should only get its data from the variable
    – jsotola
    Commented May 22, 2020 at 1:12
  • i think that you are not paying attention to what is in your code ... you complain about the screen turning white, and yet you have code like tft.fillScreen(WHITE); ... don't you think that you should be running the tft setup code like textColor and textSize only once?
    – jsotola
    Commented May 22, 2020 at 1:20
  • Thank you for all who helped me. It is now working perfect. I only have one question: How would I implement a decimal point? I can't simply make another case for it because that would have to be a number. This is my new code
    – athoe
    Commented May 22, 2020 at 3:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.