1

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);
  }
5
  • 1
    Thanks for your reply, I rewrote another way 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 && vacPin==HIGH) { delay (4000); / written this way it works but never turn Vacuum off?*/ digitalWrite(vacPin, LOW); } This way works but never turns vacuum off after its on? – Greg May 5 at 20:35
  • // 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); } – Greg May 7 at 2:16
  • Sorry again! I did read your comments and have no idea why part of my reply didn't show up and the rest doesn't show the way it looked when entered. I'm interested in your input but afraid to try again in a reply. What I learning is I need to go to a nonblocking delay. – Greg May 8 at 14:34
  • then I'll say it again ... please, do not put code into comments ... add code, and any new info, to your post – jsotola May 8 at 15:12
  • I guess I'm a slow learner, I finally get what your telling me about the code (not in comments) I thought you were telling me not to put comments in the code! At this point I can't find your original question. Learning this system is about as hard as the coding itself "for me". Thanks for you patience. – Greg May 10 at 13:21
3

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.

3
  • Thanks, I will read up on that, Your interpretation of what it does is close. When tool detected, move the stepper to position, turn on vacuum, when tool is turn off, vacuum remains on for 4 sec. Thanks again I'll check it out. – Greg May 5 at 19:10
  • That instructables presents the same ides, now structured in a nicer way. Note the important point: Each task gets many, frequent opportunities to execute. They decide whether or not they need to use any one of those opportunities, but they never wait. "Do or not do; there is no wait" to paraphrase Yoda. – JRobert May 7 at 11:15
1

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

  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 */ // 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 )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.