remotely stopping a loop

Hi, after much help from some members on here and a lot of trial and error I have my code working as I want it but as always enough is never enough so there is one last function I want to add but can't get it to work.

So an overview of what it does.

I have several functions that are called using either a android app or a variety of push sequences on 2 push buttons. These functions control 2 stepper motors.I have 1 other button that when pressed stops the motor and rotates it in the other direction slightly(basicallya limit switch) .

All works fine, I sure its not the best written or most compact code ever but it works and I understand it.

My problem lies when using the app. I can call all the functions as required but I can't stop them mid way through like i can with the limit switch.
I want to be able to press a button on the app and instantly stop the motors.

the main parts of the code are as follows.

OneButton button1(4, true);
OneButton button2(5, true);

int turnMotor = 9;
int slideMotor = 10;
int motorPin1 = 14;
int motorPin2 = 12;
int motorPin3 = 13;
int motorPin4 = 15;
int limitSwitch = 16;
float c ;
int delayTime = 2;


bool stopNow;
void turnOn(String deviceId) {
  if (deviceId == DEVICE1)
  {
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);

    turnPart(); //call function turn part

  }
  else  if (deviceId == DEVICE2)
  {
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);

    turnFull(); //call function turn full

  }
  else  if (deviceId == DEVICE7)
  {
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);

    stopNow = true;
    digitalWrite(limitSwitch, LOW);
    delay(1000);
    digitalWrite(limitSwitch, HIGH);
    
  }
}

void turnOff(String deviceId) {
  if (deviceId == DEVICE1)
  {
    Serial.print("Turn off Device ID: ");
    Serial.println(deviceId);

    //no fuction needed
  }
  else if (deviceId == DEVICE2)
  {
    Serial.print("Turn off Device ID: ");
    Serial.println(deviceId);

    //no fuction needed
  }
  else if (deviceId == DEVICE7)
  {
    Serial.print("Turn off Device ID: ");
    Serial.println(deviceId);

    digitalWrite(limitSwitch, HIGH);
  }
}
void setup() {
  Serial.begin(115200);
 stopNow = false;
  
  button1.attachClick(turnPart);
  button1.attachDoubleClick(turnFull);
  button1.attachLongPressStop(turnClose);
  button2.attachClick(slidePart);
  button2.attachDoubleClick(slideFull);
  button2.attachLongPressStop(slideClose);

  pinMode(limitSwitch, INPUT_PULLUP);


  digitalWrite(limitSwitch, HIGH);
}
void loop() {

  button1.tick();
  button2.tick();
}
void turnPart()   // turnMotor part
{
  for (int i = 0; i < 750; i++)
  {
    CWTP();
    // Exit from the loop if the limit switch has been pressed 
    if (digitalRead(limitSwitch) == LOW  ||   stopNow = true;)
      
    {
      // Hit clockwise limit so move anticlockwise ten steps and end the clockwise motion
      for (int i = 0; i < 70; i++)
      {
        ACWTP();
      }
      break;
    }
  }
}
void CWTP()
{
  digitalWrite(turnMotor, LOW);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(turnMotor, HIGH);
}
void ACWTP()
{
  digitalWrite(turnMotor, LOW);
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(turnMotor, HIGH);
}

I have cut the code down to fit it the post I can show sections in more detail if needed.

There are more motor control function but the are all very much the same.

I tried to use a boolean called stopNow to break the loop but could make it work.

Any help appreciated

Using delay means that the Arduino is doing LITERALLY NOTHING but waiting for that time to elapse. That means it can't be listening for inputs so you will never have a responsive program when using delays.

Read over Using millis() for timing. A beginner's guide to understand the concept behind it, and change all of your delay calls with the logic used when using millis() for timing.

Hi, I understand that delay blocks all other actions but I can't see where I've used delay long enough to cause the issue.

Could you elaborate please.

Thanks.

