Stepper with 2 blinking leds

Hi.
I´m a noob with code prog.
Can somebody help me coding a green led blinking slowly with the stepper runing forward at slow speed and a blue led blinking quickly with the stepper running backward at high speed?
I know it is super simple....but not for me.
Thanks in advance.

@rog8811 thanks for the project

// constants won't change. They're used here to set pin numbers:
const int ForwardPin = 16;     // the number of the pin that when high sends slide forward
const int StopForwardPin = 18;     // the number of the pin that stops motor when forward
const int BackwardPin = 17;     // the number of the pin that when high sends slide backward
const int StopBackwardPin = 19;  // the number of the pin that stops the motor when backward
int smDirectionPin = 13; //Direction pin
int smStepPin = 27; //Stepper pin

// variables will change:
boolean ForwardState = 0;         // variable for reading the micro-switch status
boolean StopForwardState = 0;     // variable that stops motor when forward
boolean BackwardState = 0;   // variable for reading the microswitch status
boolean StopBackwardState = 0;  //variable that stops the motor when backward

int slidePosition = 2;  // Tells progam what position the slide is in


/**
    setup inputs for switches and outs for motor pins
    serial begin to read the switches to test for errors
*/
void setup() {
  // initialize the pin as an inputs:
  pinMode(smDirectionPin, OUTPUT);
  pinMode(smStepPin, OUTPUT);
  // initialize the switch pins as inputs:
  pinMode(ForwardPin, INPUT);
  pinMode(StopForwardPin, INPUT);
  pinMode(BackwardPin, INPUT);
  pinMode(StopBackwardPin, INPUT);
  Serial.begin(9600);
}

/**
   this function turns motor foward
*/
void SlideForward() {
  // turn motor foward:
  digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
  /*turns the motor 1 step*/
  for (int i = 0; i < 1; i++)
    digitalWrite(smStepPin, HIGH);
    delayMicroseconds(20);
    digitalWrite(smStepPin, LOW);
  delayMicroseconds(8050);
}

/**
   this function turns motor backwards
*/
void SlideBackward() {
  digitalWrite(smDirectionPin, LOW); //Writes the direction to the EasyDriver DIR pin. (LOW is counter clockwise).
  /*Turns the motor fast 1 step*/
  for (int i = 0; i < 1; i++)
    digitalWrite(smStepPin, HIGH);
    delayMicroseconds(400);
    digitalWrite(smStepPin, LOW);
  delayMicroseconds(1000);
}

/**
   Stop the motor form moving
*/
void stopSlideFromMoving() {
  digitalWrite(smStepPin, LOW);
}

void loop() {
  // read the state of the switches value:
  ForwardState = digitalRead(ForwardPin);
  StopForwardState = digitalRead(StopForwardPin);
  BackwardState = digitalRead(BackwardPin);
  StopBackwardState = digitalRead(StopBackwardPin);


  // check which pin 16 or 17 is HIGH:
  if (ForwardState == HIGH && StopForwardState == HIGH && BackwardState == LOW) {
    SlideForward();
  } else if (ForwardState == HIGH && StopForwardState == LOW && BackwardState == LOW) {
    stopSlideFromMoving();
  }

  if (BackwardState == HIGH && StopBackwardState == HIGH && ForwardState == LOW) {
    SlideBackward();
  } else if (BackwardState == HIGH && StopBackwardState == LOW && ForwardState == LOW) {
    stopSlideFromMoving();
  }
}
1 Like

how many iterations do you expect from this ?

you should also check if you need {}

This code is working perfectly with the stepper.....only need to integrate the blinking leds :frowning:

Make a test code just for blinking LEDs. Example code is found in IDE examples. When it works and You understand the code, incorporate it with the other code.

1 Like

For extra examples look at

1 Like

I think the forward blinking led´s code needs to be under that line void SlideForward() ....but without affecting the stepPin cadence.... :face_with_spiral_eyes:

give it a try

delayMicroseconds(8050);
and
delayMicroseconds(1000);
and
delayMicroseconds(20);
and
delayMicroseconds(400);

make that difficult.

1 Like

I don´t think this code can be added to the void line of the stepper....

