Operating stepper without delay

How to avoid delay, when I press A (code with delay) it's work good, but when I press B (code without delay) it's not good, how to modify code?

int relay1Pin = 23;   // relay
int relay2Pin = 25;
int relay3Pin = 27;
int relay4Pin = 29;

int sleepPin = 47;   // stepper
const int stepPin = 49;
int dirPin = 51;

           
long previousMillis = 0;        
long interval = 1000;  


#include "czujnik_pradu.h"
#include <Keypad.h>


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);

    
  pinMode(relay1Pin, OUTPUT);     // relay       
  pinMode(relay2Pin, OUTPUT);
  pinMode(relay3Pin, OUTPUT);
  pinMode(relay4Pin, OUTPUT);
  
  pinMode(sleepPin, OUTPUT);     // stepper
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  
}
 
  
void loop()
{  
  
char customKey = customKeypad.getKey();

  
    if (customKey=='A')             // stepper
    {     
      int i;
      digitalWrite(sleepPin, HIGH);
      digitalWrite(dirPin, LOW);     // Set the direction.

      for (i = 0; i<4000; i++)       // Iterate for 4000 microsteps.
      {
      digitalWrite(stepPin, LOW);    // This LOW to HIGH change is what creates the
      digitalWrite(stepPin, HIGH);   // "Rising Edge" so the easydriver knows to when to step.
      delayMicroseconds(1000);       // This delay time is close to top speed for this
      }       
    }
    
    if (customKey=='B')             // stepper
    {     
      int i;
      digitalWrite(sleepPin, HIGH);
      digitalWrite(dirPin, LOW);     // Set the direction.
      
      for (i = 0; i<4000; i++)       // Iterate for 4000 microsteps.
      {
      unsigned long currentMillis = micros();
      if(currentMillis - previousMillis > interval)
       
      step();
      
      previousMillis = currentMillis;  // indented properly
      }      
      }
}

void step()
{
   static int lastStep = 0;
   switch(lastStep)
   {
      case 0:      
    digitalWrite(49, LOW);
    digitalWrite(49, HIGH);
         break;
      case 1:      
   digitalWrite(49, LOW);
    digitalWrite(49, HIGH);
         break;
    
    
   }
   lastStep++;
   if(lastStep > 1)
     lastStep = 0;
}

Clarify what you mean by "no good". (I have my guess, but I'd rather hear the facts)

The biggest mistake people when trying to get rid of delays is simply replacing delays with the BlinkWithoutDelay code. Putting said code inside a for loop in never going to work as desired. You need to separate the code that tells it to start from the code that tells it to continue:

static byte runFlag = 0;

if (someConditionToStart)
{
  runFlag = 1;
  // other code to setup the movement
}

if (runFlag)
{
  // BlinkWithoutDelay code goes here
  if (someConditionToStop)
  {
    runFlag = 0;
  }
}

Thank you Arrch, stepper run perfectly, but I have another problem, when I connect current sensor, stepper not work good (lower and unstable).

int relay1Pin = 23;   // relay
int relay2Pin = 25;
int relay3Pin = 27;
int relay4Pin = 29;

int sleepPin = 47;   // stepper
const int stepPin = 49;
int dirPin = 51;
  
int stepPinState = LOW;           
long previousMillis = 0;        
long interval = 500;
static byte runStepper = 0;


#include "sensor.h"
#include <Keypad.h>


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);

    
pinMode(relay1Pin, OUTPUT);     // relay       
pinMode(relay2Pin, OUTPUT);
pinMode(relay3Pin, OUTPUT);
pinMode(relay4Pin, OUTPUT);
  
pinMode(sleepPin, OUTPUT);     // stepper
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
  
}
 
  
void loop()
{  
  
char customKey = customKeypad.getKey();

if (customKey == '1')             // relay 1 on 
{     
digitalWrite(relay1Pin,LOW);
}
    
if (customKey == '4')             // relay 1 off
{     
digitalWrite(relay1Pin,HIGH);     
}
  
if (customKey == '2')             // relay 1 on 
{     
digitalWrite(relay2Pin,LOW);
}
  
if (customKey == '5')            // relay 2 off
{     
digitalWrite(relay2Pin,HIGH);           
}
    
    
    if (runStepper == 0)             // stepper
    {    
    digitalWrite(sleepPin, LOW);   
    }
 
 
    if (customKey == 'A')             // stepper
    {    
    runStepper = 1;
    digitalWrite(sleepPin, HIGH);
    digitalWrite(dirPin, LOW);     // Set the direction.     
    }

    
    if (customKey == 'B')             // stepper
    {     
    runStepper = 2;
    digitalWrite(sleepPin, HIGH);
    digitalWrite(dirPin, HIGH);     // Set the direction.
    }
    
        if (runStepper >= 1)    
        {
        unsigned long currentMillis = micros();
        if(currentMillis - previousMillis > interval) 
        {
        digitalWrite(stepPin, stepPinState);
        previousMillis = currentMillis;   
        if (stepPinState == LOW)
        stepPinState = HIGH;
        else
        stepPinState = LOW;
        digitalWrite(stepPin, stepPinState);
        }
        }
        
    if (customKey == 'C') 
    {
    runStepper = 0;
    digitalWrite(sleepPin, LOW);
    }

sensor();       //current sensor
        
}
void sensor() 
{
  int sensorValue = analogRead(A0);  // read the input on analog pin 0:
  float voltage = sensorValue * (3.3 / 1024.0);  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 3,3V):
  
  Serial.println(voltage);  // print out the value you read:

}

