user65486: Added second attempt at bottom of code, Please check it, works with out lag time but never turns Vacuum off?  This project is pretty well explained in the code description. My main problem is I need a delay for ditialWrite(vacPin,LOW);.  With this part included the stepper doesn't react a quickly as before 2/3 sec lag, & the ACS712 current sensor does not read at all in the console screen.
I have tried different operatives, statements, and relocating the code in different spots with no luck!
I think this has a simple fix, but it has alluded me!. Hope this is clear enough, any help would be appreciated.
/*This version works as is **EXCEPT** the Delay vac 4 sec has to be turned off. See ########### area for Problem.
/*Auto Blast gate project:  for dust collection in wood shop.
  Arduino Nano
  ACS712 current sensor
  DRV8825 stepper driver
  Nema 17 stepper
  Each tool will be monitor by a current sensor and when that analog port exceeds a given threshold
  1. Rotate the stepper to a predetermined position to align ports for that tool.
  2. After the gate is align a digital pin will drive relay to turn on Vacuum system.
      Vacuum will stay on as long as sensor exceed the threshold while tool is running. 
      **When tool stops vacuum will continue for 4 seconds to clear remaining dust.**
  3. If the same tool is used next, the Stepper will remain in it's current position.
      But the vacuum system should still operate the same.
  4.  currently using buttons to simulate tools and also one ACS712 for testing  */


// defines pins numbers
const int stepPin = 3;
const int dirPin  = 2;
const int enPin  = 8;
const int vacPin = 4; //simulate Vacuum relay

int ca = 0;  //currentAngle
int na = 0;  //angle
int stepPerAngle = 5 / 9; // full step = 1.8 or could have used "*1.8"
int   numstep;
int analogValue = 0;

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

  // Sets the 3 pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);
  pinMode(vacPin, OUTPUT); //Just added this for vac

  //set values for 2 outputs
  digitalWrite(enPin, LOW);
  digitalWrite(dirPin, HIGH);
  digitalWrite(vacPin, LOW); //just add this for vac

 
}

void loop() {
  int n;
 analogValue = 0;
 
  
  // Assign button degrees
  if      ( analogRead(A0) > 600) {
    analogValue = (analogRead(A0));
    Serial.print("AO = ");
    Serial.println(analogValue);
    na = 0; 
    
  }
  else if ( analogRead(A1) > 600) {
    analogValue = (analogRead(A1));
    Serial.print("A1 = ");
    Serial.println(analogValue);
    na = 45;    
  }
  
  else if ( analogRead(A2) > 600) {
    analogValue = (analogRead(A2));
    Serial.print("A2 = ");
    Serial.println(analogValue);
    na = 225;
  }
  
  else if ( analogRead(A3) > 600) {   //=tool on
  analogValue = (analogRead(A3));
    Serial.print("A3 = ");
    Serial.println(analogValue);
    na = 270;
   
  }

   
 /*###########################################################
 **this is where I need help.  The first if statement works as it should.
 when those 2 condition are met the vacPin goes high.** */  

  if(analogValue > 600 && ca==na) {
    digitalWrite(vacPin, HIGH);
  }

/* **this part is not working correctly.  If the delay is remove it works as the "else if" statment reads.
  I need the delay so when a tool is stopped, the vacuum continues for a few secounds to clear remaining saw dust** */

else if (analogValue < 600) {
   delay (4000); **/*  with this on:
             the stepper does not start as quickly as before,
             the analog will not read the ACS712 sensor at all,  only the buttons*/** 
    digitalWrite(vacPin, LOW);
  }

//################################################################################

  if ( ca != na ) {

  //2nd SCENARIO
  if (na - ca > 0 && na - ca <= 180)
    { digitalWrite(dirPin, HIGH);
      n = ((na - ca) * 5 / 9);
      numstep = n;
      ca = na;
     
    }

    //3rd SCENARIO
    else if (ca - na > 0 && ca - na > 180)
    { digitalWrite(dirPin, HIGH);
      n = ((na + 360 - ca) * 5 / 9);
      numstep = n;
      ca = na;
    }

    // 4th SCENARIO
    else if (na - ca < 0 && na - ca <= 180)
    { digitalWrite(dirPin, LOW);
      n = ((ca - na) * 5 / 9);
      numstep = n;
      ca = na;
    }

    //5th SCENARIO
    else if (na - ca > 0 && na - ca > 180)
    { digitalWrite(dirPin, LOW);
      n = ((ca + 360 - na) * 5 / 9);
      numstep = n;
      ca = na;   
  }
    

   

    for (int x = 0; x < numstep; x++) {

      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1000);  //speed
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1000);



    }
    
    
    
    delay(200);
  }
  
}

