Building off the example from @gcjr, this illustrates how you can do several things
- the LED blinking is totally contained without its own function. Just call it often and it updates as needed
- checking for a button press every time through loop so it is responsive and blinking the build-in LED as well as starting/stopping the motor and reversing direction.
- updating the motor every time through loop but using global variables since there are several functions involved with the motors
any or all of these techniques can by used
(untested)
#define LIGH_PIN_14 9
#define STEPPER_PIN_1 5
#define STEPPER_PIN_2 6
#define STEPPER_PIN_3 7
#define STEPPER_PIN_4 8
enum { Off = HIGH, On = LOW };
const byte pinsLed [] = { 10, 11, 12 };
const byte N_LED = sizeof(pinsLed) / sizeof(pinsLed[0]);
const byte pinBut = A1;
byte stateBut = HIGH;
bool isMotorRunning = false;
bool motorDir = false;
int motorStepIdx;
const int MAX_MOTOR_COUNT = 3000;
unsigned long currentTime;
/*
Sequence the LEDs every 100 msec
*/
void seqLed (void) {
static int idx = 0;
static unsigned long lastTime;
const unsigned long interval = 100;
if ( currentTime - lastTime >= interval ) {
// time to do next action
lastTime = currentTime;
digitalWrite (pinsLed [idx], Off);
idx = (idx + 1) % N_LED;
digitalWrite (pinsLed [idx], On);
}
}
void startMotor(bool state) {
isMotorRunning = true;
motorStepIdx = 0;
motorDir = state;
if (motorDir ) {
digitalWrite( LIGH_PIN_14, HIGH );
}
else {
digitalWrite( LIGH_PIN_14, HIGH );
}
}
void stopMotor() {
isMotorRunning = false;
}
void updateMotor() {
static unsigned long lastTime;
const unsigned long interval = 5;
if ( currentTime - lastTime >= interval ) {
lastTime = currentTime;
OneStep(motorDir);
motorStepIdx++;
if ( motorStepIdx >= MAX_MOTOR_COUNT ) {
stopMotor();
}
}
}
void OneStep(bool dir) {
static byte step_number;
if (dir)
{
switch (step_number)
{
case 0:
digitalWrite(STEPPER_PIN_1, HIGH);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 1:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, HIGH);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 2:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, HIGH);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 3:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, HIGH);
break;
}
}
else
{
switch (step_number)
{
case 0:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, HIGH);
break;
case 1:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, HIGH);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 2:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, HIGH);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 3:
digitalWrite(STEPPER_PIN_1, HIGH);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
}
}
step_number = (step_number + 1) % 4;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void setup () {
Serial.begin (9600);
pinMode (LED_BUILTIN, OUTPUT);
for (unsigned n = 0; n < N_LED; n++) {
pinMode (pinsLed [n], OUTPUT);
digitalWrite (pinsLed [n], Off);
}
pinMode (pinBut, INPUT_PULLUP);
}
void loop () {
currentTime = millis();
seqLed();
// toggle builtin-led on button press
byte but = digitalRead (pinBut);
if (stateBut != but) {
stateBut = but;
delay (10); // debounce
if (LOW == but) {
Serial.println ("press");
digitalWrite (LED_BUILTIN, ! digitalRead (LED_BUILTIN));
if (isMotorRunning) {
stopMotor();
}
else {
startMotor(!motorDir); // begin in the other direction
}
}
}
updateMotor();
}