How to return from while loop [solved]

I'm using while because, I would like to everything in the program stop when I press *, then I can put a variable (step interval) for stepper motor and finish while loop when I press #. How to improve?

while (customKey == '*')
  {
      if (customKey >= '0' && customKey <= '9')
  {
    num = num * 10 + (customKey - '0');
    tft.setTextColor(ST7735_BLACK);
    tft.setTextSize(2);
    tft.setCursor(75, 140);
    tft.println(num);
  }       
   if (customKey == '#')
  {
    customKey = NO_KEY;
  }
  }
  1. layout your code the he auto formatter on the tool bar

  2. post ALL your code

  3. look at your logic if customKey is * then it can't be anything else

Mark

#include <Keypad.h>
#include <Adafruit_GFX.h>    
#include <Adafruit_ST7735.h> 
#include <SPI.h>

int relay1Pin = 23;   // relay
int relay2Pin = 25;
int relay3Pin = 27;
//int relay3Pin = 29;
int m0 = 39;          // stepper
int m1 = 41;
int sleepPin = 47;   
const int stepPin = 49;
int dirPin = 51;

#define sclk 76    //TFT 
#define mosi 75
#define cs   10
#define dc   9
#define rst  8     

int stepPinState = LOW;           
long previous1 = 0;
long previous2 = 0;
long previous3 = 0;
long interval = 500;
static byte runStepper = 0;
static byte autoStepper = 0; 
long countImpulse = 0;
int lastStepPinState = LOW;
int stepperRev = 0;
int countDistance = 0;
String showTask = "GOTOWY";
int sec1 = 0;
int sec2 = 0;
int min1 = 0;
int min2 = 0;
int num = 0;
int num0 = 0;
//int runFeedValue = 0;
int resolution = 1;
int go = 0;




Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

const byte ROWS = 4;   // four rows
const byte COLS = 4;   // four columns
// define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {
    '1','2','3','A'                                                      }
  ,
  {
    '4','5','6','B'                                                      }
  ,
  {
    '7','8','9','C'                                                      }
  ,
  {
    '*','0','#','D'                                                      }
};
byte rowPins[ROWS] = {
  22, 24, 26, 28};   // connect to the row pinouts of the keypad
byte colPins[COLS] = {
  30, 32, 34, 36};   // connect to the column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);   // initialize an instance of class NewKeypad

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

  customKeypad.addEventListener(keypadEvent);
  //customKeypad.addEventListener(keypadEvent2);
  customKeypad.setHoldTime(0); 

  pinMode(relay1Pin, OUTPUT);     // relay       
  pinMode(relay2Pin, OUTPUT);
  pinMode(relay3Pin, OUTPUT);
  //pinMode(relay4Pin, OUTPUT);

  pinMode(m0, OUTPUT);        // stepper
  pinMode(m1, OUTPUT);
  pinMode(sleepPin, OUTPUT);     
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  digitalWrite(m0, LOW);       // resolution = 1 step
  digitalWrite(m1, LOW);       //

  tft.initR(INITR_REDTAB);
  tft.fillScreen(ST7735_WHITE);


}

