[SOLVED] Interfacing keypad and TFT lcd with Arduino Uno

Hi, i am new to arduino and i need some help on how to interface a 4x3 keypad and a TFT lcd with arduino uno. I want to display the values i pressed on the keypad to the lcd. Can someone help me with that?
Here is my code:

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

// pin definition for the Uno
#define cs 10
#define dc 9
#define rst 8

// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);
// char array to print to the screen
char sensorPrintout[4];

const byte ROWS = 2; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'*','0','#'}
};
byte rowPins[ROWS] = {2, 3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 6, 7}; //connect to the column pinouts of the keypad

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

void setup(){
Serial.begin(9600);
// Put this line at the beginning of every sketch that uses the GLCD:
TFTscreen.begin();
// clear the screen with a black background
TFTscreen.background(0, 0, 0);

// write the static text to the screen
// set the font color to white
TFTscreen.stroke(255, 255, 255);
// set the font size
TFTscreen.setTextSize(2);
// write the text to the top left corner of the screen
TFTscreen.text("Sensor Value :\n ", 0, 0);
// ste the font size very large for the loop
TFTscreen.setTextSize(5);
}

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

if (key != NO_KEY){
TFTscreen.text(key);
}

// Read the value of the sensor on A2
String sensorVal = String(analogRead(A2));

// convert the reading to a char array
sensorVal.toCharArray(sensorPrintout, 4);

// set the font color
TFTscreen.stroke(255, 255, 255);
// print the sensor value
TFTscreen.text(sensorPrintout, 0, 20);
// wait for a moment
delay(250);
// erase the text you just wrote
TFTscreen.stroke(0, 0, 0);
TFTscreen.text(sensorPrintout, 0, 20);
}

It would be easier if you used code tags for your code..

I guess you should just print the "key" variable on your TFT- LCD using TFTscreen.text(); ...
"key" keeps the value you press on the keypad..

  char key = keypad.getKey();

Thanks for your reply!
Did you mean something like TFTscreen.text(key,0,0); ?
I've tried that but it doesnt work..
It gives me an error "invalid conversion from 'char' to 'const char' "
Is there any sample code for interfacing both the keypad and the TFT LCD so that i could try?
Thanks in advance!

Matoo:
Thanks for your reply!
Did you mean something like TFTscreen.text(key,0,0); ?
I've tried that but it doesnt work..
It gives me an error "invalid conversion from 'char' to 'const char' "
Is there any sample code for interfacing both the keypad and the TFT LCD so that i could try?
Thanks in advance!

TRy to print the key value on your serial monitor to see if it works

 Serial.println(key);

Do you get any results on serial monitor ???

Where did you put this line in your code ?

TFTscreen.text(key,0,0);

http://playground.arduino.cc/code/Keypad

It works on the serial monitor but not on the lcd..

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

  if (key != NO_KEY){
    Serial.println(key);
  }
  TFTscreen.text(key,0,0);
  
}

Matoo:
It works on the serial monitor but not on the lcd..

I made a change, try that code :

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

  if (key != NO_KEY){
    Serial.println(key);
  }
  TFTscreen.text(key,0,0);
  
}

Tell me what happens 8)

It works!!!!! The numbers i pressed on the keypad appears on the lcd screen!!
Thank you so much!! :slight_smile:

Matoo:
It works!!!!! The numbers i pressed on the keypad appears on the lcd screen!!
Thank you so much!! :)

You re welcome, the reason why it wouldn't work was because of wrong syntax !!! ;)
Take look here :
Syntax
screen.text(text, xPos, yPos);
Parameters

text : char array, the text you want to write on the screen
xPos : int, the location on the x-axis you want to start drawing text to the screen
yPos : int, the location on the y-axis you want to start drawing text to the screen

Text needs to be an array...

Please change the topic as SOLVED :D :D :D

Thank you for your explanation!! :slight_smile:
Sorry if you dont mind i would like to ask another question,
is it possible to erase the number before another number is pressed?
If i continue to press one number after another, the numbers will overlap..
I tried to insert some of the code below the "TFTscreen.text(key,0,0);" but it doesnt seem to be working..

void loop(){
  char key[100] = {keypad.getKey()};

  if (key != NO_KEY){
    TFTscreen.text(key,0,0);
  }
 // wait for a moment
  delay(250);
  // erase the text you just wrote
  TFTscreen.stroke(0, 0, 0);
  TFTscreen.text(key, 0,0 );
}

You can print a tab : Not really sure but you can try !!!

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

  if (key != NO_KEY){
    Serial.println(key);
  }
  TFTscreen.text(key,0,0);
 TFTscreen.text("\t",0,0);
  
}

Nope it didn't work..
It's okay!! I'll figure it out on my own!!
Thank you so much for your help!! Really appreciated!! :slight_smile: :slight_smile:

Thank you zaxarias!! I needed help on this too. :smiley: 8)