Working solenoid and stepper together

I am here to ask for some help, I stated arduino back in May this year with the aim of using a stepper motor to lift the head of my X1 milling machine to the top of its travel and back down again, presently I have to wind it by hand and at my age its becoming a struggle, fortunately I had two electronic clutches and fitted these so that it didn’t have to wind the stepper as well as the quill, the mechanics are working well, so, with AccelStepper.h I have good positive movement with slow start & stop, I am not a programmer by any sense of the word, but I have managed to find programs on the net that have helped enormously but I need to energise the coil of the electronic clutch while the stepper is moving and I can’t figure out how, perhaps somebody here could point me in the right direction as to how this is achieved.

I’m using this code at the moment,

#include <AccelStepper.h>
AccelStepper stepper(1, 2, 3);

int upPin = 4;     //control panel
int downPin = 5;   //control panel
int stopPin = 6;   //control panel
int limitPin = 7;  //end of travel (microswitch)
int relayPin = 13;  //trigger of relay for electronic clutch

void setup()
{ 
  stepper.setMaxSpeed(800);
  stepper.setAcceleration(100); // Lower No slower start

  pinMode(upPin, INPUT);
  pinMode(downPin, INPUT);
  pinMode(stopPin, INPUT);
  pinMode(limitPin, INPUT); 
  pinMode(relayPin, OUTPUT);
}
void loop() 
{
  if (digitalRead(limitPin) == HIGH) {
    stepper.stop();
  } {
    if (digitalRead(stopPin)==HIGH) //set as  or 0 volts/RED 
    {
      stepper.stop();
    }
    else if(digitalRead(upPin)==HIGH)  
      //with high pulse triggering latching action
    {
      stepper.move(-640000L);
    }
    else if(digitalRead(downPin)==HIGH)
    {
      stepper.move(640000L);
    }
    stepper.run(); 
    {
    }
  }
}

I think all that you need is to keep a single boolean which is true if you are
moving the head.

When ever you see a button pressed and it isn't currently moving, set the moving
flag and call a routine to activate the relay, plus a short delay to allow the
mechanicals to respond, then back to the existing code.

Similarly when you hit the limit, and its currently moving, call a routine
to deactivate the relay (and clear the flag).

That way you only change the state of the relay just before and after the motor
goes from stationary to moving modes.

But I'm assuming that's how the clutch relay works, but so long as you do the
right things in those two routines it should be what you want?

MarkT,

I wanted to thank you for making the time to help, it probably comes as no surprise to you that I had to look up the word “boolean.” to see what I was dealing with, as I understand it it’s like a pigeon hole with a note in it, “on” or “off”, I guess that’s the flag you spoke of, however thanks to you I’ve managed to get two programs working together for the first time in months, probably a bit, crude but its starting to function as I wanted it to, ie. relay on with steppers, and yes you were right it is how the cultch works.
Now, my next problem is how to assign a boolean to a timer, because I think it might be the way to hold the relay on for 5sec after stepper stop’s, is that possible? or do I use the same boolean for the timer as I did for the rest of the program?
looking to the future, using the AccelStepper.h would it be possible to implement a “dead stop” instead of a wind down of the steppers in case something went wrong on the machine?

#include <AccelStepper.h>
AccelStepper stepper(1, 2, 3);

int upPin = 4;         //control panel
int downPin = 5;       //control panel
int stopPin = 6;       //control panel
int limitPin = 7;      //end of travel (micro switch)
int relayPin = 13;     //trigger of relay for electronic clutch
int state = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
long time = 0;         // the last time the output pin was toggled
long debounce = 1500;  // the debounce time, increase if the output flickers

void setup()
{
  stepper.setMaxSpeed(800);
  stepper.set Acceleration(1000); // Lower Number slower start

  pinMode(upPin, INPUT);
  pinMode(downPin, INPUT);
  pinMode(stopPin, INPUT);
  pinMode(limitPin, INPUT);  //This one to flash a pulse to stopPin
  pinMode(relayPin, OUTPUT);
}
void loop() 
///////////////////////////////
{
  reading = digitalRead(upPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) 
  {
    if (state == HIGH)
        state = LOW;
    else
        state = HIGH;
    time = millis();    
  }
  digitalWrite(relayPin, state);
  previous = reading;
  /////////////////////////////////
  {  
    reading = digitalRead(downPin);

    if (reading == HIGH && previous == LOW && millis() - time > debounce) 
    {
      if (state == HIGH)
          state = LOW;
      else
          state = HIGH;
      time = millis();    
    }
    digitalWrite(relayPin, state);
    previous = reading;
    
    /////////////////////////////
    {  
      reading = digitalRead(stopPin);

      if (reading == HIGH && previous == LOW && millis() - time > debounce) 
      {
        if (state == HIGH)
            state = LOW;
            time = millis();    
      }
      digitalWrite(relayPin, state);
      previous = reading;

      /////////////////////////////////
      {
        if (digitalRead(limitPin) == HIGH)
        {
          stepper.stop();
        }
        {
          if (digitalRead(stopPin)==HIGH) //set as  or 0 volts/RED 
            //with high pulse triggering latching action
          {
            stepper.stop();
          }
          else if(digitalRead(upPin)==HIGH) 
          {
            stepper.move(-640000L);  //any number will do neg or pos = dir
          }
          else if(digitalRead(downPin)==HIGH) //set as 5 or 0 volts/RED 
            //with high pulse triggering latching action
          {
            stepper.move(640000L);
          }
          stepper.run(); 
        }
      }
    }
  }
}