void loop()
{  
  char customKey = customKeypad.getKey();


  if (customKey == '0')             // distance - reset
  {     
    countDistance = 0;
  }

  if (customKey == '1')             // relay 1 on 
  {     
    digitalWrite(relay1Pin,LOW);
  }

  if (customKey == '4')             // relay 1 off
  {     
    digitalWrite(relay1Pin,HIGH);     
  }

  if (customKey == '2')             // relay 2 on 
  {     
    digitalWrite(relay2Pin,LOW);
    tft.drawRect(4, 139, 20, 20, ST7735_RED);
    tft.setCursor(8, 142);
    tft.println("S");
  }

  if (customKey == '5')            // relay 2 off
  {     
    digitalWrite(relay2Pin,HIGH);
    digitalWrite(relay3Pin,HIGH);
    tft.setCursor(8, 142);
    tft.fillRect(4,139,20,20,ST7735_WHITE);  
  }

  if (customKey == '3')             // relay 3 on 
  {     
    digitalWrite(relay3Pin,LOW);
    tft.drawRect(34, 139, 20, 20, ST7735_RED);
    tft.setCursor(38, 142);
    tft.println("P"); 
  }

  if (customKey == '6')            // relay 3 off
  {     
    digitalWrite(relay3Pin,HIGH);
    tft.setCursor(38, 142);
    tft.fillRect(34,139,20,20,ST7735_WHITE);    
  }   


  while (customKey == '*')                     //feed for stepper motor
  {
    if (customKey >= '0' && customKey <= '9')
    {
      num = num * 10 + (customKey - '0');
      tft.setTextColor(ST7735_BLACK);
      tft.setTextSize(2);
      tft.setCursor(75, 140);
      tft.println(num);
    }       
    if (customKey == '#')
    {
      customKey = NO_KEY;
    }
  }  





  if (customKey == 'C')    //stepper stop
  {
    runStepper = 0;
    autoStepper = 0;
    digitalWrite(sleepPin, LOW);
    showTask = "STOP";
  }

  if (runStepper == 0)             // stepper - stand by off
  {    
    digitalWrite(sleepPin, LOW);  
  }


  if (runStepper >= 1)             // delay for a stepper
  {
    unsigned long current1 = micros();  

    if(current1 - previous1 > interval)         
    {        
      previous1 = current1;         
      if (stepPinState == LOW)       
        stepPinState = HIGH;
      else
        stepPinState = LOW;   
      digitalWrite(stepPin, stepPinState);
    }
  }


  if (customKey == 'D')       // stepper - automatic feed
  {
    autoStepper = 1;
    showTask = "AUTO";
  }

  analogReadResolution(12);            // read sensor value
  int sensorValue = analogRead(A0);    
  int currentValue = sensorValue * (3.3 / 4096.0)*(100 / 2.47);

  if (currentValue >= 244/2.47 &&  autoStepper==1)
  {
    runStepper = 3;
    digitalWrite(sleepPin, HIGH);
    digitalWrite(dirPin, HIGH);
  }

  if (currentValue < 244/2.47 && autoStepper==1)
  {
    runStepper = 0;
    digitalWrite(sleepPin, LOW);
  }

  countImpulse = 0;
  if (stepPinState != lastStepPinState) // counting steps of stepper
  { 
    if (stepPinState == HIGH)
    {  
      countImpulse = 1;
      countDistance;
      if (runStepper == 1)
      {
        countDistance = countDistance + 1;
      }
      if (runStepper == 2)
      {
        countDistance = countDistance - 1;
      }
      if (runStepper == 3)
      {
        countDistance = countDistance + 1;        
      }

    }
  }
  lastStepPinState = stepPinState;


  if (autoStepper == 1)                 // timer
  { 
    unsigned long current2 = millis();  
    if(current2 - previous2 > 100)
    { 
      sec2++;
      previous2 = current2;
    }  
  }
  if (sec2 == 10)
  {
    sec1++;
    sec2 = 0;
  }
  if (sec1 == 6)
  {
    min2++;
    sec1 = 0;
  }
  if (min2 == 10)
  {
    min1++;
    min2 = 0;
  }




  unsigned long current3 = millis();  // delay for TFT
  if(current3 - previous3 > 2000)
  {
    tft.fillRect(5,0,80,16,ST7735_WHITE);
    tft.fillRect(5,40,100,16,ST7735_WHITE);
    tft.fillRect(5,80,60,16,ST7735_WHITE);
    tft.fillRect(66,100,62,16,ST7735_WHITE);
    tft.fillRect(75,120,42,16,ST7735_WHITE);

    //tft.setRotation(1);
    tft.setTextColor(ST7735_BLACK);
    tft.setTextSize(2);
    tft.setCursor(5, 1);
    tft.println(showTask);

    tft.setCursor(5, 20);
    tft.println("Dystans:");
    tft.setCursor(5, 40);
    tft.println(countDistance*0.01);

    tft.setCursor(5, 60);
    tft.println("Prad [A]:");
    tft.setCursor(5, 80);
    tft.println((currentValue*0.01)-1);
    tft.setCursor(5, 100);

    tft.println("Czas:");
    tft.setCursor(66, 100);
    tft.println(min1);
    tft.setCursor(78, 100);
    tft.println(min2);
    tft.setCursor(90, 100);
    tft.println(":");
    tft.setCursor(102, 100);
    tft.println(sec1);
    tft.setCursor(114, 100);
    tft.println(sec2);

    tft.setCursor(5, 120);    
    tft.println("Posuw:");
    tft.setCursor(75, 120);
    tft.println(interval);
    previous3 = current3;
  }

}



void keypadEvent(KeypadEvent customKey)
{
  switch (customKeypad.getState())
  {
  case HOLD:
    if (customKey == 'A')  // stepper - right direction
    {
      runStepper = 1;
      digitalWrite(sleepPin, HIGH);
      digitalWrite(dirPin, LOW);
      showTask = "GORA";    
    }
    if (customKey == 'B')  // stepper - left direction
    {
      runStepper = 1;
      digitalWrite(sleepPin, HIGH);
      digitalWrite(dirPin, HIGH);
      showTask = "DOL";    
    }               
    break;
  case RELEASED:
    if (customKey == 'A' || customKey == 'B')  
    {
      runStepper = 0;
      autoStepper = 0;
      digitalWrite(sleepPin, LOW);
      showTask = "STOP";
    }
    break;
  }
}
 while (customKey == '*')                     //feed for stepper motor
  {
    if (customKey >= '0' && customKey <= '9')

Why test?
It can't possibly be be anything but '*' at this point.

I do something like this, now I can put value , but I still don't now how to finish loop.

void loop()
{
while (customKey == '*')
  {
  runFeedValue();
  {
}


void runFeedValue()
{
  char customKey = customKeypad.getKey();
  if (customKey >= '0' && customKey <= '9')
  {
    num = num * 10 + (customKey - '0');
    tft.setTextColor(ST7735_BLACK);
    tft.setTextSize(2);
    tft.setCursor(75, 140);
    tft.println(num);
  }  
}

You've just declared a new "customKey" that isn't in the same scope as your "while" loop.
Just take the code out of the function and put it in the "while" loop, but lose the declaration of the new "customkey" - just make it an assignment.

Thank you. This is my code, it's working perfectly.

while (customKey == '*')
  {   
    char customKey2 = customKeypad.getKey();
    if (customKey2 >= '0' && customKey2 <= '9')
    {
      num = num * 10 + (customKey2 - '0');
      tft.setTextColor(ST7735_BLACK);
      tft.setTextSize(2);
      tft.setCursor(75, 140);
      tft.println(num);      
    }       
    if (customKey2 == '#')
    {    
      customKey = NO_KEY;      
    }
  }