Step motor stop with micro-switch

Hello guys, Im working on new project with step motor controlled with a4899 and arduino uno, using separate 12V PSU for driver motor.

What Im trying to archive is motor movement until it hits micro-switch ( not in the fixed position so cant do it with number of steps or time measurement ).

Got it moving and to stop when it hits switch but when switch is relised motor moves again ( I need it to move only till it hits switch first time ).

Any advice ?

//not my code, made it combining code from arcuino.cc from users Robin2 & case ( Thanks ) with few modifications//

byte directionPin = 2;
byte stepPin = 3;
byte button = 7;

boolean buttonpressed = false;

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

void setup() { 

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

     pinMode(directionPin, OUTPUT);
     pinMode(stepPin, OUTPUT);
    
     pinMode(button, INPUT_PULLUP);
    
}

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

void readButtons() {
    
     buttonpressed = false;
    
    if (digitalRead(button) == LOW) {
        buttonpressed = true;
   
    }
}

void actOnButtons() {
    if (buttonpressed == false) {
       digitalWrite(directionPin, LOW);
      singleStep();
     
       }
}     


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

Dont know if it matters same arduino will be used for controlling relay and servo motor, operations will be:

  1. stepper until it hits switch
  2. servo and relay control at the same time ( that code works fine, I can post it if it is needed ).

You wrote: "Got it moving and to stop when it hits switch but when switch is relised motor moves again ( I need it to move only till it hits switch first time ).".

When the button is pushed, you stop, but immediately set the button back to not-pushed. Perhaps you need to consider a second push of the button to start again.

Paul

StateChangeDetection example sketch will be worth studying for this...

Thanks guys for the answers, managed to control LED with switch( 1 click on , 1 click off ) which could work for this application.

Which pin should I control on A4988, Im trying with enable pin but cant get motor moving from the start.

//not my code, made it combining code from arcuino.cc from users Robin2 & case ( Thanks ) with few modifications//

byte directionPin = 2;
byte stepPin = 3;
byte button = 7;
byte enable = 12;

boolean buttonpressed = false;

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

void setup() { 

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

    pinMode(directionPin, OUTPUT);
    pinMode(stepPin, OUTPUT);
    pinMode(enable, OUTPUT); 
   
    pinMode(button, INPUT_PULLUP);

   
}

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


void readButtons() {
   
    buttonpressed = false;
   
   if (digitalRead(button) == LOW) {
       buttonpressed = true;
  
   }
}

void actOnButtons() {
   if (buttonpressed == false) {

 
      digitalWrite(enable, HIGH);
     singleStep();
    
      }
}     


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