#################################################
if(analogValue > 600 && ca==na) {
    digitalWrite(vacPin, HIGH);
  }

/* this part is not working correctly.  If the delay is remove it works as the "else if" statment reads.
  I need the delay so when a tool is stopped, the vacuum continues for a few secounds to clear remaining saw dust */

// another variation to the section in question
else if (analogValue < 600 && vacPin==HIGH) {
   delay (4000);
/*  written this way it works but never turn Vacuum off?*/ 
    digitalWrite(vacPin, LOW);
  }


user65486: I don’t know where to start but, this worked PERFECTLY! I need to work on stepper speed and ramp Length to smooth out, or jumpers on the DRV8825 expansion bd. Other than that, I want to understand the library! I went to the readme and it explained the thing it can do, but I don’t know how to utilize on my own. I noticed your name and written by are the same! Am I talking to a person or a company? Thanks again
Thanks for everyone else that help as well and I need to study some of those solutions as well!
MicroBahner
May 8
This would be my suggestion with MobaTools:
/*This version works as is EXCEPT the Delay vac 4 sec has to be turned off. See ########### area for Problem.
Auto Blast gate project:  for dust collection in wood shop.
Arduino Nano
ACS712 current sensor
DRV8825 stepper driver
Nema 17 stepper
Each tool will be monitor by a current sensor and when that analog port exceeds a given threshold

Rotate the stepper to a predetermined position to align ports for that tool.
After the gate is align a digital pin will drive relay to turn on Vacuum system.
Vacuum will stay on as long as sensor exceed the threshold while tool is running.
When tool stops vacuum will continue for 4 seconds to clear remaining dust.
If the same tool is used next, the Stepper will remain in it's current position.
But the vacuum system should still operate the same.
currently using buttons to simulate tools and also one ACS712 for testing
*/
// suggestion with MobaTools for driving the stepper and timer fuction
#include <MobaTools.h>

// defines pins numbers
const int stepPin = 3;
const int dirPin  = 2;
const int enPin  = 8;
const int vacPin = 4; //simulate Vacuum relay
const int stepPer360Degree = 200; // Number of steps for one revolution ( adjust to your needs )
MoToStepper stepper ( stepPer360Degree, STEPDIR );
MoToTimer vacTimer; // for delayed switch off
const long runOnTime = 4000;
int na = 0;  //angle
int analogValue = 0;
void setup() {
Serial.begin(9600);
// Setup the stepper
stepper.attach( stepPin, dirPin );
stepper.setSpeedSteps( 1000, 20 ); // sets Speed and ramplength
stepper.attachEnable( enPin, 100, LOW ); // Disable after 100ms. enabled is LOW
pinMode(vacPin, OUTPUT); //Just added this for vac
//set values for 2 outputs
digitalWrite(vacPin, LOW); //just add this for vac
}
void loop() {
analogValue = 0;
// Assign button degrees
if      ( analogRead(A0) > 600) {
analogValue = (analogRead(A0));
Serial.print("AO = ");
Serial.println(analogValue);
na = 0;
}
else if ( analogRead(A1) > 600) {
analogValue = (analogRead(A1));
Serial.print("A1 = ");
Serial.println(analogValue);
na = 45;
}
else if ( analogRead(A2) > 600) {
analogValue = (analogRead(A2));
Serial.print("A2 = ");
Serial.println(analogValue);
na = 225;
}
else if ( analogRead(A3) > 600) {   //=tool on
analogValue = (analogRead(A3));
Serial.print("A3 = ");
Serial.println(analogValue);
na = 270;
}
// position stepper if needed
if ( stepper.read() != na ) stepper.write( na );
// start vac if tool is on and stepper is in position
if (analogValue > 600 && !stepper.moving()) {
digitalWrite(vacPin, HIGH);
}
if ( vacTimer.expired() ) {
// switch off vac if timer expires
Serial.println( "Vac = off");
digitalWrite(vacPin, LOW);
}
if (analogValue < 600 && digitalRead( vacPin ) && !vacTimer.running()) {
// start timer to switch off vac after time
Serial.println( " start vac run-on-time" );
vacTimer.setTime( runOnTime );
}
}
If you want to give it a try, the library can be installed by means of the library manager ( enter mobatools in the search field )

