Stepper motor in While loop very slow

Hi to all friends here
i wrote a code to drive a small Nema17 motor with a A4988 driver in full step

with my code i have 3 states, one that is the main loop "normal mode" , one that is the "service mode" and a third one that is the "error mode"
My code in normal mode when has a signal in one pin of arduino runs the motor with a speed based in a potentiometer and stops if reaches a terminal switch connected to an other arduino pin
if it has no signal then runs the motor in the opposite direction until reaches an other terminal switch
My code works fine in the "Normal mode" and i can see the speed of the motor changing when i rotate the potentiometer.

if i press one switch then i m going to the service mode

my problem is in the service mode that runs in a while loop . There i can control my motor (direction and stopping it with limit switches as above, but the speed is very slow and cant change it what ever i do
i tried with the same code line that i used in the mail loop to read speed from potentiometer = Nothing
i tried to put new int variable names = Nothing
i tried to place directly new numbers in the code = Nothing

i guess something has to do with the while loop condition and the time-delay

Here is my code :

i had to remove the code because i got an error for to many words in one post... code will follow in the next post

any advice ?

Thanks in advance

const byte Signal = 2;  // sima gia na traviksei i kama tou magniti  .sima mesa apo rele plaketas. No epafi  , 110/48v rele travaei otan parei 110v/48v  apo pinaka
const byte ContactOpen =  5;  //  sima apo pouraki anoigmatos portas se stasi . Sima mesa apo rele pou pernei tasi apo epafi NO pouraki .
const byte ContactClose =  6;
const byte ContactOveride =  4;
const byte ButtonOpen =  A0;
const byte ServiceSwitch =  A1;
const byte ButtonClose =  A2;
const byte SpeedRead = A6 ;


boolean flagOpen = false;
boolean flagClose = false;


// Defines Stepper pins numbers
const int stepPin = 7;
const int dirPin = 8;
const int enablePin = 9;
int customDelay, customDelayMapped; // Define speed variable
int customDelaySpeed = 1000;
int customDelay1 = 0;




void setup() {

  Serial.begin(9600);
  //   INPUTS
  pinMode(Signal, INPUT_PULLUP);
  pinMode(ContactOpen, INPUT_PULLUP);
  pinMode(ContactClose, INPUT_PULLUP);
  pinMode(ContactOveride, INPUT_PULLUP);
  pinMode(ButtonOpen, INPUT_PULLUP);
  pinMode(ServiceSwitch, INPUT_PULLUP);
  pinMode(ButtonClose, INPUT_PULLUP);
  //   OUTPUTS
  // stepper
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  delay(100);
  digitalWrite(enablePin, HIGH); //Enables or disable the motor  HIGH = Disable , LOW = Enable

}


void loop() {

  digitalRead(Signal);
  digitalRead(ContactOpen);
  digitalRead(ContactClose);
  digitalRead(ContactOveride);
  digitalRead(ButtonOpen);
  digitalRead(ServiceSwitch);
  digitalRead(ButtonClose);

  int SignalState = digitalRead(Signal);
  int ContactOpenState = digitalRead(ContactOpen);
  int ContactCloseState = digitalRead(ContactClose);
  int ContactOverideState = digitalRead(ContactOveride);
  int ButtonOpenState = digitalRead(ButtonOpen);
  int ServiceSwitchState = digitalRead(ServiceSwitch);
  int ButtonCloseState = digitalRead(ButtonClose);

  //////////////////// When No Input Signal /////////////////////////
  if (SignalState == HIGH && ContactOpenState == HIGH && ContactOverideState == HIGH) {

    digitalWrite(dirPin, HIGH); //Enables the motor to move in a particular direction
    digitalWrite(enablePin, LOW); //Enables the motor
    customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
    // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(customDelayMapped);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(customDelayMapped);
  }
  ////////// When No Input Signal  and the mechanism is  at open position - open contact tiggered  //////////////
  if (SignalState == HIGH && ContactOpenState == LOW && ContactOverideState == HIGH) {
    digitalWrite(enablePin, HIGH); // Disable the motor
  }
  //////////////////// When Signal input is present  signal /////////////////////////
  if (SignalState == LOW && ContactCloseState == HIGH && ContactOverideState == HIGH) {
    digitalWrite(dirPin, LOW); //Enables the motor to move in a particular direction
    digitalWrite(enablePin, LOW); //Enables or disable the motor
    customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
    // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(customDelayMapped);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(customDelayMapped);
  }
  ///////// When Signal input is present and the mechanism is at close position - close contact tiggered ////////////
  if (SignalState == LOW && ContactCloseState == LOW && ContactOverideState == HIGH) {
    digitalWrite(enablePin, HIGH); // Disable the motor
  }
  //////////////////// When Button Switch is on Service /////////////////////////
  if (ServiceSwitchState == LOW ) {
    Service();
  }
  //////////////////// When Terminal contacts are activated /////////////////////////
  if (ContactOpenState == LOW && ContactCloseState == LOW || ContactOverideState == LOW ) {
    Errors();
  }
}

void Service() {

  int ServiceSwitchState = digitalRead(ServiceSwitch);

  while (ServiceSwitchState == LOW ) {

    int ServiceSwitchState = digitalRead(ServiceSwitch);
    int ButtonCloseState = digitalRead(ButtonClose);
    int ButtonOpenState = digitalRead(ButtonOpen);
    int ContactOpenState = digitalRead(ContactOpen);
    int ContactCloseState = digitalRead(ContactClose);
    int ContactOverideState = digitalRead(ContactOveride);
    int customDelay1 = analogRead(A6); // Reads the potentiometer
    int customDelaySpeed = map(customDelay1, 0, 1023, 300, 4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
    // return newCustom;

    ////////////////     Check terminal contacts     ///////////////////////////////
    if (ContactOverideState == LOW) {
      Errors();
    }
    ///////     Manual closing    ///////////////////////////////

    if (ButtonCloseState == LOW && ContactCloseState == HIGH && ContactOverideState == HIGH ) {
      digitalWrite(dirPin, HIGH); //Enables the motor to move in a particular direction
      digitalWrite(enablePin, LOW); //Enables the motor
      //int customDelay1 = analogRead(A6); // Reads the potentiometer
      //int customDelaySpeed = map(customDelay1, 0, 1023, 300, 4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
      //customDelayMapped = customDelaySpeed; // Gets custom delay values from the custom speedUp function
      // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(300);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(300);
      Serial.println("Closing door");
    }
    else if (ButtonCloseState == LOW && ContactCloseState == LOW && ContactOverideState == HIGH) {
      digitalWrite(enablePin, HIGH); // Disable the motor
      Serial.println("Door Closed");
    }
    ///////     Manual open  ///////////////////////////////
    if (ButtonOpenState == LOW && ContactOpenState == HIGH && ContactOverideState == HIGH ) {
      digitalWrite(dirPin, LOW); //Enables the motor to move in a particular direction
      digitalWrite(enablePin, LOW); //Enables the motor
      //customDelayMapped = customDelaySpeed; // Gets custom delay values from the custom speedUp function
      // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
      //digitalWrite(stepPin, HIGH);
      //delayMicroseconds(customDelayMapped);
      //digitalWrite(stepPin, LOW);
      //delayMicroseconds(customDelayMapped);
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1500);
      Serial.println("opening door");
    }
    ///////     No action     ///////////////////////////////
    else if (ButtonOpenState == LOW && ContactOpenState == LOW && ContactOverideState == HIGH) {
      digitalWrite(enablePin, HIGH); // Disable the motor
      Serial.println("Door Opend");
    }
    if (ButtonOpenState == HIGH && ButtonCloseState == HIGH && ContactOverideState == HIGH) {
      digitalWrite(enablePin, HIGH); // Disable the motor
      Serial.println("No Button");
    }
    Serial.println("Service Mode");
    if (ServiceSwitchState == HIGH ) {
      break;
    }
  }
}

void Errors() {

  int ContactOpenState = digitalRead(ContactOpen);
  int ContactCloseState = digitalRead(ContactClose);
  int ContactOverideState = digitalRead(ContactOveride);
  digitalWrite(enablePin, HIGH);  // Disable the motor
  if (ContactOpenState == LOW && ContactCloseState == LOW) {
    Serial.println("Error");
  }
  if (ContactOverideState == LOW) {
    Serial.println("Overide");
  }
}
// Function for reading the Potentiometer
int speedUp() {
  int customDelay = analogRead(A6); // Reads the potentiometer
  int newCustom = map(customDelay, 0, 1023, 300, 4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
  return newCustom;
}

Take a look at AccelStepper library and my tutorial on Multi-tasking in Arduino that includes a detailed example of driving a stepper while handling other inputs.
Also look at How to write Timers and Delays in Arduino
and Debouncing Switches in Arduino

i have a hard time reading the code

one reason is the numerous times inputs are read using digitalRead(). and there are initial reads that don't capture the value being read

i don't understand the need for open/close inputs (e.g. ContactOpen/Close, ButtonOpen/Close), wouldn't an open or closed switch simply change an input from HIGH to LOW?

the code would be easier to read if the name of the input reflected its purpose (e.g. ServiceSwitch) rather than Button, Contact (e.g. limitRight?)

there are also numerous calls to digitalWrite (), 25 calls affecting 3 pins. 11 to enablePin, 7 of which set it HIGH. it's harder to understand what the code is doing by understanding the behavior resulting from setting pins HIGH/LOW.

there would certainly be less code to read and it would be easier to understand if there were sub-functions to step the motor in a specific direction.

having sub-functions and variables with meaningful names will not only make the code easier for someone else to review, but the author (you) as well.

you say the code has three states. do you actually have a variable reflecting each state or is it the state of the inputs or is it implicit is the states of the inputs?

if it has no signal then runs the motor in the opposite direction until reaches an other terminal switch

what is "signal" and which inputs are the signal and "terminal switch"?

if i press one switch then i m going to the service mode

so which input is "one switch"?

my problem is in the service mode that runs in a while loop . There i can control my motor (direction and stopping it with limit switches as above, but the speed is very slow and cant change it what ever i do

i don't see a call to speedUp() within Service() and it looks like the delay between step pulses is hardcoded to 1500. (customDelayMapped remains whatever it was set to previously.

i see no need for Service (). the code can check for specific inputs in loop() and perform actions based on those input. For example, shouldn't the motor be stopped if a limit switch becomes active no matter which "mode" is active?

  while (ServiceSwitchState == LOW ) {

    int ServiceSwitchState = digitalRead(ServiceSwitch);

You are creating a new local variable named 'ServiceSwitchState' inside your while loop. This is NOT the same variable that the while loop is testing! Remove the "int" keyword to use the same 'ServiceSwitchState' variable both inside and outside the loop.

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