Help to make a button that doesn't loop the action while pressed

Hello, I have a Waveshare 4" TFT touch screen LCD and I am trying to do a passcode program. I am using the library given for this screen, it’s not adafruit or other known libraries. I made buttons from 1 to 9 but when pressed, the action that is inside the if condition is executed in a loop while the button is pressed. So if I want to input the number 7 by pressing the button “7” once, I will get 7 like 10 times. I tried different things, it didn’t work. I would want the button to make its action only when I remove my finger from it or to do the action only once, even if I let my finger on it for a longer time. Thanks for help, here are some screenshots of the program:

Arduino2.PNG

Setup...??

It doesn't work

Place your code as shown in the first section, maybe you'll get some assistance then.
Photos of snippets are no help to anyone.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks... Tom... :slight_smile:

#include "DEV_Config.h"
#include "LCD_Driver.h"
#include "LCD_GUI.h"
#include "LCD_Touch.h"

void setup()
{
  System_Init();

  Serial.println("4inch TFT Touch Shield Touch Demo");
  Serial.println("Init...");
  LCD_SCAN_DIR Lcd_ScanDir = SCAN_DIR_DFT;    //SCAN_DIR_DFT = D2U_L2R
  LCD_Init( Lcd_ScanDir, 200);  

  Serial.println("Init Touch Pad...");
  TP_Init( Lcd_ScanDir );
  TP_GetAdFac();
  //TP_Dialog();

 //TP_MyGUI();
 TP_Code();


}

void loop()
{
TP_TestRct();


TP_VerifCode();
  
  //TP_DrawBoard();
}
void TP_Code()
{
  GUI_Clear(WHITE);

  //First row
  GUI_DrawRectangle(30, 20, 110, 100, CYAN, DRAW_FULL, DOT_PIXEL_DFT);
  GUI_DisString_EN(62, 52, "7", &Font24, FONT_BACKGROUND, BLACK);

  GUI_DrawRectangle(30, 120, 110, 200, CYAN, DRAW_FULL, DOT_PIXEL_DFT);
  GUI_DisString_EN(62, 152, "4", &Font24, FONT_BACKGROUND, BLACK);
  
  GUI_DrawRectangle(30, 220, 110, 300, CYAN, DRAW_FULL, DOT_PIXEL_DFT);
  GUI_DisString_EN(62, 252, "1", &Font24, FONT_BACKGROUND, BLACK);

  //Second row
  GUI_DrawRectangle(130, 20, 210, 100, CYAN, DRAW_FULL, DOT_PIXEL_DFT);
  GUI_DisString_EN(162, 52, "8", &Font24, FONT_BACKGROUND, BLACK);
  
  GUI_DrawRectangle(130, 120, 210, 200, CYAN, DRAW_FULL, DOT_PIXEL_DFT);
  GUI_DisString_EN(162, 152, "5", &Font24, FONT_BACKGROUND, BLACK);
  
  GUI_DrawRectangle(130, 220, 210, 300, CYAN, DRAW_FULL, DOT_PIXEL_DFT);
  GUI_DisString_EN(162, 252, "2", &Font24, FONT_BACKGROUND, BLACK);

  //Tird row
  GUI_DrawRectangle(230, 20, 310, 100, CYAN, DRAW_FULL, DOT_PIXEL_DFT);
  GUI_DisString_EN(262, 52, "9", &Font24, FONT_BACKGROUND, BLACK);
  
  GUI_DrawRectangle(230, 120, 310, 200, CYAN, DRAW_FULL, DOT_PIXEL_DFT);
  GUI_DisString_EN(262, 152, "6", &Font24, FONT_BACKGROUND, BLACK);
  
  GUI_DrawRectangle(230, 220, 310, 300, CYAN, DRAW_FULL, DOT_PIXEL_DFT);
  GUI_DisString_EN(262, 252, "3", &Font24, FONT_BACKGROUND, BLACK);

  //Enter button
  GUI_DrawRectangle(330, 120, 460, 200, RED, DRAW_FULL, DOT_PIXEL_DFT);
  GUI_DisString_EN(355, 152, "ENTER", &Font24, FONT_BACKGROUND, BLACK);
  
  }
int num1, num2, num3, num4 = 0;

void TP_VerifCode()
{
  if(num1 == 7, num2 == 4, num3 == 9, num4 == 2)
  {
    Serial.println("CODE CORRECT");
    GUI_DisString_EN(330, 230, "ACCESS GRANTED", &Font12, FONT_BACKGROUND, GREEN);
    }
  
  
  }