user313: It's not exactly clear what you're trying to do, but I think you want to start the vac and start the stepper, then 4 seconds later stop the vac. The 4 second delay forces the rest of your code to wait (doing nothing) during the 4 seconds.
Look at the Arduino IDE example program, BlinkWithoutDelay. The technique used there is what you need to use instead of delays, to allow your program to manage multiple things at once. This technique is called non-blocking; any time part of your program waits for something to happen blocks the code from doing anything else until that 'something' completes.
Search for non-blocking programming to read more. There are plenty of articles on the web, including in Arduino Stack Exchange that discuss that.
Update (following your 2021-05-05, 20:48Z update):
Here is an answer I wrote to similar question in which I wrote about non-blocking programming in more detail. The short answer is that you can't use delay() when you need to monitor or control several things simultaneously. Delays keep anything else from happening until they expire which is what is keeping your stepper from advancing as you intended. You need to do something like:
if the tool is running,
  ; // do nothing
else,
  save the time;    // this is the tool-stopped time
  move the stepper; // I assume the stepper completes in < 4s
end

if the tool is stopped and the vac is running,
  if (current_time - saved_time) >= 4000,
    stop the vac;
  end;
end;
// and let loop() keep running

Notice that this never waits for the tool or the fan. It just checks quickly and often, whether something needs to be done; if so, it does it; and in any case it continues on.
Because this is in your loop function, this code runs over and over again, so eventually, when 4 seconds has passed (in the case of the vac), the vac will get stopped, but meanwhile, the stepper has been able to run and any other small tasks needing doing will have an opportunity. The important point is, your code never hangs around waiting for anything to happen; it just very rapidly checks everything that could need to be {started, stopped, accelerated, ...}, if/when something is ready to be done, it does it, but it moves on immediately, in either case.

65486: <p><em><strong>Added second attempt at bottom of code, Please check it, works with out lag time but never turns Vacuum off?</strong></em>  This project is pretty well explained in the code description. My main problem is I need a delay for <strong>ditialWrite(vacPin,LOW);</strong>.  With this part included the stepper doesn't react a quickly as before 2/3 sec lag, &amp; the ACS712 current sensor does not read at all in the console screen.
I have tried different operatives, statements, and relocating the code in different spots with no luck!<br />
I think this has a simple fix, but it has alluded me!. Hope this is clear enough, any help would be appreciated.</p>
<pre><code>/*This version works as is **EXCEPT** the Delay vac 4 sec has to be turned off. See ########### area for Problem.
/*Auto Blast gate project:  for dust collection in wood shop.
  Arduino Nano
  ACS712 current sensor
  DRV8825 stepper driver
  Nema 17 stepper
  Each tool will be monitor by a current sensor and when that analog port exceeds a given threshold
  1. Rotate the stepper to a predetermined position to align ports for that tool.
  2. After the gate is align a digital pin will drive relay to turn on Vacuum system.
      Vacuum will stay on as long as sensor exceed the threshold while tool is running. 
      **When tool stops vacuum will continue for 4 seconds to clear remaining dust.**
  3. If the same tool is used next, the Stepper will remain in it's current position.
      But the vacuum system should still operate the same.
  4.  currently using buttons to simulate tools and also one ACS712 for testing  */


// defines pins numbers
const int stepPin = 3;
const int dirPin  = 2;
const int enPin  = 8;
const int vacPin = 4; //simulate Vacuum relay

int ca = 0;  //currentAngle
int na = 0;  //angle
int stepPerAngle = 5 / 9; // full step = 1.8 or could have used &quot;*1.8&quot;
int   numstep;
int analogValue = 0;

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

  // Sets the 3 pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);
  pinMode(vacPin, OUTPUT); //Just added this for vac

  //set values for 2 outputs
  digitalWrite(enPin, LOW);
  digitalWrite(dirPin, HIGH);
  digitalWrite(vacPin, LOW); //just add this for vac

 
}

