Go Down

Topic: TFT 1.8: + Keypad (Read 219 times) previous topic - next topic

zeus2kx

Hello,
I am using an 1.8" TFT screen with mega 2560 to see digits pressed using a keypad.
Problem is all the keys are displayed over another. After each keypress, the cursor doesn't move.
I actually want to see digits displayed like"1234567890".
Please suggest a solution.

Later on, I want to get a combination of digits and use as a constant for function "delay".
For example, I pressed 100 and then pressed # to set delay to 100ms. And maybe press * to reenter digits.
How can I do that?

Thanks

Code: [Select]

#include <TFT.h>  // Arduino LCD library
#include <SPI.h>
#include <Keypad.h>
int x;
char buffer;
// keypad type definition
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] =
 {{'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}};
 
byte rowPins[ROWS] = {
  5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  8, 7, 6}; // connect to the column pinouts of the keypad
 
int count=0;
 
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

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

// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);

// char array to print to the screen
char key_print[4];

void setup() {
 Serial.begin(9600);
  // Put this line at the beginning of every sketch that uses the GLCD:
  TFTscreen.begin();
  TFTscreen.background(0, 0, 0);
  TFTscreen.stroke(255, 255, 255);
  // set the font size
  TFTscreen.setTextSize(2);

}

void loop(){
 char key[100] = {keypad.getKey()};
 count = 0;
 TFTscreen.text(key,0,0);
 delay(250);
    count = count+10;
    TFTscreen.setCursor(count, 0);
     TFTscreen.text(key,count,0);
 
  if (key != NO_KEY){
    Serial.println(key);

  }
 
}

zeus2kx

Hi,
Someone who can help?

Thanks.

bodmer

#2
Sep 29, 2016, 03:30 pm Last Edit: Sep 29, 2016, 03:32 pm by bodmer
Think about it.... loop() runs the code within it repeatedly,  since you always set count = 0 at the beginning of each loop you will not see the cursor move!

You will need to reset it to 0 inside the loop() only after you have drawn the correct number of digits.

Formerly Rowboteer (now a broken user profile!)

zeus2kx

Thanks.
I don't have the device at the moment but please correct if I am wrong

Code: [Select]
void loop(){
 count = 0;
 char key[100] = {keypad.getKey()};

 TFTscreen.text(key,count,0);

 
  if (key != NO_KEY){
    Serial.println(key);

  }
 return key;
 count = count+10;
}

bodmer

Oh dear, you have not understood that the code keeps looping around in the loop() functions.

When the execution of the code reaches the end of loop() after you have added 10 to count it goes back to the start and immediately sets count to 0 again... think about what the processor is doing.

Try:

Code: [Select]
if (count >100) count = 0;

At the start of loop()

Go and look at some of the examples in the keypad library.
Formerly Rowboteer (now a broken user profile!)

zeus2kx

Hi bodmer,
Sorry I couldn't find any relevant example which could give an idea how to solve the problem correctly.

I tried this:

Code: [Select]
void loop(){

  key_loop();

 
}

int key_loop(){

  if (count >100) count = 0;
 char key[100] = {keypad.getKey()};
 
 TFTscreen.text(key,count,60);
 if (key != NO_KEY){
    Serial.println(key);
  }


 
  int key1 = atoi(key);
 
  count = count+1;
  return(key1);
}


But the problem is numbers are being displayed at random columns of the row 60.
For example when I press 12345:
I see

45  32     1

I will very appreciate if you help me solve this issue.

Thanks.

zeus2kx

I modified the code a bit but still the gap between digits is not constant.
Code: [Select]
void setup() {
 Serial.begin(9600);
  TFTscreen.begin();
  TFTscreen.background(0, 0, 0);
  TFTscreen.setTextSize(2);
  TFTscreen.setCursor(0,60);
  count = 0;
}

void loop(){

    key_loop();
}

int key_loop(){
 // if (count > 100) count = 0;
 char key[100] = {keypad.getKey()};
 
 TFTscreen.text(key,count,60);
 if (key != NO_KEY){
    Serial.println(key);
    if (count > 1000) count = 0;
    delay(150);
  } 
 
  int key1 = atoi(key);
  count=count + 3;
  return(count);
}


On the display I see 1  2 3 456   7

How can I fix this?
Also, whenever I restart arduino, the digits don't start from (0,60) although in the setup, I have defined count = 0 in the setup.

Thanks.

zeus2kx

Another thing I wanted was, if I pressed 12345, and then # to save these numbers as int num.
How do I do this?

Thanks.

zeus2kx


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy