Notifications
Clear all

Questions about changing to array from buttons  

Page 5 / 7

GAM
 GAM
(@gam)
Eminent Member
Joined: 4 months ago
Posts: 46
Topic starter  

@robotbuilder

Then I have failed because it was all about a high level description anyone should be able to follow.

I feel that I have offended you in someway?  I hope that is not the case. 

I have learned much from you and your replies.  The word "decipher" was maybe a poor choice.  What you wrote was clear and understood.  Your help is very much appreciated and I have looked forward to your replies. If anyone has failed it would be me! 

I struggle as much writing questions and replies as I do writing code. 

Greg


ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 777
 

@gam

No offence taken.  I made the mistake of trying to get you to code my way 🙂

It is hard to know if any suggestion works without being able to test it,

Essentially I think your first block of code lacked a signal when no tools were on returning na == 0 which is the value for a tool.  So I have used analogValue = 0 at the start of each loop so it will exit the first block of code at that value if none of the sensors are greater than 600.

Hopefully you can spot my changes where I have used lines of ****************

 

 

/*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 + 5 seconds.
  3. If the same tool is used next, the Stepper will remain in it's current position.
      But the vacuum stytem should still operate the same.

*/


// 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;                //new 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  // reset in case no tools on *
  //*********************************************

  // 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) {
  analogValue = (analogRead(A3));
    Serial.print("A3 = ");
    Serial.println(analogValue);

    na = 270;
   
  }

  // **********************************************
  // *  now analogValue == 0 if no tools were on  *
  // **********************************************
  If (analogValue > 600 && ca==na) {
    digitalWrite(vacPin, HIGH);
  }
  else if (analogValue < 600) {
    delay (4000):
    digitalWrite(vacPin, LOW);
  }
// ************************************************


  /* 1st SCENARIO if these to ARE the same nothing happens
     only if they ARE not equal then steps through to find a true statement to act on*/
  if ( ca != na ) {

  // ******************************************************************
  digitalWrite(vacPin, LOW); // *****  turn of vac before moving disc *
  // ******************************************************************
  
  //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(500);
  }
}

 

 
This post was modified 3 weeks ago 2 times by robotBuilder

ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 777
 

@gam

Previous post arduino code had some syntax errors unfortunately the 1 hour limit has passed so I can't edit in the corrections.  So here is a repost of same code that compiled.  I was having trouble with the Arduino IDE and have only just fixed it.

 

 

/*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 + 5 seconds.
  3. If the same tool is used next, the Stepper will remain in it's current position.
      But the vacuum stytem should still operate the same.

*/


// 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;                //new 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;  // reset in case no tools on *
  //*********************************************

  // 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) {
  analogValue = (analogRead(A3));
    Serial.print("A3 = ");
    Serial.println(analogValue);

    na = 270;
   
  }

  // **********************************************
  // *  now analogValue == 0 if no tools were on  *
  // **********************************************
  if (analogValue > 600 && ca==na) {
    digitalWrite(vacPin, HIGH);
  }
  else if (analogValue < 600) {
    delay (4000);
    digitalWrite(vacPin, LOW);
  }
// ************************************************


  /* 1st SCENARIO if these to ARE the same nothing happens
     only if they ARE not equal then steps through to find a true statement to act on*/
  if ( ca != na ) {

  // ******************************************************************
  digitalWrite(vacPin, LOW); // *****  turn of vac before moving disc *
  // ******************************************************************
  
  //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(500);
  }
}

 

 


ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 777
 

@gam

When trying to run a simulation of your code I couldn't get it to work properly.  I have long been interested in playing around with stepper motor code so I will try to find time to put a little arrow on a small stepper motor and see if I can write the code to work with actual hardware.  My own code works fine but the code to work with real hardware has to replace the current code in the functions (shown as stubs) that work with a simulation of the hardware.

I don't compute the number of steps rather I move the stepper motor until the position of the disc equals the position of the current tool's outlet in MOVE_DISC_TO_TOOL_POSITION().  The ANY_TOOL_ON() function returns the number of the first tool found to be on. I assume only one tool can be on at once with your disc method of selecting an outlet.

 

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

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
}