I can't see where I've used delay long enough to cause the issue.

  for (int i = 0; i < 750; i++)
  {
    CWTP();

750 calls to a function with 5 delay()s in it each of 2 milliseconds is 7.5 seconds. That is long enough to cause the problem

If you want a responsive program there should be no "loops" that run for so long that they need to be interrupted. The use of FOR and WHILE can be just as much of a problem as delay() unless the FOR or WHILE completes its task in a few microsecs. Use IF and allow loop() to do the iteration.

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete its task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R

OK,

how can I create the delay using millis

#include "OneButton.h"
#define MyApiKey "84e"
#define MySSID "BTH5"
#define MyWifiPassword "R"
#define HEARTBEAT_INTERVAL 300000

OneButton button2(5, true);

int turnMotor = 9;
int slideMotor = 10;
int motorPin1 = 14;
int motorPin2 = 12;
int motorPin3 = 13;
int motorPin4 = 15;
int limitSwitch = 16;
float c ;
int delayTime = 2;


uint64_t heartbeatTimestamp = 0;
bool isConnected = false;

#define DEVICE1 "5c4cf1351f4"
#define DEVICE7 "5c7483a0ace962"
void setPowerStateOnServer(String deviceId, String value);

void turnOn(String deviceId) {
  if (deviceId == DEVICE1)
  {
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);

    slidePart(); //call function turn part

  }
  else  if (deviceId == DEVICE7)
  {
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);

    digitalWrite(limitSwitch, LOW);
  }
}

void turnOff(String deviceId) {
  if (deviceId == DEVICE1)
  {
    Serial.print("Turn off Device ID: ");
    Serial.println(deviceId);

    //no fuction needed
  }
  else if (deviceId == DEVICE7)
  {
    Serial.print("Turn off Device ID: ");
    Serial.println(deviceId);


    digitalWrite(limitSwitch, HIGH);
  }
}
void setup() {
  Serial.begin(115200);

  bRelay1State = false;

  button2.attachClick(slidePart);

  pinMode(limitSwitch, INPUT_PULLUP);

  pinMode(limitSwitch, OUTPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  pinMode(slideMotor, OUTPUT);
  pinMode(turnMotor, OUTPUT);

  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  digitalWrite(slideMotor, HIGH);
  digitalWrite(turnMotor, HIGH);

  digitalWrite(limitSwitch, HIGH);
void slidePart()   // slideMotor part
{
  for (int i = 0; i < 750; i++)
  {
    CWSP();
    // Exit from the loop if the limit switch has been pressed
    if (digitalRead(limitSwitch) == LOW)
    {
      // Hit clockwise limit so move anticlockwise ten steps and end the clockwise motion
      for (int i = 0; i < 70; i++)
      {
        ACWSP();
      }
      break;
    }
  }
}

void CWSP()
{
  digitalWrite(slideMotor, LOW);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);

  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);

  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);

  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);

  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);

  digitalWrite(slideMotor, HIGH);
}
void ACWSP()
{
  digitalWrite(slideMotor, LOW);
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(slideMotor, HIGH);
}

I have again shorten the code to just one function

Did you read the guide I posted and the one Robin2 posted? Read over those and try to understand how it works, then give it a go yourself. If you try and can't get it to work, post your code that you tried and we can help. Or you could wait and see if someone writes it for you. Up to you.

Hi, I have indeed tried to understand the example and thought I did.
However as I couldn't get it working so I am missing something somewhere. I will post my efforts and any advice will be graciously accepted.

#include "OneButton.h"
#define MyApiKey "84e"
#define MySSID "BTH5"
#define MyWifiPassword "R"
#define HEARTBEAT_INTERVAL 300000

OneButton button2(5, true);

int turnMotor = 9;
int slideMotor = 10;
int motorPin1 = 14;
int motorPin2 = 12;
int motorPin3 = 13;
int motorPin4 = 15;
int limitSwitch = 16;
float c ;
int delayTime = 2;


uint64_t heartbeatTimestamp = 0;
bool isConnected = false;

#define DEVICE1 "5c4cf1351f4"
#define DEVICE7 "5c7483a0ace962"

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 3000;
void setup() {
  Serial.begin(115200);

  startMillis = millis();

  button2.attachClick(slidePart);

  pinMode(limitSwitch, INPUT_PULLUP);

  pinMode(limitSwitch, OUTPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  pinMode(slideMotor, OUTPUT);
  pinMode(turnMotor, OUTPUT);

  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  digitalWrite(slideMotor, HIGH);
  digitalWrite(turnMotor, HIGH);

  digitalWrite(limitSwitch, HIGH);
void loop() {

  currentMillis = millis();

  button2.tick();

  webSocket.loop();


  if (isConnected) {
    uint64_t now = millis();

    // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
    if ((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
      heartbeatTimestamp = now;
      webSocket.sendTXT("H");
    }
  }
}
void slidePart()   // slideMotor part
{
  for (int i = 0; i < 750; i++)
  {
    CWSP();
    // Exit from the loop if the limit switch has been pressed
    if (digitalRead(limitSwitch) == LOW)
    {
      // Hit clockwise limit so move anticlockwise ten steps and end the clockwise motion
      for (int i = 0; i < 70; i++)
      {
        ACWSP();
      }
      break;
    }
  }
}
void CWSP()
{
  digitalWrite(slideMotor, LOW);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  currentMillis - startMillis >= period;
  startMillis = currentMillis;
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);

  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);

  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);

  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);

  digitalWrite(slideMotor, HIGH);
}
void ACWSP()
{
  digitalWrite(slideMotor, LOW);
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);

  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);

  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);

  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);

  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);

  digitalWrite(slideMotor, HIGH);
}

