CONTER WORKS IN STEP BY STEP. NOT running CPNTINUSLY

I wrote this program to scan a button pad and check if up to add or down to subtract a 1 if the button is pressed and released and if the button is not released in a certain time add or Subtract continuously every certain time. Up is Pin 6 and Down is Pin 3.
Using Tinkertank.
If I try it step by step it works fine.
But if I run it continuously it adds 1 more before I try to Subtract or Subtract 1 before I want to change direction. Also on the first press
It starts from the preset number and not one more or one less.
Can anyone help me find out why?
Thank you all

/*  COUNTR PART BY PART F1 23-8-10
    By Antonio Domit.
    
    PARTS TO DO:
    
    1.- Ok  Set global constants.
    2.- Ok  Set global variables.
    3.- Ok  Set up Function 1. void setup();
    4.- Ok  Loop Function 2.  void loop();
    5.-     Increase Decrease function 3. int SlcIncDcr_Fnc():
    6.-     Scan function 4. ScnBtt_Fnc();
*/

//  GLOBAL CONSTANTS.

// Buttons Pad

const byte LwrBtt_Pn3 (3);	//Lower button pin.
const byte RghBtt_Pn4 (4);	//Right button pin. RINS
const byte LftBtt_Pn5 (5);	//Left button pin. BACKWASH
const byte TopBtt_Pn6 (6);	//Top button pin.
const byte StUpBtt_Pn7 (7);	//SetUp button start Pin 7 Center Button.

//  Timer constants.

const int TTC = 500;     // Time in ms to change setpoint. 
const int DTR = 200;	    // Delay to read.

//  Mesages.

const String BtnRls_Msg("REALEASE THE BUTTON: "); // Button reaese message.
const String LnsDrw_Msg("----------------"); // Draw separating lines.

const String Dwn("DOWN");
const String Up("UP");
const String Lft("LEFT");
const String Cnt("CENTER");
const String Rgh("Right");
//  ------------------------------------------

//  GLOBAL VARIABLES.

//  -------------------------------------------

void setup()
{
  Serial.begin(9600);

	pinMode(LwrBtt_Pn3, INPUT_PULLUP);
	pinMode(RghBtt_Pn4, INPUT_PULLUP);
	pinMode(LftBtt_Pn5, INPUT_PULLUP);
	pinMode(TopBtt_Pn6, INPUT_PULLUP);
	pinMode(StUpBtt_Pn7, INPUT_PULLUP);	//Declare pin 7 as input. Button status.
}
//  END of SETUP Function 1.
//  ------------------------

//  LOOP Function 2.
//  -------------------

void loop()
{
  int StUpWhlSvd = 7; // Set Up While Saved.
  
  SlcIncDcr_Fnc(StUpWhlSvd);  // Start incremant or decrement function.
}
//  END of LOOP Function 2.
//  ------------------------

//  INCREASE OR DECREASE Function 3.
//  --------------------------------

int SlcIncDcr_Fnc(int StUpWhlSvd)
{
  bool StyIncDec_Flg = 1; // 1 to stay In Increasing or decreasing function.
  byte BttPrs = 0;        // Variable to catch the button pressed.
  bool BttStt = 1;        // Whats the status of the button read.
  int StTmToQt = 5000;   // Set Time in ms to quit. 
  int TmFrCnt = 100;       // Time for continuos change.

  
  Serial.println("Down button to decrease");
  Serial.println("Up button to increase");
  //Serial.println("Left & Right button to EXIT NO SAVE");
  //Serial.println("Center button to SAVE and EXIT setup");
  Serial.println(LnsDrw_Msg); // Print a line.
  delay(DTR); // Delay to read.
  
  int TmToQt = StTmToQt;  // Time to quit = Set time to quit.
  
  while(StyIncDec_Flg == 1 && TmToQt > 0) // Keep doing this until flag = 0 or time ended.
  {
    BttPrs = 0;     // Reset the button set.
    
    BttPrs = ScnBtt_Fnc();  // Scan and store the button pressed.
    
    if(BttPrs == 3 || BttPrs == 6)
    {
      BttStt = digitalRead(BttPrs);  // Read the status of the button pressed.

      TmToQt = StTmToQt;  // Time to quit = Set time to quit to reset it.

      while(TmFrCnt > 0 && BttStt == 0)  // While there is time and button is pressed.
      {
        BttStt = digitalRead(BttPrs);  // Read the status of the button pressed.
        TmFrCnt --; // subtract 1 form the timer.
      }
         
      if(BttStt == 1) // If button was realesed before the time ended.
      {
        if(BttPrs == 3)
        {
          StUpWhlSvd --;  // subtract 1 to the previus value.
        }
        if(BttPrs == 6)
        {
          StUpWhlSvd ++;  // Add 1 to the previus value.
        }
        Serial.println(StUpWhlSvd); // Print the new value.
      }
      
      while(BttStt == 0)  // While the button is pressed.
      {
        Serial.println(StUpWhlSvd); // Print the new value.
        
        delay(TTC); // Delay time to add or substract.
        if(BttPrs == 3)
        {
          StUpWhlSvd --;  // subtract 1 to the previus value.
        }
        if(BttPrs == 6)
        {
          StUpWhlSvd ++;  // Add 1 to the previus value.
        }
        BttStt = digitalRead(BttPrs); // Check the status of the button pressed.
      }
    }
    if(BttPrs != 3 && BttPrs != 6)
    {
      TmToQt --;  // Decrease by 1 the time to quit and restart.
    }
    if(BttPrs == 3 || BttPrs == 6)
    {
      delay(10);
    }
  }
}
//  END of INCREASE OR DECREASE Function 3.
//  --------------------------------