bool ANY_TOOL_ON(){}
bool DISC_AT_TOOL_POSITION(){}
bool VACUUM_ON(){}
void TURN_ON_VACUUM(){}
void TURN_OFF_VACUUM(){}
void MOVE_DISC_TO_TOOL_POSITION(){}

void loop() {
  if (ANY_TOOL_ON() == true) {
    if (DISC_AT_TOOL_POSITION()==true) { 
      if (VACUUM_ON() == false) {
        TURN_ON_VACUUM();
      }else{
        TURN_OFF_VACUUM();
        MOVE_DISC_TO_TOOL_POSITION();
        TURN_ON_VACUUM();
      }
    }else{
    TURN_OFF_VACUUM();
    }
  }
}

 

This post was modified 3 weeks ago by robotBuilder

ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 777
 

@gam

Just remembered I have already posted the code example in the last post!

I have just now simplified the code a bit and written and tested the code for the functions.  Perhaps I can use the Arduino's Serial Monitor to enter tool numbers and display the results until I can test it with actual hardware.  The code solution may not be what you want but it will be an example of another way of coding a solution.

 

This post was modified 3 weeks ago 3 times by robotBuilder

ReplyQuote
GAM
 GAM
(@gam)
Eminent Member
Joined: 4 months ago
Posts: 46
Topic starter  

@robotbuilder@madmisha@yurkshirelad@byron

Copy of code below.  I have add the proposed code from robotbuilder for turning the Vacuum on and off.  Works correctly only if I take the delay out of the else code for turning the vacuum off.  Of course then I don't have the added time to clear the debris in the lines. 

With the delay included

  • the stepper doesn't move instantly as before (might be 2/3 sec before stepper moves depending on the order of buttons push and maybe time between being selected.
  • the ACS712  current sensor isn't being detected at all, only the buttons.  

I have move that code to different place in the program, Bottom, top ,, ect. and have tried a while loop as well with no luck.  Still trying things and think it must be simple?  

I can add a "master" ACS712 and use that for the vacuum only, but think I should be able to code it without that.

 

```cpp
/*This version what as it EXCEPT the Delay vac 4 sec has to be turned off. See line 87 for info
/*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 + 5 seconds.
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.*/

// 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;

}

 

 

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

 

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
an with out it, can't clear vac lines */
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);
}

}

```


ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 777
 

@gam

If you are using the Arduino IDE then right mouse Select All,  right mouse Copy As HTML, then insert into post between a pair of <p></p>

Here is a modified version of your code. I can't test the code so I don't know if there are any logic errors.

 

 