It doesn't work so I know its wrong. I tried putting the function in the loop but it just runs constantly.

Please post the complete program and tell us in as much detail as possible what happens when you run it and what you want it to do that is different.

...R

Ok, its to big to fit in 1 post so I'll spit it across a few.

The purpose of the code is to run 2 stepper motors each motor has 3 preset functions 'part' 'full' 'close'

The 3 functions are called in 2 ways.

1 push button for each motor using one click, double click and long press to call the functions
2 an android app using 6 virtual switches to call the functions.

Each function can be stopped/changed by a limit switch.

all of this works perfectly.

The problem I am having is I want to mimic the limit switch using a virtual switch from the app but I can't get the app to do anything while a function is running which completely defeats the purpose of the switch.

Now it has been suggested that because I delay in the function it is blocking the app command from running, so I think the only option is to replace delay with the millis() this is where I'm struggling.

D4vew5577:
Ok, its to big to fit in 1 post so I'll spit it across a few.

Please just post your complete .ino file as an attachment.

I'm not going to join pieces together in case I make a mistake.

...R

Ok, makes sense.

stepp_multibutton_post.ino (19.5 KB)

This stuff looks very strange

   digitalWrite(limitSwitch, LOW);
    delay(1000);
    digitalWrite(limitSwitch, HIGH);
 pinMode(limitSwitch, INPUT_PULLUP);

  pinMode(limitSwitch, OUTPUT);

Why would you want to write to a switch, or have it as anything but INPUT or INPUT_PULLUP. If a switch shorts an OUPUT pin to 5v or to GND you risk destroying the Arduino.

Many of your function names are completely meaningless to me.

void CWTP()

Spell it out in full. Meaningful names for functions and variables makes it much easier to develop and maintain a program. Especially when you return to it after 6 weeks absence.

This style of programming

void CWTP()
{
  digitalWrite(turnMotor, LOW);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);

  .....

almost guarantees an unresponsive program. Don't use delay() and don't try to do all the stages one after the other without returning to loop().

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

My thinking was to write to the switch low with the app to replicate what the limit switch does, namely ground the switch.

The function names are a reference to the function and what it is doing.

eg CWTP is
Direction of turn, Clockwise
Which motor, Turnmotor
Which function, part

As I posted I am aware that the likely cause was the use of 'delay' and i have looked at both examples but am struggling with incorporating them in to my code.

Would this be a correct start.

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h> //  https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
#include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
#include <StreamString.h>
#include <OneButton.h>

ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
WiFiClient client;


#include "OneButton.h"
#define MyApiKey "840e8a95e"
#define MySSID "BR5"
#define MyWifiPassword "RP3NMH"
#define HEARTBEAT_INTERVAL 300000

OneButton button1(4, true);
OneButton button2(5, true);

int turnMotor = 9;
int slideMotor = 10;
int motorPin1 = 14;
int motorPin2 = 12;
int motorPin3 = 13;
int motorPin4 = 15;
int limitSwitch = 16;
float c ;
int delayTime = 2;

uint64_t heartbeatTimestamp = 0;
bool isConnected = false;

#define DEVICE1 "5c4cff4"
#define DEVICE2 "53351f6"
#define DEVICE3 "5c4c1fb"
#define DEVICE4 "5c4llfd"
#define DEVICE5 "5c73058"
#define DEVICE6 "5c7337b"
#define DEVICE7 "5c7e962"

