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