// 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() {

  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;
  }

  if(analogValue < 600) {   // then no tool on
    digitalWrite(vacPin, LOW);  // so turn off vac
  } 
  else if ( ca != na )  // tool on but not equal to disc angle so move disc
  {
    digitalWrite(vacPin, LOW);  // turn off vac while moving disc
   // compute numstep and set direction pin
   //2nd SCENARIO    if (na - ca > 0 && na - ca <= 180){      digitalWrite(dirPin, HIGH);      numstep = ((na - ca) * 5 / 9);      ca = na;    //3rd SCENARIO    } else if (ca - na > 0 && ca - na > 180){      digitalWrite(dirPin, HIGH);      numstep = ((na + 360 - ca) * 5 / 9);      ca = na;    // 4th SCENARIO    }else if (na - ca < 0 && na - ca <= 180){      digitalWrite(dirPin, LOW);      numstep = ((ca - na) * 5 / 9);      ca = na;      //5th SCENARIO    }else if (na - ca > 0 && na - ca > 180){      digitalWrite(dirPin, LOW);      numstep = ((ca + 360 - na) * 5 / 9);      ca = na;    }    for (int x = 0; x < numstep; x++) {      digitalWrite(stepPin, HIGH);      delayMicroseconds(1000); //speed      digitalWrite(stepPin, LOW);      delayMicroseconds(1000);    }        digitalWrite(vacPin, HIGH);  // turn on vac after moving disc        delay(200);  } }

 

 


ReplyQuote
GAM
 GAM
(@gam)
Eminent Member
Joined: 4 months ago
Posts: 46
Topic starter  

 

@robotbuilder   Thanks for the reply,  Just got back to the bench and getting ready to test the version you sent.

  • Looks like you move the "Vac on" portion to the end of the loop.
  • "Vac off" or low remains were it was before the scenarios start.  I don't see the delay for turning "vac off".  Are those all the changes or am I missing something else?

What are your thoughts about using a While statement for

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

else if (analogValue < 600) {
 delay (4000); 
digitalWrite(vacPin, LOW);
}

 

You mention a simulator you used and was curious about which one?  I have tried a couple and seemed harder that just hooking it up.  Would be nice though when away for the bench to try things.


ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 777
 

@gam

I write my own simulations in the BASIC language which I find easy to understand.

I suspect the last version I posted, a modified version of your code, will not work as I just tried a BASIC simulation of the code with crazy results. I think maybe there is something wrong with the computation of the direction pin value and the number of steps.  I compute the direction but not the number of steps. I simply move it until ca == na.

Does your version actually work?

The problem with a while loop is if the condition is never met then the program is locked up in the loop.

My simulated version works fine so I will convert it to Arduino code. I now have a little stepper motor and driver so I will use buttons to simulate the tool on/off and a LED to simulate vac on/off to test it.

Probably if I was building your disc system I wouldn't use a stepper motor, just an ordinary motor with hall effect devices to detect each disc position.

To enlarge image right mouse click and open in new window.

simulation
This post was modified 2 weeks ago by robotBuilder

ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 777
 

@gam

Sorry gam your code works fine on the simulation.  I forgot about the fact the actual hardware has 200 steps per 360 degrees rotation.  My version uses the 200 steps and sets na accordingly.

 


ReplyQuote
GAM
 GAM
(@gam)
Eminent Member
Joined: 4 months ago
Posts: 46
Topic starter  

@robotbuilder 

Yes the code I have works correctly "IF" I don't include the delay (4000); before the statement

digitalWrite(vacPin, LOW);  
 
With this included the stepper movement is sluggish and the ACS712 current sensor does not work at all. " Doesn't show in console at all."
 
Of course I need the shut off delay to clear the remaining dust from the line.
 
I wonder if I activate a new button to soon it is still in the 4 sec delay?
I have no idea why it stops the ACS712 from showing any activity?
 
 

ReplyQuote
GAM
 GAM
(@gam)
Eminent Member
Joined: 4 months ago
Posts: 46
Topic starter  

@robotbuilder

Just ran across this article in Nut & Volt Mag.  It is about RTOS.  Real Time Operating System.  You might already be familiar with this.  I thought it might offer another solution for my problem. 

Nuts Volts Magazine | Nuts & Volts Magazine https://www.nutsvolts.com/magazine/issue/2019/06


ReplyQuote
GAM
 GAM
(@gam)
Eminent Member
Joined: 4 months ago
Posts: 46
Topic starter  

@robotbuilder@madmisha@yurkshirelad@byron

 

 

I don’t know where to start but, this workedPERFECTLY!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!

 

I tried to copy as HTLM and put between <p>  </p>  about 7 times and this is as close as I can get.

MicroBahnerMay 8 This would be my suggestion with MobaTools:

p

<pre>
<font color="#95a5a6">/*This version works as is **EXCEPT** the Delay vac 4 sec has to be turned off. See ########### area for Problem.</font>
<font color="#95a5a6"> &nbsp;Auto Blast gate project: &nbsp;for dust collection in wood shop.</font>
<font color="#95a5a6"> &nbsp;Arduino Nano</font>
<font color="#95a5a6"> &nbsp;ACS712 current sensor</font>
<font color="#95a5a6"> &nbsp;DRV8825 stepper driver</font>
<font color="#95a5a6"> &nbsp;Nema 17 stepper</font>
<font color="#95a5a6"> &nbsp;Each tool will be monitor by a current sensor and when that analog port exceeds a given threshold</font>
<font color="#95a5a6"> &nbsp;1. Rotate the stepper to a predetermined position to align ports for that tool.</font>
<font color="#95a5a6"> &nbsp;2. After the gate is align a digital pin will drive relay to turn on Vacuum system.</font>
<font color="#95a5a6"> &nbsp;&nbsp;Vacuum will stay on as long as sensor exceed the threshold while tool is running.</font>
<font color="#95a5a6"> &nbsp;&nbsp;**When tool stops vacuum will continue for 4 seconds to clear remaining dust.**</font>
<font color="#95a5a6"> &nbsp;3. If the same tool is used next, the Stepper will remain in it's current position.</font>
<font color="#95a5a6"> &nbsp;&nbsp;But the vacuum system should still operate the same.</font>
<font color="#95a5a6"> &nbsp;4. &nbsp;currently using buttons to simulate tools and also one ACS712 for testing </font>
<font color="#95a5a6">*/</font>
<font color="#434f54">// suggestion with MobaTools for driving the stepper and timer fuction</font>
<font color="#5e6d03">#include</font> <font color="#434f54">&lt;</font><font color="#000000">MobaTools</font><font color="#434f54">.</font><font color="#000000">h</font><font color="#434f54">&gt;</font>

<font color="#434f54">// defines pins numbers</font>
<font color="#00979c">const</font> <font color="#00979c">int</font> <font color="#000000">stepPin</font> <font color="#434f54">=</font> <font color="#000000">3</font><font color="#000000">;</font>
<font color="#00979c">const</font> <font color="#00979c">int</font> <font color="#000000">dirPin</font> &nbsp;<font color="#434f54">=</font> <font color="#000000">2</font><font color="#000000">;</font>
<font color="#00979c">const</font> <font color="#00979c">int</font> <font color="#000000">enPin</font> &nbsp;<font color="#434f54">=</font> <font color="#000000">8</font><font color="#000000">;</font>
<font color="#00979c">const</font> <font color="#00979c">int</font> <font color="#000000">vacPin</font> <font color="#434f54">=</font> <font color="#000000">4</font><font color="#000000">;</font> <font color="#434f54">//simulate Vacuum relay</font>

<font color="#00979c">const</font> <font color="#00979c">int</font> <font color="#000000">stepPer360Degree</font> <font color="#434f54">=</font> <font color="#000000">200</font><font color="#000000">;</font> <font color="#434f54">// Number of steps for one revolution ( adjust to your needs )</font>
<font color="#000000">MoToStepper</font> <font color="#000000">stepper</font> <font color="#000000">(</font> <font color="#000000">stepPer360Degree</font><font color="#434f54">,</font> <font color="#000000">STEPDIR</font> <font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">MoToTimer</font> <font color="#000000">vacTimer</font><font color="#000000">;</font> <font color="#434f54">// for delayed switch off</font>
<font color="#00979c">const</font> <font color="#00979c">long</font> <font color="#000000">runOnTime</font> <font color="#434f54">=</font> <font color="#000000">4000</font><font color="#000000">;</font>

<font color="#00979c">int</font> <font color="#000000">na</font> <font color="#434f54">=</font> <font color="#000000">0</font><font color="#000000">;</font> &nbsp;<font color="#434f54">//angle</font>

<font color="#00979c">int</font> <font color="#000000">analogValue</font> <font color="#434f54">=</font> <font color="#000000">0</font><font color="#000000">;</font>

<font color="#00979c">void</font> <font color="#5e6d03">setup</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#000000">{</font>
&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">begin</font><font color="#000000">(</font><font color="#000000">9600</font><font color="#000000">)</font><font color="#000000">;</font>

&nbsp;<font color="#434f54">// Setup the stepper</font>
&nbsp;<font color="#000000">stepper</font><font color="#434f54">.</font><font color="#d35400">attach</font><font color="#000000">(</font> <font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#000000">dirPin</font> <font color="#000000">)</font><font color="#000000">;</font>
&nbsp;<font color="#000000">stepper</font><font color="#434f54">.</font><font color="#000000">setSpeedSteps</font><font color="#000000">(</font> <font color="#000000">1500</font><font color="#434f54">,</font> <font color="#000000">10</font> <font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// sets Speed and ramplength</font>
&nbsp;<font color="#000000">stepper</font><font color="#434f54">.</font><font color="#000000">attachEnable</font><font color="#000000">(</font> <font color="#000000">enPin</font><font color="#434f54">,</font> <font color="#000000">100</font><font color="#434f54">,</font> <font color="#00979c">LOW</font> <font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Disable after 100ms. enabled is LOW</font>

&nbsp;<font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">vacPin</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Just added this for vac</font>

&nbsp;<font color="#434f54">//set values for 2 outputs</font>
&nbsp;<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">vacPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//just add this for vac</font>


<font color="#000000">}</font>

<font color="#00979c">void</font> <font color="#5e6d03">loop</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#000000">{</font>
&nbsp;<font color="#000000">analogValue</font> <font color="#434f54">=</font> <font color="#000000">0</font><font color="#000000">;</font>

&nbsp;<font color="#434f54">// Assign button degrees</font>
&nbsp;<font color="#5e6d03">if</font> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#000000">(</font> <font color="#d35400">analogRead</font><font color="#000000">(</font><font color="#000000">A0</font><font color="#000000">)</font> <font color="#434f54">&gt;</font> <font color="#000000">600</font><font color="#000000">)</font> <font color="#000000">{</font>
&nbsp;&nbsp;&nbsp;<font color="#000000">analogValue</font> <font color="#434f54">=</font> <font color="#000000">(</font><font color="#d35400">analogRead</font><font color="#000000">(</font><font color="#000000">A0</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">"AO = "</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#000000">analogValue</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;&nbsp;&nbsp;<font color="#000000">na</font> <font color="#434f54">=</font> <font color="#000000">0</font><font color="#000000">;</font>

&nbsp;<font color="#000000">}</font>
&nbsp;<font color="#5e6d03">else</font> <font color="#5e6d03">if</font> <font color="#000000">(</font> <font color="#d35400">analogRead</font><font color="#000000">(</font><font color="#000000">A1</font><font color="#000000">)</font> <font color="#434f54">&gt;</font> <font color="#000000">600</font><font color="#000000">)</font> <font color="#000000">{</font>
&nbsp;&nbsp;&nbsp;<font color="#000000">analogValue</font> <font color="#434f54">=</font> <font color="#000000">(</font><font color="#d35400">analogRead</font><font color="#000000">(</font><font color="#000000">A1</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">"A1 = "</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#000000">analogValue</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;&nbsp;&nbsp;<font color="#000000">na</font> <font color="#434f54">=</font> <font color="#000000">45</font><font color="#000000">;</font>
&nbsp;<font color="#000000">}</font>

&nbsp;<font color="#5e6d03">else</font> <font color="#5e6d03">if</font> <font color="#000000">(</font> <font color="#d35400">analogRead</font><font color="#000000">(</font><font color="#000000">A2</font><font color="#000000">)</font> <font color="#434f54">&gt;</font> <font color="#000000">600</font><font color="#000000">)</font> <font color="#000000">{</font>
&nbsp;&nbsp;&nbsp;<font color="#000000">analogValue</font> <font color="#434f54">=</font> <font color="#000000">(</font><font color="#d35400">analogRead</font><font color="#000000">(</font><font color="#000000">A2</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">"A2 = "</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#000000">analogValue</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;&nbsp;&nbsp;<font color="#000000">na</font> <font color="#434f54">=</font> <font color="#000000">225</font><font color="#000000">;</font>
&nbsp;<font color="#000000">}</font>

&nbsp;<font color="#5e6d03">else</font> <font color="#5e6d03">if</font> <font color="#000000">(</font> <font color="#d35400">analogRead</font><font color="#000000">(</font><font color="#000000">A3</font><font color="#000000">)</font> <font color="#434f54">&gt;</font> <font color="#000000">600</font><font color="#000000">)</font> <font color="#000000">{</font> &nbsp;&nbsp;<font color="#434f54">//=tool on</font>
&nbsp;&nbsp;&nbsp;<font color="#000000">analogValue</font> <font color="#434f54">=</font> <font color="#000000">(</font><font color="#d35400">analogRead</font><font color="#000000">(</font><font color="#000000">A3</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">print</font><font color="#000000">(</font><font color="#005c5f">"A3 = "</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font><font color="#000000">analogValue</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;&nbsp;&nbsp;<font color="#000000">na</font> <font color="#434f54">=</font> <font color="#000000">270</font><font color="#000000">;</font>

&nbsp;<font color="#000000">}</font>

&nbsp;<font color="#434f54">// position stepper if needed</font>
&nbsp;<font color="#5e6d03">if</font> <font color="#000000">(</font> <font color="#000000">stepper</font><font color="#434f54">.</font><font color="#d35400">read</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#434f54">!=</font> <font color="#000000">na</font> <font color="#000000">)</font> <font color="#000000">stepper</font><font color="#434f54">.</font><font color="#d35400">write</font><font color="#000000">(</font> <font color="#000000">na</font> <font color="#000000">)</font><font color="#000000">;</font>

&nbsp;<font color="#434f54">// start vac if tool is on and stepper is in position</font>
&nbsp;<font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">analogValue</font> <font color="#434f54">&gt;</font> <font color="#000000">600</font> <font color="#434f54">&amp;&amp;</font> <font color="#434f54">!</font><font color="#000000">stepper</font><font color="#434f54">.</font><font color="#000000">moving</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">)</font> <font color="#000000">{</font>
&nbsp;&nbsp;&nbsp;<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">vacPin</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;<font color="#000000">}</font>
&nbsp;
&nbsp;<font color="#5e6d03">if</font> <font color="#000000">(</font> <font color="#000000">vacTimer</font><font color="#434f54">.</font><font color="#000000">expired</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#000000">)</font> <font color="#000000">{</font>
&nbsp;&nbsp;&nbsp;<font color="#434f54">// switch off vac if timer expires</font>
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font> <font color="#005c5f">"Vac = off"</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;&nbsp;&nbsp;<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">vacPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
&nbsp;<font color="#000000">}</font>

&nbsp;<font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">analogValue</font> <font color="#434f54">&lt;</font> <font color="#000000">600</font> <font color="#434f54">&amp;&amp;</font> <font color="#d35400">digitalRead</font><font color="#000000">(</font> <font color="#000000">vacPin</font> <font color="#000000">)</font> <font color="#434f54">&amp;&amp;</font> <font color="#434f54">!</font><font color="#000000">vacTimer</font><font color="#434f54">.</font><font color="#d35400">running</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">)</font> <font color="#000000">{</font>
&nbsp;&nbsp;&nbsp;<font color="#434f54">// start timer to switch off vac after time</font>
&nbsp;&nbsp;&nbsp;<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">println</font><font color="#000000">(</font> <font color="#005c5f">" start vac run-on-time"</font> <font color="#000000">)</font><font color="#000000">;</font>
&nbsp;&nbsp;&nbsp;<font color="#000000">vacTimer</font><font color="#434f54">.</font><font color="#000000">setTime</font><font color="#000000">(</font> <font color="#000000">runOnTime</font> <font color="#000000">)</font><font color="#000000">;</font>
&nbsp;<font color="#000000">}</font>

<font color="#000000">}</font>

</pre>

ReplyQuote
byron
(@byron)
Honorable Member
Joined: 2 years ago
Posts: 503
 

@gam

I'm not sure what all that code is, but I think I gather its good news 😀 and it works perfectly?

Is this the library you are using, and is your post meant to show your code using this library? 

https://github.com/MicroBahner/MobaTools

 


ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 777
 

@byron

I tried to copy as HTLM and put between <p> </p> about 7 times and this is as close as I can get.

Are you using the Arduino IDE on Windows?

Right click Select All, right click Copy as HTML, (it goes to the clipboard)

Then go back to your post on the forum and make sure you have a few <p></p> by hitting the Enter key a few times where you want to insert the code.

Then in the top bar of your post click {;} You should see a window pop up with your text with each line beginning with a <p> and ending with a </p>

Now right mouse click between a <p> and </p> on one of those bottom lines and select Paste

 


ReplyQuote
Page 5 / 7