void loop() {
  digitalWrite(LED, HIGH);
  delay(500);
  digitalWrite(LED, LOW);
  delay(500);
}

Try this one.

// constants won't change. They're used here to set pin numbers:
const int ForwardPin = 16; // the number of the pin that when high sends slide forward
const int StopForwardPin = 18; // the number of the pin that stops motor when forward
const int BackwardPin = 17; // the number of the pin that when high sends slide backward
const int StopBackwardPin = 19; // the number of the pin that stops the motor when backward
int smDirectionPin = 13; //Direction pin
int smStepPin = 27; //Stepper pin

// variables will change:
boolean ForwardState = 0; // variable for reading the micro-switch status
boolean StopForwardState = 0; // variable that stops motor when forward
boolean BackwardState = 0; // variable for reading the microswitch status
boolean StopBackwardState = 0; //variable that stops the motor when backward

int slidePosition = 2; // Tells progam what position the slide is in

const int greenLedPin = 11; // Green LED for slow blinking
const int blueLedPin = 12; // Blue LED for quick blinking

/**
setup inputs for switches and outs for motor pins
serial begin to read the switches to test for errors
*/
void setup() {
// initialize the pins as an outputs:
pinMode(smDirectionPin, OUTPUT);
pinMode(smStepPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(blueLedPin, OUTPUT);
// initialize the switch pins as inputs:
pinMode(ForwardPin, INPUT);
pinMode(StopForwardPin, INPUT);
pinMode(BackwardPin, INPUT);
pinMode(StopBackwardPin, INPUT);

Serial.begin(9600);
}

/**
this function turns motor foward
*/
void SlideForward() {
// turn motor foward:
digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
//turns the motor 1 step
//for (int i = 0; i < 1; i++) //Commented out because have no effect on the behavior of this function
digitalWrite(smStepPin, HIGH);
delayMicroseconds(20);
digitalWrite(smStepPin, LOW);
delayMicroseconds(8050);
blinkSlowly();
}

/**
this function turns motor backwards
*/
void SlideBackward() {
digitalWrite(smDirectionPin, LOW); //Writes the direction to the EasyDriver DIR pin. (LOW is counter clockwise).
//Turns the motor fast 1 step
//for (int i = 0; i < 1; i++) //Commented out because have no effect on the behavior of this function
digitalWrite(smStepPin, HIGH);
delayMicroseconds(400);
digitalWrite(smStepPin, LOW);
delayMicroseconds(1000);
blinkQuickly();
}

/**
Stop the motor form moving
*/
void stopSlideFromMoving() {
digitalWrite(smStepPin, LOW);
}

/**
This function blings slowly the green LED
*/
void blinkSlowly() {
static int forwardBlinkCounter = 0; // Counter for the number of delayMicroseconds
forwardBlinkCounter++; // Increment the counter every time the function is called
if (forwardBlinkCounter >= 250) {
static bool ledState = LOW; // Toggle state variable
ledState = !ledState; // Toggle the LED state
digitalWrite(greenLedPin, ledState); // Apply the new state to green LED
forwardBlinkCounter = 0; // Reset the counter
}
}

/**
This function blings quickly the blue LED
*/
void blinkQuickly() {
static int backwardBlinkCounter = 0; // Counter for the number of delayMicroseconds
backwardBlinkCounter++; // Increment the counter every time the function is called
if (backwardBlinkCounter >= 700) {
static bool ledState = LOW; // Toggle state variable
ledState = !ledState; // Toggle the LED state
digitalWrite(blueLedPin, ledState); // Apply the new state to blue LED
backwardBlinkCounter = 0; // Reset the counter
}
}

void loop() {
// read the state of the switches value:
ForwardState = digitalRead(ForwardPin);
StopForwardState = digitalRead(StopForwardPin);
BackwardState = digitalRead(BackwardPin);
StopBackwardState = digitalRead(StopBackwardPin);

// check which pin 16 or 17 is HIGH:
if (ForwardState == HIGH && StopForwardState == HIGH && BackwardState == LOW) {
SlideForward();
} else if (ForwardState == HIGH && StopForwardState == LOW && BackwardState == LOW) {
stopSlideFromMoving();
}

if (BackwardState == HIGH && StopBackwardState == HIGH && ForwardState == LOW) {
SlideBackward();
} else if (BackwardState == HIGH && StopBackwardState == LOW && ForwardState == LOW) {
stopSlideFromMoving();
}
}
1 Like

Hi, @bitslaver
Please edit your code with code tags.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

Thank you.....it works at the first time but when i stop the stepper by the 3 position switch the leds remains on.... :frowning:

When you stop the stepper, turn off the LEDs.

a7

The leds should operate at same time of the stepper

start by writing a code that has no delay (see the examples using millis, use micros() instead if your timing is shorter)

you might benefit also from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

2 Likes

Of course, the advice in post #15 is excellent, and I strongly recommend following it. However, if you're not ready or prefer to continue playing with the current, somewhat restrictive beginner code, you might want to try a solution like this:

// constants won't change. They're used here to set pin numbers:
const int ForwardPin = 16;       // the number of the pin that when high sends slide forward
const int StopForwardPin = 18;   // the number of the pin that stops motor when forward
const int BackwardPin = 17;      // the number of the pin that when high sends slide backward
const int StopBackwardPin = 19;  // the number of the pin that stops the motor when backward
int smDirectionPin = 13;         //Direction pin
int smStepPin = 27;              //Stepper pin

// variables will change:
boolean ForwardState = 0;       // variable for reading the micro-switch status
boolean StopForwardState = 0;   // variable that stops motor when forward
boolean BackwardState = 0;      // variable for reading the microswitch status
boolean StopBackwardState = 0;  //variable that stops the motor when backward

int slidePosition = 2;  // Tells progam what position the slide is in

const int greenLedPin = 11;  // Green LED for slow blinking
const int blueLedPin = 12;   // Blue LED for quick blinking

boolean movesNotAllowed = false; // Indicates if moves are restricted (true) or allowed (false)

/**
setup inputs for switches and outs for motor pins
serial begin to read the switches to test for errors
*/
void setup() {
  // initialize the pins as an outputs:
  pinMode(smDirectionPin, OUTPUT);
  pinMode(smStepPin, OUTPUT);
  pinMode(greenLedPin, OUTPUT);
  pinMode(blueLedPin, OUTPUT);
  // initialize the switch pins as inputs:
  pinMode(ForwardPin, INPUT);
  pinMode(StopForwardPin, INPUT);
  pinMode(BackwardPin, INPUT);
  pinMode(StopBackwardPin, INPUT);

  Serial.begin(9600);
}

/**
this function turns motor foward
*/
void SlideForward() {
  // turn motor foward:
  digitalWrite(smDirectionPin, HIGH);  //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
  //turns the motor 1 step
  //for (int i = 0; i < 1; i++) //Commented out because have no effect on the behavior of this function
  digitalWrite(smStepPin, HIGH);
  delayMicroseconds(20);
  digitalWrite(smStepPin, LOW);
  delayMicroseconds(8050);
  blinkSlowly();
}

/**
this function turns motor backwards
*/
void SlideBackward() {
  digitalWrite(smDirectionPin, LOW);  //Writes the direction to the EasyDriver DIR pin. (LOW is counter clockwise).
  //Turns the motor fast 1 step
  //for (int i = 0; i < 1; i++) //Commented out because have no effect on the behavior of this function
  digitalWrite(smStepPin, HIGH);
  delayMicroseconds(400);
  digitalWrite(smStepPin, LOW);
  delayMicroseconds(1000);
  blinkQuickly();
}

/**
Stop the motor form moving
*/
void stopSlideFromMoving() {
 if (StopForwardState == HIGH || StopBackwardState == HIGH) {
movesNotAllowed = false;
 } else {
  movesNotAllowed = true;
  digitalWrite(greenLedPin, LOW); // Ensure the green LED is turned off if it is currently on
  digitalWrite(blueLedPin, LOW); // Ensure the blue LED is turned off if it is currently on
 }
  
}

/**
This function blings slowly the green LED
*/
void blinkSlowly() {
  if (movesNotAllowed == false) {
    static int forwardBlinkCounter = 0;  // Counter for the number of delayMicroseconds
    forwardBlinkCounter++;               // Increment the counter every time the function is called
    if (forwardBlinkCounter >= 250) {
      static bool ledState = LOW;           // Toggle state variable
      ledState = !ledState;                 // Toggle the LED state
      digitalWrite(greenLedPin, ledState);  // Apply the new state to green LED
      forwardBlinkCounter = 0;              // Reset the counter
    }
  }
}

/**
This function blings quickly the blue LED
*/
void blinkQuickly() {
  if (movesNotAllowed == false) {
    static int backwardBlinkCounter = 0;  // Counter for the number of delayMicroseconds
    backwardBlinkCounter++;               // Increment the counter every time the function is called
    if (backwardBlinkCounter >= 700) {
      static bool ledState = LOW;          // Toggle state variable
      ledState = !ledState;                // Toggle the LED state
      digitalWrite(blueLedPin, ledState);  // Apply the new state to blue LED
      backwardBlinkCounter = 0;            // Reset the counter
    }
  }
}

void loop() {
  // read the state of the switches value:
  ForwardState = digitalRead(ForwardPin);
  StopForwardState = digitalRead(StopForwardPin);
  BackwardState = digitalRead(BackwardPin);
  StopBackwardState = digitalRead(StopBackwardPin);

  // check which pin 16 or 17 is HIGH:
  if (ForwardState == HIGH && StopForwardState == HIGH && BackwardState == LOW) {
    SlideForward();
  } else if (ForwardState == HIGH && StopForwardState == LOW && BackwardState == LOW) {
    stopSlideFromMoving();
  }

  if (BackwardState == HIGH && StopBackwardState == HIGH && ForwardState == LOW) {
    SlideBackward();
  } else if (BackwardState == HIGH && StopBackwardState == LOW && ForwardState == LOW) {
    stopSlideFromMoving();
  }
}

Additional suggestion: Use bool instead of boolean, as recommended by the Arduino Reference page.

1 Like

Thank you so much @bitslaver ....in fact it works but if i switch off the stepper rotation (cw or ccw) at the precise moment the respective led is on it remains always on ...even when i invert rotation direction.... :frowning:

if (movesNotAllowed == false) will this condition not turns the leds off when the stepper is not moving??

I've made some changes, but I'm not certain they will work as expected

// constants won't change. They're used here to set pin numbers:
const int ForwardPin = 16;       // the number of the pin that when high sends slide forward
const int StopForwardPin = 18;   // the number of the pin that stops motor when forward
const int BackwardPin = 17;      // the number of the pin that when high sends slide backward
const int StopBackwardPin = 19;  // the number of the pin that stops the motor when backward
int smDirectionPin = 13;         //Direction pin
int smStepPin = 27;              //Stepper pin

// variables will change:
boolean ForwardState = 0;       // variable for reading the micro-switch status
boolean StopForwardState = 0;   // variable that stops motor when forward
boolean BackwardState = 0;      // variable for reading the microswitch status
boolean StopBackwardState = 0;  //variable that stops the motor when backward

int slidePosition = 2;  // Tells progam what position the slide is in

const int greenLedPin = 11;  // Green LED for slow blinking
const int blueLedPin = 12;   // Blue LED for quick blinking

boolean blinkingOfLedsAllowed = true;  // Indicates if blinki of LEDs are restricted (false) or allowed (true)

/**
setup inputs for switches and outs for motor pins
serial begin to read the switches to test for errors
*/
void setup() {
  // initialize the pins as an outputs:
  pinMode(smDirectionPin, OUTPUT);
  pinMode(smStepPin, OUTPUT);
  pinMode(greenLedPin, OUTPUT);
  pinMode(blueLedPin, OUTPUT);
  // initialize the switch pins as inputs:
  pinMode(ForwardPin, INPUT);
  pinMode(StopForwardPin, INPUT);
  pinMode(BackwardPin, INPUT);
  pinMode(StopBackwardPin, INPUT);

  Serial.begin(9600);
}

/**
this function turns motor foward
*/
void SlideForward() {
  // turn motor foward:
  digitalWrite(smDirectionPin, HIGH);  //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
  //turns the motor 1 step
  //for (int i = 0; i < 1; i++) //Commented out because have no effect on the behavior of this function
  digitalWrite(smStepPin, HIGH);
  delayMicroseconds(20);
  digitalWrite(smStepPin, LOW);
  delayMicroseconds(8050);
  blinkSlowly();
}

/**
this function turns motor backwards
*/
void SlideBackward() {
  digitalWrite(smDirectionPin, LOW);  //Writes the direction to the EasyDriver DIR pin. (LOW is counter clockwise).
  //Turns the motor fast 1 step
  //for (int i = 0; i < 1; i++) //Commented out because have no effect on the behavior of this function
  digitalWrite(smStepPin, HIGH);
  delayMicroseconds(400);
  digitalWrite(smStepPin, LOW);
  delayMicroseconds(1000);
  blinkQuickly();
}

/**
Stop the motor form moving
*/
void stopSlideFromMoving() {
  if (StopForwardState == HIGH || StopBackwardState == HIGH) { // If the stop states are in a default state
    blinkingOfLedsAllowed = true; // The blinking of LEDs allowed
  } else {
    ensureLEDsAreOff();
  }
}

/**
This function blings slowly the green LED
*/
void blinkSlowly() {
  if (blinkingOfLedsAllowed == true) {
    static int forwardBlinkCounter = 0;  // Counter for the number of delayMicroseconds
    forwardBlinkCounter++;               // Increment the counter every time the function is called
    if (forwardBlinkCounter >= 250) {
      static bool ledState = LOW;           // Toggle state variable
      ledState = !ledState;                 // Toggle the LED state
      digitalWrite(greenLedPin, ledState);  // Apply the new state to green LED
      forwardBlinkCounter = 0;              // Reset the counter
    }
  }
}

/**
This function blings quickly the blue LED
*/
void blinkQuickly() {
  if (blinkingOfLedsAllowed == true) {
    static int backwardBlinkCounter = 0;  // Counter for the number of delayMicroseconds
    backwardBlinkCounter++;               // Increment the counter every time the function is called
    if (backwardBlinkCounter >= 700) {
      static bool ledState = LOW;          // Toggle state variable
      ledState = !ledState;                // Toggle the LED state
      digitalWrite(blueLedPin, ledState);  // Apply the new state to blue LED
      backwardBlinkCounter = 0;            // Reset the counter
    }
  }
}

/**
 * This function ensures LEDs are turned off wherever needed.
 */
void ensureLEDsAreOff() {
  blinkingOfLedsAllowed = false;   // The blinking of LEDs is not allowed
  digitalWrite(greenLedPin, LOW);  // Ensure the green LED is turned off if it is currently on
  digitalWrite(blueLedPin, LOW);   // Ensure the blue LED is turned off if it is currently on
}

void loop() {
  // read the state of the switches value:
  ForwardState = digitalRead(ForwardPin);
  StopForwardState = digitalRead(StopForwardPin);
  BackwardState = digitalRead(BackwardPin);
  StopBackwardState = digitalRead(StopBackwardPin);

  // check which pin 16 or 17 is HIGH:
  if (ForwardState == HIGH && StopForwardState == HIGH && BackwardState == LOW) {  // If forward input case is active
    SlideForward();
  } else if (ForwardState == LOW && StopForwardState == HIGH && BackwardState == LOW) {  // If all three checking inputs are in default state
    ensureLEDsAreOff();
  } else if (ForwardState == HIGH && StopForwardState == LOW && BackwardState == LOW) {  // If stop forward case is active
    stopSlideFromMoving();
  }

  if (BackwardState == HIGH && StopBackwardState == HIGH && ForwardState == LOW) {  // If backward input case is active
    SlideBackward();
  } else if (BackwardState == LOW && StopBackwardState == HIGH && ForwardState == LOW) {  // If all three checking inputs are in default state
    ensureLEDsAreOff();
  } else if (BackwardState == HIGH && StopBackwardState == LOW && ForwardState == LOW) {  // If we have stop backward case // If stop backward case is active
    stopSlideFromMoving();
  }
}

Do you have the Driver? Must do watch The Engineering Mindset