Notifications
Clear all

Questions about changing to array from buttons

111 Posts
5 Users
6 Likes
11.1 K Views
 GAM
(@gam)
Member
Joined: 3 years ago
Posts: 58
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)
Member
Joined: 5 years ago
Posts: 2037
 

   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
 

   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
 

   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
 

@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.

 


   
ReplyQuote
 GAM
(@gam)
Member
Joined: 3 years ago
Posts: 58
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)
Member
Joined: 5 years ago
Posts: 2037
 

   
ReplyQuote
 GAM
(@gam)
Member
Joined: 3 years ago
Posts: 58
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)
Member
Joined: 5 years ago
Posts: 2037
 

@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

   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
 

@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)
Member
Joined: 3 years ago
Posts: 58
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)
Member
Joined: 3 years ago
Posts: 58
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)
Member
Joined: 3 years ago
Posts: 58
Topic starter  

   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1112
 

@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)
Member
Joined: 5 years ago
Posts: 2037
 

@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 / 8