void loop() {
  int n;
 analogValue = 0;
 
  
  // Assign button degrees
  if      ( analogRead(A0) &gt; 600) {
    analogValue = (analogRead(A0));
    Serial.print(&quot;AO = &quot;);
    Serial.println(analogValue);
    na = 0; 
    
  }
  else if ( analogRead(A1) &gt; 600) {
    analogValue = (analogRead(A1));
    Serial.print(&quot;A1 = &quot;);
    Serial.println(analogValue);
    na = 45;    
  }
  
  else if ( analogRead(A2) &gt; 600) {
    analogValue = (analogRead(A2));
    Serial.print(&quot;A2 = &quot;);
    Serial.println(analogValue);
    na = 225;
  }
  
  else if ( analogRead(A3) &gt; 600) {   //=tool on
  analogValue = (analogRead(A3));
    Serial.print(&quot;A3 = &quot;);
    Serial.println(analogValue);
    na = 270;
   
  }

   
 /*###########################################################
 **this is where I need help.  The first if statement works as it should.
 when those 2 condition are met the vacPin goes high.** */  

  if(analogValue &gt; 600 &amp;&amp; ca==na) {
    digitalWrite(vacPin, HIGH);
  }

/* **this part is not working correctly.  If the delay is remove it works as the &quot;else if&quot; statment reads.
  I need the delay so when a tool is stopped, the vacuum continues for a few secounds to clear remaining saw dust** */

else if (analogValue &lt; 600) {
   delay (4000); **/*  with this on:
             the stepper does not start as quickly as before,
             the analog will not read the ACS712 sensor at all,  only the buttons*/** 
    digitalWrite(vacPin, LOW);
  }

//################################################################################

  if ( ca != na ) {

  //2nd SCENARIO
  if (na - ca &gt; 0 &amp;&amp; na - ca &lt;= 180)
    { digitalWrite(dirPin, HIGH);
      n = ((na - ca) * 5 / 9);
      numstep = n;
      ca = na;
     
    }

    //3rd SCENARIO
    else if (ca - na &gt; 0 &amp;&amp; ca - na &gt; 180)
    { digitalWrite(dirPin, HIGH);
      n = ((na + 360 - ca) * 5 / 9);
      numstep = n;
      ca = na;
    }

    // 4th SCENARIO
    else if (na - ca &lt; 0 &amp;&amp; na - ca &lt;= 180)
    { digitalWrite(dirPin, LOW);
      n = ((ca - na) * 5 / 9);
      numstep = n;
      ca = na;
    }

    //5th SCENARIO
    else if (na - ca &gt; 0 &amp;&amp; na - ca &gt; 180)
    { digitalWrite(dirPin, LOW);
      n = ((ca + 360 - na) * 5 / 9);
      numstep = n;
      ca = na;   
  }
    

   

    for (int x = 0; x &lt; numstep; x++) {

      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1000);  //speed
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1000);



    }
    
    
    
    delay(200);
  }
  
}
</code></pre>
<p>#################################################</p>
<pre><code>if(analogValue &gt; 600 &amp;&amp; ca==na) {
    digitalWrite(vacPin, HIGH);
  }

/* this part is not working correctly.  If the delay is remove it works as the &quot;else if&quot; statment reads.
  I need the delay so when a tool is stopped, the vacuum continues for a few secounds to clear remaining saw dust */

// another variation to the section in question
else if (analogValue &lt; 600 &amp;&amp; vacPin==HIGH) {
   delay (4000);
/*  written this way it works but never turn Vacuum off?*/ 
    digitalWrite(vacPin, LOW);
  }
</code></pre>
 65486: <p>I don’t know where to start but, this worked <strong>PERFECTLY!</strong> I need to work on stepper speed and ramp Length to smooth out, or jumpers on the DRV8825 expansion bd. Other than that, I want to understand the library! I went to the readme and it explained the thing it can do, but I don’t know how to utilize on my own. I noticed your name and written by are the same! Am I talking to a person or a company? Thanks again
