Stepper motor auto running and stopping when hits end switch

Hello,

I'm totaly new in arduino programming, I got code for stepper motor moving with two buttons cw and ccw. I want to embed code for 3rd switch to run stepper until 4th switch is activated ... but no luck for now. I need some help please.

Code:

// testing a stepper motor with a Pololu A4988 driver board or equivalent

// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW

byte directionPin = 4;
byte stepPin = 3;

byte buttonLevel = 8;
byte endSwitch = 9;

byte buttonCWpin = 10;
byte buttonCCWpin = 11;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
boolean buttonLevelpressed = false;
boolean endSwitchpressed = false;

byte ledPin = 13;

unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 1; // milliseconds

void setup() { 

     Serial.begin(9600);
     Serial.println("Starting Stepper Demo with millis()");

     pinMode(directionPin, OUTPUT);
     pinMode(stepPin, OUTPUT);
     pinMode(ledPin, OUTPUT);
     
     pinMode(buttonCWpin, INPUT_PULLUP);
     pinMode(buttonCCWpin, INPUT_PULLUP);
     pinMode(buttonLevel, INPUT_PULLUP);
     pinMode(endSwitch, INPUT_PULLUP);
     
}

void loop() { 
    
    curMillis = millis();
    readButtons();
    actOnButtons();
    
}

void readButtons() {
    
    buttonCCWpressed = false;
    buttonCWpressed = false;
    buttonLevelpressed = false;
    endSwitchpressed = true;
    
    if (digitalRead(buttonCWpin) == LOW) {
        buttonCWpressed = true;
    }
    if (digitalRead(buttonCCWpin) == LOW) {
        buttonCCWpressed = true;
    }
        if (digitalRead(buttonLevel) == LOW) {
        buttonLevelpressed = true;
    }
}

void actOnButtons() {
    if (buttonCWpressed == true) {
        digitalWrite(directionPin, LOW);
        singleStep();
    }
    if (buttonCCWpressed == true) {
        digitalWrite(directionPin, HIGH);
        singleStep();
    }
        if (buttonLevelpressed == true) {
        digitalWrite(directionPin, LOW);
        multiStep();
    }
}

void singleStep() {
    if (curMillis - prevStepMillis >= millisBetweenSteps) {
            // next 2 lines changed 28 Nov 2018
        //prevStepMillis += millisBetweenSteps;
        prevStepMillis = curMillis;
        digitalWrite(stepPin, HIGH);
        digitalWrite(stepPin, LOW);
    }
}


void multiStep() {

for (int i = 0; i < 200000; i++) {
    if (curMillis - prevStepMillis >= millisBetweenSteps) {
            // next 2 lines changed 28 Nov 2018
        //prevStepMillis += millisBetweenSteps;
        prevStepMillis = curMillis;
        digitalWrite(stepPin, HIGH);
        digitalWrite(stepPin, LOW);
    }
    if (digitalRead(endSwitch) == false) {
      break;
    }
}
}

When I press "buttonLevel" nothing happens - this button is meant to run stepper until "endStop" button gets hit.

Managed to get it working.

// testing a stepper motor with a Pololu A4988 driver board or equivalent

// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW

byte directionPin = 4;
byte stepPin = 3;

byte buttonLevel = 8;
byte endSwitch = 9;

byte buttonCWpin = 10;
byte buttonCCWpin = 11;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
boolean buttonLevelpressed = false;
boolean endSwitchpressed = false;

byte ledPin = 13;

unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 1; // milliseconds

void setup() {

  Serial.begin(9600);
  Serial.println("Starting Stepper Demo with millis()");

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);

  pinMode(buttonCWpin, INPUT_PULLUP);
  pinMode(buttonCCWpin, INPUT_PULLUP);
  pinMode(buttonLevel, INPUT_PULLUP);
  pinMode(endSwitch, INPUT_PULLUP);

}

void loop() {

  curMillis = millis();
  readButtons();
  actOnButtons();


}

void readButtons() {

  buttonCCWpressed = false;
  buttonCWpressed = false;
  buttonLevelpressed = false;
  endSwitchpressed = false;

  if (digitalRead(buttonCWpin) == LOW) {
    buttonCWpressed = true;
  }
  if (digitalRead(buttonCCWpin) == LOW) {
    buttonCCWpressed = true;
  }
  if (digitalRead(buttonLevel) == LOW) {
    buttonLevelpressed = true;
  }
  if (digitalRead(endSwitch) == LOW) {
    endSwitchpressed = true;
  }
}

