Prox switch delay

I have a tb660 driving a stepper motor, I use a proximity switch to turn the motor on and off using an arduino uno . The prox switch is normally open. What I want to happen is

  1. As soon as the sensor wire on the prox switch goes high, turn on the motor.
  2. When the prox switch sensor wire goes low then I want the motor to continue running for about a 1/2 second.

This is the code I am using now to run the motor and the prox switch

Stepper Motor Demonstration 4
Stepper-Demo4.ino
Demonstrates NEMA 17 Bipolar Stepper with A4988 Driver

DroneBot Workshop 2018
https://dronebotworkshop.com
*/

// Define Constants

// Connections to A4988
const int dirPin = 6; // Direction
const int stepPin = 7; // Step
const int buttonPin = 2;
int buttonState = 0;

// Motor steps per rotation
const int STEPS_PER_REV = 200;

void setup() {

// Setup the pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {

// Set motor direction clockwise
digitalWrite(dirPin,HIGH);

// Read state of buttonPin
buttonState = digitalRead(buttonPin);

while (buttonState == LOW) {

digitalWrite(stepPin,HIGH);
delayMicroseconds(550);
digitalWrite(stepPin,LOW);
delayMicroseconds(550);
buttonState = digitalRead(buttonPin);

}

}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the </> icon above the compose window) to make it easier to read and copy for examination

consider following which uses an LED to simulate your stepper motor

const byte PinBut = A1;
const byte PinLed = LED_BUILTIN;

byte butState;

unsigned long msec;
unsigned long msecLst;
unsigned long MsecPeriod = 500;

enum { Idle, Run, Delay };
int state = Idle;

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void doSomething   (void) {
    digitalWrite (PinLed, On);
}

void stopSomething (void) {
    digitalWrite (PinLed, Off);
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    msec = millis ();

    switch (state)  {
    case Run:
         doSomething ();
         break;

    case Delay:
        if ( msec - msecLst > MsecPeriod) {
            stopSomething ();
            state = Idle;
        }
        else
            doSomething ();
        break;
    }

    byte but = digitalRead (PinBut);
    if (butState != but)  {
        butState = but;

        if (LOW == but)
            state = Run;
        else  {
            msecLst = msec;
            state   = Delay;
        }
        
    }
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);

    pinMode (PinLed, OUTPUT);
    digitalWrite (PinLed, Off);

    pinMode (PinBut, INPUT_PULLUP);
    butState = digitalRead (PinBut);
}
  static bool buttonWasPressed = false;

  while (buttonState == LOW) {
    buttonWasPressed = true;
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(550);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(550);
    buttonState = digitalRead(buttonPin);
  }

  if (buttonWasPressed)
  {
    // Half-second after-run
    buttonWasPressed = false;
    unsigned long startTime = millis();
    while (millis() - startTime <= 500)
    {
      digitalWrite(stepPin,HIGH);
      delayMicroseconds(550);
      digitalWrite(stepPin,LOW);
      delayMicroseconds(550);
    }
  }