Thanks for everyone else that help as well and I need to study some of those solutions as well!</p>
<p><strong>MicroBahner</strong>
May 8
This would be my suggestion with MobaTools:</p>
<p>/*This version works as is <strong>EXCEPT</strong> the Delay vac 4 sec has to be turned off. See ########### area for Problem.
Auto Blast gate project:  for dust collection in wood shop.
Arduino Nano
ACS712 current sensor
DRV8825 stepper driver
Nema 17 stepper
Each tool will be monitor by a current sensor and when that analog port exceeds a given threshold</p>
<ol>
<li>Rotate the stepper to a predetermined position to align ports for that tool.</li>
<li>After the gate is align a digital pin will drive relay to turn on Vacuum system.
Vacuum will stay on as long as sensor exceed the threshold while tool is running.
<strong>When tool stops vacuum will continue for 4 seconds to clear remaining dust.</strong></li>
<li>If the same tool is used next, the Stepper will remain in it's current position.
But the vacuum system should still operate the same.</li>
<li>currently using buttons to simulate tools and also one ACS712 for testing
*/
// suggestion with MobaTools for driving the stepper and timer fuction
#include &lt;MobaTools.h&gt;</li>
</ol>
<p>// defines pins numbers
const int stepPin = 3;
const int dirPin  = 2;
const int enPin  = 8;
const int vacPin = 4; //simulate Vacuum relay</p>
<p>const int stepPer360Degree = 200; // Number of steps for one revolution ( adjust to your needs )
MoToStepper stepper ( stepPer360Degree, STEPDIR );
MoToTimer vacTimer; // for delayed switch off
const long runOnTime = 4000;</p>
<p>int na = 0;  //angle</p>
<p>int analogValue = 0;</p>
<p>void setup() {
Serial.begin(9600);</p>
<p>// Setup the stepper
stepper.attach( stepPin, dirPin );
stepper.setSpeedSteps( 1000, 20 ); // sets Speed and ramplength
stepper.attachEnable( enPin, 100, LOW ); // Disable after 100ms. enabled is LOW</p>
<p>pinMode(vacPin, OUTPUT); //Just added this for vac</p>
<p>//set values for 2 outputs
digitalWrite(vacPin, LOW); //just add this for vac</p>
<p>}</p>
<p>void loop() {
analogValue = 0;</p>
<p>// Assign button degrees
if      ( analogRead(A0) &gt; 600) {
analogValue = (analogRead(A0));
Serial.print(&quot;AO = &quot;);
Serial.println(analogValue);
na = 0;</p>
<p>}
else if ( analogRead(A1) &gt; 600) {
analogValue = (analogRead(A1));
Serial.print(&quot;A1 = &quot;);
Serial.println(analogValue);
na = 45;
}</p>
<p>else if ( analogRead(A2) &gt; 600) {
analogValue = (analogRead(A2));
Serial.print(&quot;A2 = &quot;);
Serial.println(analogValue);
na = 225;
}</p>
<p>else if ( analogRead(A3) &gt; 600) {   //=tool on
analogValue = (analogRead(A3));
Serial.print(&quot;A3 = &quot;);
Serial.println(analogValue);
na = 270;</p>
<p>}</p>
<p>// position stepper if needed
if ( stepper.read() != na ) stepper.write( na );</p>
<p>// start vac if tool is on and stepper is in position
if (analogValue &gt; 600 &amp;&amp; !stepper.moving()) {
digitalWrite(vacPin, HIGH);
}</p>
<p>if ( vacTimer.expired() ) {
// switch off vac if timer expires
Serial.println( &quot;Vac = off&quot;);
digitalWrite(vacPin, LOW);
}</p>
<p>if (analogValue &lt; 600 &amp;&amp; digitalRead( vacPin ) &amp;&amp; !vacTimer.running()) {
// start timer to switch off vac after time
Serial.println( &quot; start vac run-on-time&quot; );
vacTimer.setTime( runOnTime );
}</p>
<p>}
If you want to give it a try, the library can be installed by means of the library manager ( enter mobatools in the search field )</p>
 313: <p>It's not exactly clear what you're trying to do, but I think you want to start the vac and start the stepper, then 4 seconds later stop the vac. The 4 second delay forces the rest of your code to wait (doing nothing) during the 4 seconds.</p>
<p>Look at the Arduino IDE example program, BlinkWithoutDelay. The technique used there is what you need to use instead of delays, to allow your program to manage multiple things at once. This technique is called non-blocking; any time part of your program waits for something to happen blocks the code from doing anything else until that 'something' completes.</p>
<p>Search for non-blocking programming to read more. There are plenty of articles on the web, including in Arduino Stack Exchange that discuss that.</p>
<p>Update (following your 2021-05-05, 20:48Z update):</p>
<p><a href="https://arduino.stackexchange.com/questions/60759/how-to-rotate-two-or-more-steppers-simultaneously-by-giving-array-of-angles/66380#66380">Here is an answer</a> I wrote to similar question in which I wrote about non-blocking programming in more detail. The short answer is that you can't use delay() when you need to monitor or control several things simultaneously. <em>Delays keep anything else from happening until they expire</em> which is what is keeping your stepper from advancing as you intended. You need to do something like:</p>
<pre><code>if the tool is running,
  ; // do nothing
else,
  save the time;    // this is the tool-stopped time
  move the stepper; // I assume the stepper completes in &lt; 4s
end

if the tool is stopped and the vac is running,
  if (current_time - saved_time) &gt;= 4000,
    stop the vac;
  end;
end;
// and let loop() keep running
</code></pre>
<p>Notice that this <em>never waits</em> for the tool or the fan. It just checks quickly and often, whether something needs to be done; if so, it does it; and in any case it continues on.</p>
<p>Because this is in your loop function, this code runs over and over again, so eventually, when 4 seconds has passed (in the case of the vac), the vac will get stopped, but meanwhile, the stepper has been able to run and any other small tasks needing doing will have an opportunity. The important point is, your code <em>never hangs around waiting</em> for anything to happen; it just very rapidly checks everything that could need to be {started, stopped, accelerated, ...}, if/when something is ready to be done, it does it, but it moves on immediately, in either case.</p>
