how to read 4 digit numbers via ttp229 touch keypad

Hi.
I’m a beginner with Arduino programming and need some advice.
First of all I want to give my apologies because of my english and my programming capabilities.
I’m using Fingerprint sensor that works with AT Commands. one of the AT Commands is “AT+ADD=xxxx”; which xxxx is a 4 digit number.
this command adds a new finger template to the memory of FP sensor. (ex:AT+ADD=0012, this command stores a finger template to 0012!! )
What I need is to read 4 digit numbers via ttp229 touch keypad and then add “AT+ADD=” to it
and just send it via serial to FP sensor.

the code I use is:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f,16 ,2); // set the LCD address to 0x3f for a 16 chars and 2 line display
#include <SoftwareSerial.h>
SoftwareSerial FP(2, 3);
#define SCL_PIN 9
#define SDO_PIN 10

/* Used to store the key state */
byte Key;

void setup() {

   /* Configure the clock and data pins */
  pinMode(SCL_PIN, OUTPUT);  
  pinMode(SDO_PIN, INPUT);
 
  pinMode(12,OUTPUT); // just a LED to let u know a key is touched.
  Serial.begin(19200);
  FP.begin(9600);
  lcd.init();
  lcd.backlight();
  delay(100);  
}

void loop() {
  
       Key = Read_Keypad(); //read from keypad
   
   if (Serial.available()) {
    delay(100);
    FP.write(Serial.read());
  }
   
    while (FP.available()>0) {
      char i = FP.read();
      Serial.write(i);
      lcd.print(i);
    }
   

   

   if (Key == 11){
    digitalWrite(12,1);
    lcd.clear();
    FP.println("AT+CHECK"); // cheking finger template, works fine.
   } else {
    digitalWrite(12,0);
   }

    if (Key == 12){
    digitalWrite(12,1);
    lcd.clear();
    FP.println("AT"); // gives version of FP sensor, works fine.
   } else {
    digitalWrite(12,0);
   }
  
 if (Key == 13){
    digitalWrite(12,1);
    lcd.clear();
    FP.println("AT+EMPTYMEM?"); // gives empty memories, works fine.
   } else {
    digitalWrite(12,0);
   }

if  (Key == 14){
  digitalWrite(12,1);
  lcd.clear();
  GenerateCommand(); // HERE IS THE PROBLEM :( 
} else {
  digitalWrite(12,0);


}



}


byte Read_Keypad(void)
{
  byte Count;
  byte Key_State = 0;

  /* Pulse the clock pin 16 times (one for each key of the keypad) 
     and read the state of the data pin on each pulse */
  for(Count = 1; Count <= 16; Count++)
  {
    digitalWrite(SCL_PIN, LOW); 
    
    /* If the data pin is low (active low mode) then store the 
       current key number */
    if (!digitalRead(SDO_PIN))
      Key_State = Count; 
    
    digitalWrite(SCL_PIN, HIGH);
  }  
  
  return Key_State; 
}



// generating ("AT+ADD=xxxx") & send it to Fingerprint

 String GenerateCommand() {
  String command;
  String temp;
  int num=Get4digit();
  if (num < 1000) {
    temp=String(num, DEC); 
    temp=String("0"+temp);
  } else {
    temp=String(num, DEC);
  }
  command=String("AT+ADD="+temp);
}
int Get4digit(){
  int num = 0;
       Key = Read_Keypad();
  int counter = 0;
  while (counter < 4){
    switch (Key){
      
      case 1: case 2: case 3: case 4: case 5:
      case 6: case 7: case 8: case 9:
        num = num * 10 + (int)Key;
        break;

      case 10:
        num = num * 10;
        break;
    }
    delay(150);
       Key = Read_Keypad();
    counter++;
  }
   
  return num;
}

I use number 10 of keypad as zero, since it doesn’t have zero number.

when I touch key num 14 it just gives “AT+ADD=00”
I don’t know how to solve it :frowning:
please help me thank you in advance… :slight_smile:

Ditch about 98% of that code until you KNOW that you are reading the keypad correctly.