//	SCAN BUTTONS Function 5.
// -------------------------------

byte ScnBtt_Fnc()	// Scan button to know which button was pressed.
{
	byte PnNumber[] = {3,4,5,6,7};	// Array pins numbers.
	byte ScnNmb = 0;	              // Array position to read from..
  byte BttPrs = 0;                // Which button has been pressed
  byte BttStt = 1;	                    // Button status status.
  
	while(ScnNmb < 5)	//While no pin been pressed or less than.
	{
		BttStt = digitalRead(PnNumber[ScnNmb]);	//Read and store ecah pin. one by one.
    if (BttStt == 0)	//If IS pin been pressed, then'
		{
			BttPrs = PnNumber[ScnNmb];	// Store which pin Number was pressed.
      return(BttPrs); //Return which button was pressed.
      break;	//Stop sacning pins.
    } // If NO button was press keep scaning.
		ScnNmb++;	// Increase Scan Number in the array.
	}
  BttPrs = 0; // Reset button pressed.
  return(BttPrs); // Return a 0.
}
//  END SCAN BUTTONS Function 5.
//----------------------------------
````Use code tags to format code for the forum`

I moved your topic to an appropriate forum category @tonydg.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

You are incrementing or decrementing when the 3 and 6 are pressed (0) and released (1).

Wy d y abrvt vrthng? t mks t hrd t rd yr cd. Us fll wrds s vrbls.

/*  COUNTR PART BY PART F1 23-8-10
    By Antonio Domit.

    PARTS TO DO:

    1.- Ok  Set global constants.
    2.- Ok  Set global variables.
    3.- Ok  Set up Function 1. void setup();
    4.- Ok  Loop Function 2.  void loop();
    5.-     Increase Decrease function 3. int SelectIncrementOrDecrement():
    6.-     Scan function 4. ScanAndStoreButtonPressed();
*/

//  GLOBAL CONSTANTS.

// Buttons Pad

const byte LowerButton_Pin3 (3);	//Lower button pin.
const byte RightButton_Pin4 (4);	//Right button pin. RINS
const byte LeftButton_Pin5 (5);	//Left button pin. BACKWASH
const byte TopButton_Pin6 (6);	//Top button pin.
const byte SetupButton_Pin7 (7);	//SetUp button start Pin 7 Center Button.

//  Timer constants.

const int TimeToChange = 500;     // Time in ms to change setpoint.
const int DelayToRead = 200;	    // Delay to read.

//  Mesages.

const String ButtonRelease_Message("REALEASE THE BUTTON: "); // Button reaese message.
const String LinesDraw_Message("----------------"); // Draw separating lines.

const String Dwn("DOWN");
const String Up("UP");
const String Lft("LEFT");
const String Cnt("CENTER");
const String Rgh("Right");
//  ------------------------------------------

//  GLOBAL VARIABLES.

//  -------------------------------------------

void setup()
{
  Serial.begin(9600);

  pinMode(LowerButton_Pin3, INPUT_PULLUP);
  pinMode(RightButton_Pin4, INPUT_PULLUP);
  pinMode(LeftButton_Pin5, INPUT_PULLUP);
  pinMode(TopButton_Pin6, INPUT_PULLUP);
  pinMode(SetupButton_Pin7, INPUT_PULLUP);	//Declare pin 7 as input. Button status.
}
//  END of SETUP Function 1.
//  ------------------------

//  LOOP Function 2.
//  -------------------

void loop()
{
  int SetUpWhileSaved = 7; // Set Up While Saved.

  SelectIncrementOrDecrement(SetUpWhileSaved);  // Start incremant or decrement function.
}
//  END of LOOP Function 2.
//  ------------------------

//  INCREASE OR DECREASE Function 3.
//  --------------------------------

int SelectIncrementOrDecrement(int SetUpWhileSaved)
{
  bool StayIncreasingDecreasing_Flag = 1; // 1 to stay In Increasing or decreasing function.
  byte ButtonPressed = 0;        // Variable to catch the button pressed.
  bool ButtonState = 1;        // Whats the status of the button read.
  int SetTimeToQuit = 5000;   // Set Time in ms to quit.
  int TimeForContinuousChange = 100;       // Time for continuos change.

  Serial.print("Down button to decrease. ");
  Serial.println("Up button to increase");
  //Serial.println("Left & Right button to EXIT NO SAVE");
  //Serial.println("Center button to SAVE and EXIT setup");
  Serial.println(LinesDraw_Message); // Print a line.
  delay(DelayToRead); // Delay to read.

  int TimeToQuitAndRestart = SetTimeToQuit;  // Time to quit = Set time to quit.
  while (StayIncreasingDecreasing_Flag == 1 && TimeToQuitAndRestart > 0) { // Keep doing this until flag = 0 or time ended.
    ButtonPressed = 0;     // Reset the button set.
    ButtonPressed = ScanAndStoreButtonPressed();  // Scan and store the button pressed.

    if (ButtonPressed == 3 || ButtonPressed == 6) {
      ButtonState = digitalRead(ButtonPressed);  // Read the status of the button pressed.
      TimeToQuitAndRestart = SetTimeToQuit;  // Time to quit = Set time to quit to reset it.

      while (TimeForContinuousChange > 0 && ButtonState == 0) { // While there is time and button is pressed.
        ButtonState = digitalRead(ButtonPressed);  // Read the status of the button pressed.
        TimeForContinuousChange --; // subtract 1 form the timer.
      }

      if (ButtonState == 1) { // If button was realesed before the time ended.
        if (ButtonPressed == 3) {
          SetUpWhileSaved --;  // subtract 1 to the previus value.
        }
        if (ButtonPressed == 6) {
          SetUpWhileSaved ++;  // Add 1 to the previus value.
        }
        Serial.println(SetUpWhileSaved); // Print the new value.
      }

      while (ButtonState == 0) { // While the button is pressed.
        Serial.println(SetUpWhileSaved); // Print the new value.
        delay(TimeToChange); // Delay time to add or substract.
        if (ButtonPressed == 3) {
          SetUpWhileSaved --;  // subtract 1 to the previus value.
        }
        if (ButtonPressed == 6) {
          SetUpWhileSaved ++;  // Add 1 to the previus value.
        }
        ButtonState = digitalRead(ButtonPressed); // Check the status of the button pressed.
      }
    }
    if (ButtonPressed != 3 && ButtonPressed != 6) {
      TimeToQuitAndRestart --;  // Decrease by 1 the time to quit and restart.
    }
    if (ButtonPressed == 3 || ButtonPressed == 6) {
      delay(10);
    }
  }
}
//  END of INCREASE OR DECREASE Function 3.
//  --------------------------------

//	SCAN BUTTONS Function 5.
// -------------------------------

byte ScanAndStoreButtonPressed()	// Scan button to know which button was pressed.
{
  byte PinNumbers[] = {3, 4, 5, 6, 7};	// Array pins numbers.
  byte ScanNumber = 0;	              // Array position to read from..
  byte ButtonPressed = 0;                // Which button has been pressed
  byte ButtonState = 1;	                    // Button status status.

  while (ScanNumber < 5)	//While no pin been pressed or less than.
  {
    ButtonState = digitalRead(PinNumbers[ScanNumber]);	//Read and store ecah pin. one by one.
    if (ButtonState == 0)	//If IS pin been pressed, then'
    {
      ButtonPressed = PinNumbers[ScanNumber];	// Store which pin Number was pressed.
      return (ButtonPressed); //Return which button was pressed.
      break;	//Stop sacning pins.
    } // If NO button was press keep scaning.
    ScanNumber++;	// Increase Scan Number in the array.
  }
  ButtonPressed = 0; // Reset button pressed.
  return (ButtonPressed); // Return a 0.
}
//  END SCAN BUTTONS Function 5.
//----------------------------------

Sorry for that I thought that the proper category was using Arduino.
Where should I have written it?

Thank you xfpd.
I did that to write faster and with fewer errors in the code. I try to avoid using vocals and describe what it is with 3 or fewer abbreviations. I agree clearer the way you wright it Thank you.
But why isn't working?

You are incrementing/decrementing when the 3/6 are pressed (0) and also released (1). That seems to me to add or subtract twice.

Hello tonydg

All in all, you are trying to programme a button manager.

In general, a button manager must perform the following tasks:

  1. read and debounce buttons
  2. recognise state changes
  3. perform action on state change

These tasks must be performed "simultaneously" if you have more than one button.

My suggestion - my design recipe

In general, arrays and structs are your friends.
Do not duplicate code in your sketch. Write code once - use it multiple times.
You should not use magic numbers. I/O pins love to have a real functional name.

Do you have experience with programming in C++?

The task can easily be realised with an object.
A structured array contains all the information, such as the pin addresses for the I/O devices, as well as the information for timing.
A single service takes care of this information and triggers the desired action.
The structured array makes the sketch scalable until all I/O pins are used up without having to adjust the code for the service.
That's cool, isn't it?

Have a nice day and enjoy programming in C++.

look this over

// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

// -----------------------------------------------------------------------------
int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

        if (butState [n] != but)  {
            butState [n] = but;

            delay (10);     // debounce

            if (On == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case 2:
        digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
        break;

    case 1:
        digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
        break;

    case 0:
        digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }
}

Thank you, Paul.
That is what I was trying to do. About the names I like to keep them as short as possible to avoid more mistakes but make them more unreadable I might change that idea.

Thank you GCJR.
That will help a lot.

Ok Thank you I will change it.