Stepper with 2 blinking leds

@bitslaver Unfortunately it does the same....seems it keeps the last led´s state.....if i switch off the stepper when the led is on then it stays on....if off it stays off :frowning:

If your input pins are connected to mechanical switches, it's essential to implement a hardware or software debounce solution to ensure stable and reliable readings.

1 Like

Can this lines be moved to the void loop?

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

Yes, it is possible, But you will need to add some additional checks and possibly modify other parts of the code.

I noticed some repetitive checks in the 'void loop' that could be related to the problem, so I removed them. Additionally, I modified the 'stopSlideFromMoving' function. Try it out and see how it performs. Wishing you success!

/**
Stop the motor from moving and turn OFF the LEDS
*/
void stopSlideFromMoving() {
  smStepPin = LOW;
  ensureLEDsAreOff();
}


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 && StopBackwardState == HIGH && BackwardState == HIGH) {  // If backward input case is active
    SlideBackward();
  } else if (ForwardState == LOW && StopBackwardState == LOW && BackwardState == HIGH) {  // If we have stop case is active
    stopSlideFromMoving();
  } else if ((ForwardState == LOW && StopForwardState == HIGH && BackwardState == LOW)
             || (ForwardState == LOW && StopBackwardState == HIGH && BackwardState == LOW)) {  // If all three checking inputs are in default state
    ensureLEDsAreOff();
  }
}
1 Like

In fact i do not understand this led´s issues....looking at the code that @bitslaver kindly compiled for me,it seems to have all to work well but I don't understand why the LEDs stay on when I turn off the stepper´s switch if at that moment they are on !! ....everything else is perfect .

I need to take a closer look at my hardware.....i think i'm using active low inputs and maybe that's the problem as i can see at the code :pleading_face::pensive:

Everything now works like a charm.....there was some hardware issue that i had repair....
I would like to thanks to all your helps especially to @bitslaver for the code.

It's great to hear that! However, with some additional study and understanding of how to program without relying on the delay() or delayMicroseconds() functions, you could develop code that is both more efficient and easier to modify.

As I was testing your code, I wrote a function named simulateSwitchViaSerialCommand that allows the use of a PC keyboard instead of mechanical ON-OFF switches. I made some changes to the entire code using this function, and I've posted the complete updated version below if you're interested in seeing it.

// 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 = LOW;        // variable for reading the micro-switch status
boolean StopForwardState = HIGH;   // variable that stops motor when forward
boolean BackwardState = LOW;       // variable for reading the microswitch status
boolean StopBackwardState = HIGH;  //variable that stops the motor when backward

//int slidePosition = 2;  // Tells progam what position the slide is in // Commented out because have no usage in this code

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 from moving and turn OFF the LEDS
*/
void stopSlideFromMoving() {
  ForwardState = LOW;
  BackwardState = LOW;
  smStepPin = LOW;
  ensureLEDsAreOff();
}

/**
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
    Serial.println("blink Slowly");       // Debug print // Add Time stamp on the serial monitor is suggested
  }
}

/**
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
    Serial.println("blink kquickly");    // Debug print // Add Time stamp on the serial monitor is suggested
  }
}

/**
 * This function ensures LEDs are turned off wherever needed.
 */
void ensureLEDsAreOff() {
  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 simulates the action of toggling switches using serial commands.
 * It allows turning on and off forward and backward slides and enables or disables them.
 *
 * Command Mapping:
 * 'F' or 'f': Turn ON the Forward Slide.
 * 'G' or 'g': Turn OFF the Forward Slide.
 * 'B' or 'b': Turn ON the Backward Slide.
 * 'N' or 'n': Turn OFF the Backward Slide.
 * 'S' or 's': Stop both slides (make unavailable for use).
 * 'D' or 'd': Make both slides available for use (after a stop).
 */
void simulateSwitchViaSerialCommand() {
  if (Serial.available() > 0) {
    char command = Serial.read();  // Read incoming byte from the serial buffer.

    // Turn ON the forward slide by setting the ForwardState to HIGH.
    if (command == 'F' || command == 'f') {
      ForwardState = HIGH;  // Simulate pressing the forward switch to ON position.
      Serial.println("forward slide ON");   // Debug print
    }
    // Turn OFF the forward slide by setting the ForwardState to LOW.
    else if (command == 'G' || command == 'g') {
      ForwardState = LOW;   // Simulate pressing the forward switch to OFF position.
      stopSlideFromMoving();
      Serial.println("forward slide OFF");  // Debug print
    }
    // Turn ON the backward slide by setting the BackwardState to HIGH.
    else if (command == 'B' || command == 'b') {
      BackwardState = HIGH;  // Simulate pressing the backward switch to ON position.
      Serial.println("backward slide ON");   // Debug print
    }
    // Turn OFF the backward slide by setting the BackwardState to LOW.
    else if (command == 'N' || command == 'n') {
      BackwardState = LOW;   // Simulate pressing the backward switch to OFF position.
      stopSlideFromMoving();
      Serial.println("backward slide OFF");  // Debug print
    }
    // Stop both slides by setting both StopForwardState and StopBackwardState to LOW.
    else if (command == 'S' || command == 's') {
      StopForwardState = LOW;
      StopBackwardState = LOW;  // Simulate pressing both stop switches to ON position.
      Serial.println("Both forward and backward movements are now unavailable");   // Debug print
    }
    // Make both slides available by setting both StopForwardState and StopBackwardState to HIGH.
    else if (command == 'D' || command == 'd') {
      StopForwardState = HIGH;
      StopBackwardState = HIGH;  // Simulate releasing both stop switches to OFF position.
      Serial.println("Both forward and backward movements are now available");   // Debug print
    }
  }
}


void loop() {

    // read the state of the switches value:           
  //ForwardState = digitalRead(ForwardPin);            // Commented out for testing purposes
  //StopForwardState = digitalRead(StopForwardPin);    // Commented out for testing purposes
  //BackwardState = digitalRead(BackwardPin);          // Commented out for testing purposes
  //StopBackwardState = digitalRead(StopBackwardPin);  // Commented out for testing purposes

  simulateSwitchViaSerialCommand();

  // 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 && StopBackwardState == HIGH && BackwardState == HIGH) {  // If backward input case is active
    SlideBackward();
  } else if (StopForwardState == LOW || StopBackwardState == LOW) {  // If stop case is active
    stopSlideFromMoving();
  } else if (ForwardState == HIGH && BackwardState == HIGH) {        // If the user changes the switch to a conflicting combination.
    stopSlideFromMoving();  
    Serial.println("The switches were set to a conflicting combination, which caused all movements to halt.");  
  }
}

What were they?
As we have no schematic, and a complete solution would help end this thread.

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.