//This function is called in loop(). Please ignore the comments, they are just tests. Also there are
//a lot of other files that I think are not useful for fixing this problem. Oh and this program is //based on an example, because there is not a lot of documentation for the use of this touch //screen.
void TP_TestRct()
{
  //Serial.println(num1);
  //Serial.println(num2);
  TP_Scan(0);

  if(num1 == 0, num2 == 0, num3 == 0, num4 == 0)
  {
if(num1 == 0) num1 = num;
else if(num2 == 0) num2 = num;
else if(num3 == 0) num3 = num;
else if(num4 == 0) num4 = num;
Serial.println(num);
  if (sTP_DEV.chStatus & TP_PRESS_DOWN) 
  { //Press the button
    //Horizontal screen
    if (sTP_Draw.Xpoint < sLCD_DIS.LCD_Dis_Column &&
        //Determine whether the law is legal
        sTP_Draw.Ypoint < sLCD_DIS.LCD_Dis_Page) 
        {
          //Judgment is horizontal screen
          if (sLCD_DIS.LCD_Dis_Column > sLCD_DIS.LCD_Dis_Page) 
          {
              if (sTP_Draw.Xpoint > 30 && sTP_Draw.Xpoint < 110 && sTP_Draw.Ypoint > 20 && sTP_Draw.Ypoint < 100) 
              {    
               
               if(num1 == 0) num1 = 7;
                 else if(num2 == 0) num2 = 7;
                 else if(num3 == 0) num3 = 7;
                 else if(num4 == 0) num4 = 7;
                 Serial.println("7");
                 
              

                 
                }
              }
              
              }
              if (sTP_Draw.Xpoint > 30 && sTP_Draw.Xpoint < 110 && sTP_Draw.Ypoint > 120 && sTP_Draw.Ypoint < 200) 
              {    
                if(num1 == 0) num1 = 4;
                 else if(num2 == 0) num2 = 4;
                 else if(num3 == 0) num3 = 4;
                 else if(num4 == 0) num4 = 4;
                 Serial.println("4");
          

              }
              if (sTP_Draw.Xpoint > 30 && sTP_Draw.Xpoint < 110 && sTP_Draw.Ypoint > 220 && sTP_Draw.Ypoint < 300) 
              {    
                if(num1 == 0) num1 = 1;
                 else if(num2 == 0) num2 = 1;
                 else if(num3 == 0) num3 = 1;
                 else if(num4 == 0) num4 = 1;
                 Serial.println("1");

              }
              
              if (sTP_Draw.Xpoint > 130 && sTP_Draw.Xpoint < 210 && sTP_Draw.Ypoint > 20 && sTP_Draw.Ypoint < 100) 
              {    
                if(num1 == 0) num1 = 8;
                 else if(num2 == 0) num2 = 8;
                 else if(num3 == 0) num3 = 8;
                 else if(num4 == 0) num4 = 8;
                 Serial.println("8");
              }
              if (sTP_Draw.Xpoint > 130 && sTP_Draw.Xpoint < 210 && sTP_Draw.Ypoint > 120 && sTP_Draw.Ypoint < 200) 
              {    
                if(num1 == 0) num1 = 5;
                 else if(num2 == 0) num2 = 5;
                 else if(num3 == 0) num3 = 5;
                 else if(num4 == 0) num4 = 5;
                 Serial.println("5");

              }
              if (sTP_Draw.Xpoint > 130 && sTP_Draw.Xpoint < 210 && sTP_Draw.Ypoint > 220 && sTP_Draw.Ypoint < 300) 
              {    
                if(num1 == 0) num1 = 2;
                 else if(num2 == 0) num2 = 2;
                 else if(num3 == 0) num3 = 2;
                 else if(num4 == 0) num4 = 2;
                 Serial.println("2");

              }

              if (sTP_Draw.Xpoint > 230 && sTP_Draw.Xpoint < 310 && sTP_Draw.Ypoint > 20 && sTP_Draw.Ypoint < 100) 
              {    
                if(num1 == 0) num1 = 9;
                 else if(num2 == 0) num2 = 9;
                 else if(num3 == 0) num3 = 9;
                 else if(num4 == 0) num4 = 9;
                 Serial.println("9");

              }
              if (sTP_Draw.Xpoint > 230 && sTP_Draw.Xpoint < 310 && sTP_Draw.Ypoint > 120 && sTP_Draw.Ypoint < 200) 
              {    
                if(num1 == 0) num1 = 6;
                 else if(num2 == 0) num2 = 6;
                 else if(num3 == 0) num3 = 6;
                 else if(num4 == 0) num4 = 6;
                 Serial.println("6");

              }
              if (sTP_Draw.Xpoint > 230 && sTP_Draw.Xpoint < 310 && sTP_Draw.Ypoint > 220 && sTP_Draw.Ypoint < 300) 
              {    
                if(num1 == 0) num1 = 3;
                 else if(num2 == 0) num2 = 3;
                 else if(num3 == 0) num3 = 3;
                 else if(num4 == 0) num4 = 3;
                 Serial.println("3");

              }
          }
      
        }
}
  if(num1 == 0, num2 == 0, num3 == 0, num4 == 0)

The comma operator (,) does not do what you think it does. I think you are looking for OR which is written as ||

Any time you have numbers in your variable names, you should consider using an array. Look it up in the Arduino reference.

Oh no that part is just to verify the code. That’s not the problem. The part I have problems with is just a bit under that one.

I thought this part verifies the code...

  if(num1 == 7, num2 == 4, num3 == 9, num4 == 2)

In this line the AND operator && would be indicated.

The problem is that when I press on the button, for example 7, the loop will detect many times that I am pressing the button and it will input 77777777... instead of just 7. If a video would be usefull say it please! Thanks!

You are detecting that the button IS pressed. You need to detect when it BECOMES pressed.

There are lots of examples showing how to do this with regular buttons. Very few will use onscreen buttons in the example as that is extra complication.

Look at the StateChange example in your Arduino examples folder.