void actOnButtons() {
  if (buttonCWpressed == true) {
    digitalWrite(directionPin, LOW);
    singleStep();
  }
  if (buttonCCWpressed == true) {
    digitalWrite(directionPin, HIGH);
    singleStep();
  }
  if (buttonLevelpressed == true) {
    digitalWrite(directionPin, LOW);
    multiStep();
  }
  if (endSwitchpressed == true) {
    exit(0);
  }
}

void singleStep() {
  if (curMillis - prevStepMillis >= millisBetweenSteps) {
    prevStepMillis = curMillis;
    digitalWrite(stepPin, HIGH);
    digitalWrite(stepPin, LOW);
  }
}

void multiStep() {
int start1 = 0;
int stop2 = 2000;

while (start1 <= stop2){
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(1000);
  if (digitalRead(endSwitch) == false) {
    exit(0);
  }
}

}

exit doesn't mean much on an Arduino - you might want to go back to using break as you were before.

or return;

You could pass a value back to the caller (return x;), and let it handle the end of program.

I repaired code and added commands that when stop switch is on, motor runs some small amount backwards. I use this on home made laser engraver to have some kind of auto-focus add-on (always same distance from laser head and material, no matter what thickness of material is).

New code:

// testing a stepper motor with a Pololu A4988 driver board or equivalent

// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW

byte directionPin = 4;
byte stepPin = 3;

byte buttonLevel = 8;
byte endSwitch = 9;

byte buttonCWpin = 10;
byte buttonCCWpin = 11;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
boolean buttonLevelpressed = false;
boolean endSwitchpressed = false;

byte ledPin = 13;

unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 1; // milliseconds

void setup() {

  Serial.begin(9600);
  Serial.println("Starting Stepper Demo with millis()");

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);

  pinMode(buttonCWpin, INPUT_PULLUP);
  pinMode(buttonCCWpin, INPUT_PULLUP);
  pinMode(buttonLevel, INPUT_PULLUP);
  pinMode(endSwitch, INPUT_PULLUP);

}

void loop() {

  curMillis = millis();
  readButtons();
  actOnButtons();


}

void readButtons() {

  buttonCCWpressed = false;
  buttonCWpressed = false;
  buttonLevelpressed = false;
  endSwitchpressed = false;

  if (digitalRead(buttonCWpin) == LOW) {
    buttonCWpressed = true;
  }
  if (digitalRead(buttonCCWpin) == LOW) {
    buttonCCWpressed = true;
  }
  if (digitalRead(buttonLevel) == LOW) {
    buttonLevelpressed = true;
  }
  if (digitalRead(endSwitch) == LOW) {
    endSwitchpressed = true;
  }
}

void actOnButtons() {
  if (buttonCWpressed == true) {
    digitalWrite(directionPin, LOW);
    singleStep();
  }
  if (buttonCCWpressed == true) {
    digitalWrite(directionPin, HIGH);
    singleStep();
  }
  if (buttonLevelpressed == true) {
    digitalWrite(directionPin, LOW);
    multiStep();
  }
  if (endSwitchpressed == true) {
  }
}

void singleStep() {
  if (curMillis - prevStepMillis >= millisBetweenSteps) {
    prevStepMillis = curMillis;
    digitalWrite(stepPin, HIGH);
    digitalWrite(stepPin, LOW);
  }
}

void multiStep() {
  while (digitalRead(endSwitch) == HIGH) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
  }
  delay(200);
  backStep();
}

void backStep() {
  digitalWrite(directionPin, HIGH);
  for (int i=0; i<1000; i++) {
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
}
}
void multiStep() {
  while (digitalRead(endSwitch) == HIGH) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
  }
  delay(200);
  backStep();
}

I'm not smart enough to figure out how to add one more condition to this code. I want for "while" to stop if endSwitch OR buttonLevel are pressed. I tried like this:

while ((digitalRead(endSwitch) == HIGH)  &&  (digitalRead(buttonLevel) == HIGH))

But it just skips to "backStep()" instantly

If I understood right "&&" operator: it should run until one of buttons gets pushed?

Thanks.