unsigned long startMillis;  program
unsigned long currentMillis;
const unsigned long period = 100;
void setup() {
  Serial.begin(115200);

  startMillis = millis(); 
  
  button1.attachClick(turnPart);
  button1.attachDoubleClick(turnFull);
  button1.attachLongPressStop(turnClose);
  button2.attachClick(slidePart);
  button2.attachDoubleClick(slideFull);
  button2.attachLongPressStop(slideClose);

  pinMode(limitSwitch, INPUT_PULLUP);

  
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  pinMode(slideMotor, OUTPUT);
  pinMode(turnMotor, OUTPUT);

  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  digitalWrite(slideMotor, HIGH);
  digitalWrite(turnMotor, HIGH);

  digitalWrite(limitSwitch, HIGH);

  WiFiMulti.addAP(MySSID, MyWifiPassword);
  Serial.println();
  Serial.print("Connecting to Wifi: ");
  Serial.println(MySSID);

  // Waiting for Wifi connect
  while (WiFiMulti.run() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  if (WiFiMulti.run() == WL_CONNECTED) {
    Serial.println("");
    Serial.print("WiFi connected. ");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }

  // server address, port and URL
  webSocket.begin("iot.sinric.com", 80, "/");

  // event handler
  webSocket.onEvent(webSocketEvent);
  webSocket.setAuthorization("apikey", MyApiKey);

  // try again every 5000ms if connection has failed
  webSocket.setReconnectInterval(5000);   // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
}
void loop() {

  currentMillis = millis();

  button1.tick();
  button2.tick();

  webSocket.loop();


  if (isConnected) {
    uint64_t now = millis();

    // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
    if ((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
      heartbeatTimestamp = now;
      webSocket.sendTXT("H");
    }
  }
}

D4vew5577:
Would this be a correct start.

It's a waste of time posting snippets of code.

Why not write a short program that illustrates the stuff you are having a problem with and excludes all the irrelevant stuff. It will make it much easier to help you.

The function names are a reference to the function and what it is doing.

eg CWTP is
Direction of turn, Clockwise
Which motor, Turnmotor
Which function, part

So write the function name as

clockwiseWhichMotorTurnPart()

and that still makes no sense to me. And (I suspect) it won't make a lot of sense to your either. Find a genuinely meaningful name for the function. You will be surprised at how it helps to clear the mind.

...R

Ok I have renamed the functions and removed everything not need in the code, including for now the motor control.

all I need this code to do at the minute is when the button is pressed set pin 10 low for 5 seconds then set it high
once I know how to do that I will try and incorporate the motor control.

Can you help me achieve this.

#include <OneButton.h>
#include "OneButton.h"
OneButton button2(5, true);

int slideMotor = 10;
int motorPin1 = 14;
int motorPin2 = 12;
int motorPin3 = 13;
int motorPin4 = 15;
int limitSwitch = 16;

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 5000;

void setup() {
  Serial.begin(115200);

  startMillis = millis();

  button2.attachClick(slidePart);

  pinMode(limitSwitch, INPUT_PULLUP);

  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  pinMode(slideMotor, OUTPUT);


  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  digitalWrite(slideMotor, HIGH);

}
void loop() {

  currentMillis = millis();

  button2.tick();

}
void slidePart()
{
  for (int i = 0; i < 750; i++)
  {
    SlideMotorClockwisePart();
    // Exit from the loop if the limit switch has been pressed
    if (digitalRead(limitSwitch) == LOW)
    {
      // Hit clockwise limit so move anticlockwise ten steps and end the clockwise motion
      for (int i = 0; i < 70; i++)
      {
        SlideMotorAntilockwisePart();
      }
      break;
    }
  }
}
void SlideMotorClockwisePart()
{
  startMillis = currentMillis;
  digitalWrite(slideMotor, LOW);

  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    digitalWrite(slideMotor, HIGH);
  }

}

void SlideMotorAntilockwisePart()
{
  digitalWrite(slideMotor, LOW);

  digitalWrite(slideMotor, HIGH);
}

Now this doesn't work this is how i thought it would. The slideMotor goes LOW but stays LOW

My thinking

Call the function
record the time, startMillis = currentMillis
Set the pin low
Check the time, currentMillis to see if the time period has elapsed
if so set the pin high.

I think my issue is related to checking the current time

  for (int i = 0; i < 750; i++)
  {
    SlideMotorClockwisePart();

You are calling SlideMotorClockwisePart() 750 times

Each time that you call it you do this

void SlideMotorClockwisePart()
{
  startMillis = currentMillis;
  digitalWrite(slideMotor, LOW);
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed

which resets startMillis to currentMillis so the required period will never elapse.

Set startMillis to currentMillis before the for loop and not in the function

Incidentally you do not need to set currentMillis to millis() in the function because you already do that in loop()

I get you, thanks.

However I'm unclear where to put the startMillis=currentMillis

Thanks for the help Bob

I'm unclear where to put the startMillis=currentMillis

Here is a clue

all I need this code to do at the minute is when the button is pressed set pin 10 low for 5 seconds then set it high

So, when does the 5 second period start do you think ?