It's really hard to
see what your code
is supposed to
be doing when
it jerks all over the
page.

Use Tools + Auto Format before posting code, so that at least we can read it. I think that you'll find that YOU have an easier time, too.

No problem, here you are

int relay1Pin = 23;   // relay
int relay2Pin = 25;
int relay3Pin = 27;
int relay4Pin = 29;

int sleepPin = 47;   // stepper
const int stepPin = 49;
int dirPin = 51;

int stepPinState = LOW;           
long previousMillis = 0;        
long interval = 100;
static byte runStepper = 0;


#include "sensor.h"
#include <Keypad.h>


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);


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

  pinMode(sleepPin, OUTPUT);     // stepper
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);

}


void loop()
{  

  char customKey = customKeypad.getKey();

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

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

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

  if (customKey == '5')            // relay 2 off
  {     
    digitalWrite(relay2Pin,HIGH);           
  }


  if (runStepper == 0)             // stepper
  {    
    digitalWrite(sleepPin, LOW);   
  }


  if (customKey == 'A')             // stepper
  {    
    runStepper = 1;
    digitalWrite(sleepPin, HIGH);
    digitalWrite(dirPin, LOW);     // Set the direction.     
  }


  if (customKey == 'B')             // stepper
  {     
    runStepper = 2;
    digitalWrite(sleepPin, HIGH);
    digitalWrite(dirPin, HIGH);     // Set the direction.
  }

  if (runStepper >= 1)    
  {
    unsigned long currentMillis = micros();
    if(currentMillis - previousMillis > interval) 
    {
      digitalWrite(stepPin, stepPinState);
      previousMillis = currentMillis;   
      if (stepPinState == LOW)
        stepPinState = HIGH;
      else
        stepPinState = LOW;
      digitalWrite(stepPin, stepPinState);
    }
  }

  if (customKey == 'C') 
  {
    runStepper = 0;
    digitalWrite(sleepPin, LOW);
  }

  //sensor();       //current sensor

}

With the call to sensor() uncommented, I'm guessing that it takes more than one millisecond to read the sensor, print the value, and get back to determining that the stepper needs to step again. That will result in the stepper being slower. Though it really isn't clear exactly what "lower and unstable" means with respect to a stepper motor.

It's not call sensor(), it's something different but stepper run much slower and unstable. I think sensor disrupts motor works. Is it possible that is something wrong with micros function when sensor work.

Your sensor calculations involve floating point arithmetic. The microcontrollers on the Arduino's don't have an FPU, so software emulation needs to be used. This means that it takes much longer to make floating point calculations than it does integer calculations. You're also printing to the Serial monitor unconditionally on every pass through loop; don't do that.

It's not call sensor(), it's something different

Really?

  //sensor();       //current sensor
void sensor() 
{
  int sensorValue = analogRead(A0);  // read the input on analog pin 0:
  float voltage = sensorValue * (3.3 / 1024.0);  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 3,3V):
  
  Serial.println(voltage);  // print out the value you read:

}

Looks like it is called sensor(), to me.

Thank you Arrch and PaulS, now it's work good.

void sensor() 
{
  int sensorValue = analogRead(A0);  // read the input on analog pin 0:
  int voltage = sensorValue * (3.3 / 1024.0)*100;  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 3,3V):

  unsigned long current2 = millis();
  if(current2 - previous2 > 2000)
  {
    Serial.println(voltage);  // print out the value you read:
    previous2 = current2;
  }

}

I have another problem, Why this code doesn't work. I think something is wrong with variable currentValue.

 int sensorValue = analogRead(A0);  // read the input on analog pin 0:
 int currentValue = sensorValue * (3.3 / 1024.0)*100;  
   
    if (customKey == 'D' &&  currentValue > 244)    // stepper - Auto
    {     
    runStepper = 3;
    digitalWrite(sleepPin, HIGH);
    digitalWrite(dirPin, HIGH);   
    }

It's work