Notifications
Clear all

Questions about changing to array from buttons

Page 1 / 8

GAM
 GAM
(@gam)
Trusted Member
Joined: 11 months ago
Posts: 58
Topic starter  

Hi All, I have been working on a project for some time to operate a stepper to align blast gates for dust collection in wood shop.  I have 2 codes below,  the top one (couldn't figure out how to post them separately)  works as I want it to, but using buttons instead of current sensor to check the operation.  As it is the stepper moves to the next selected degrees and takes the shortest route CW or CCW, and if the same button is pressed twice it remains in place.  

You will notice that some of the code is in RED as these sections will be remove and replace with the 2nd code list below the 1st, of course some of that will be in the declaration part and setup as well.  I only showed the code that would change or be added.  On the very bottom you will see the following code. 

stepper.rotate (stepper_positions[i]); //move stepper to degrees noted in array
} // need to make this angle = to na (new angle)

This is the stepper position (degrees) from the array called for that specific Analog
sensor. I need that degree to = na (new angle) for the rest of the code to use. I'm a little lost here as how to do this. Can I just make the statement read  ....(stepper position[i]); = na?

Thanks for any help or thoughts.

Greg

[code]
/*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 stsytem should still operate the same.

*/

// defines pins numbers
const int stepPin = 3;
const int dirPin = 2;
const int enPin = 8;
const int vacPin = 4; //to led to 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;

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

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

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

// set buttons as inputs, using buttons instead of ACS712 for now for testing
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
}

void loop() {
int n;
// Assign button degrees
if ( digitalRead(A0) == HIGH) {
na = 0;
}
else if ( digitalRead(A1) == HIGH) {
na = 45;
}
else if ( digitalRead(A2) == HIGH) {
na = 225;
}
else if ( digitalRead(A3) == HIGH) {
na = 270;
}

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

//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 IS THE NEW CODE TOE INSERTED FOR THE ARRAY
[/code]

#define SENSOR_N 8
uint8_tsensor_pins[SENSOR_N] = {A0,A1,A2,A3,A4,A5,A6,A7,}
uint16_tsensor_thresholds[SENSOR_N] = {570,570,570,570,570,570,570,570,}
uint8_tstepper_positions[SENSOR_N] = {00,45,90,135,180,225,270,315}

#define CURRENT_SAMPLE_PERIOD 200  //millis secends to sample current reading
int analogValue = 0;  // stores ADC value read
unsignedlong stopwatch = 0;  // track elapsed time

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

voidsetup() {
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 3 outputs
digitalWrite(enPin, LOW);
digitalWrite(dirPin, HIGH);
digitalWrite(vacPin, LOW); //just add this for vac

}
 
 
voidloop() {
   //this Step tru sensors
for (uint8_t i=0;i<SENSOR_N;i++) {
     analogValue + 0;  //initalize analog Valur to know state
     stopwatch = millis();  //store current time since program started exc

while (millis() - stopwatch < CURRENT_SAMPLE_PERIOD) {
     analogValue = max(analogValue, analogRead(sensor_pins[i]));
Serial.println(analogValue);
   }

if (analogValue > sensor_thresholds[i]) {
stepper.enable();  //energize coils
stepper.rotate (stepper_positions[i]);  //move stepper to degrees noted in array
   }    // need to make this angle = to na (new angle)

Quote
Topic Tags
robotBuilder
(@robotbuilder)
Noble Member
Joined: 2 years ago
Posts: 1004
 

@gam

If using Arduino IDE use right mouse button to Select All and then right mouse button to Copy as HTML if you want it looking nice and retain indentations.

Can't compile the second piece.

 

 

/*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 stsytem should still operate the same.

*/

// defines pins numbers
const int stepPin = 3;
const int dirPin = 2;
const int enPin = 8;
const int vacPin = 4; //to led to 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;

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

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

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

//==================================================================================
// start of code to be removed and replaced
//==================================================================================
  // set buttons as inputs, using buttons instead of ACS712 for now for testing
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
}

void loop() {
  int n;
  // Assign button degrees
  if ( digitalRead(A0) == HIGH) {
    na = 0;
  }
  else if ( digitalRead(A1) == HIGH) {
    na = 45;
  }
  else if ( digitalRead(A2) == HIGH) {
    na = 225;
  }
  else if ( digitalRead(A3) == HIGH) {
    na = 270;
  }

//==========================================================================================
// end of code to be removed and replaced
//==========================================================================================

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

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

<sensor_n;i++) {="" analogvalue="" +="" 0;="" initalize="" analog="" valur="" to="" know="" state="" stopwatch="millis();" store="" current="" time="" since="" program="" started="" exc="" while="" (millis()="" -="" <="" current_sample_period)="" analogread(sensor_pins[i]));="" serial.println(analogvalue);="" }="" if="" (analogvalue=""></sensor_n;i++)>  
This post was modified 8 months ago 4 times by robotBuilder

ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 2 years ago
Posts: 1004
 

Second piece,

 

 

#define SENSOR_N 8
uint8_tsensor_pins[SENSOR_N] = {A0,A1,A2,A3,A4,A5,A6,A7,}
uint16_tsensor_thresholds[SENSOR_N] = {570,570,570,570,570,570,570,570,}
uint8_tstepper_positions[SENSOR_N] = {00,45,90,135,180,225,270,315}
#define CURRENT_SAMPLE_PERIOD 200  //millis secends to sample current reading
int analogValue = 0;  // stores ADC value read
unsignedlong stopwatch = 0;  // track elapsed time

int ca = 0;  //currentAngle
int na = 0;  //angle
int stepPerAngle = 5 / 9; // full step = 1.8 or could have used "*1.8"
int   numstep;
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 3 outputs
  digitalWrite(enPin, LOW);
  digitalWrite(dirPin, HIGH);
  digitalWrite(vacPin, LOW); //just add this for vac
}
 
 
voidloop() {
  //this Step tru sensors
  for (uint8_t i=0;i<SENSOR_N;i++) {
    analogValue + 0;  //initalize analog Valur to know state
    stopwatch = millis();  //store current time since program started exc
    
    while (millis() - stopwatch < CURRENT_SAMPLE_PERIOD) {
      analogValue = max(analogValue, analogRead(sensor_pins[i]));
    Serial.println(analogValue);
    }
    
    if (analogValue > sensor_thresholds[i]) {
      stepper.enable();  //energize coils
      stepper.rotate (stepper_positions[i]);  //move stepper to degrees noted in array
   }    // need to make this angle = to na (new angle)

 

 


ReplyQuote
GAM
 GAM
(@gam)
Trusted Member
Joined: 11 months ago
Posts: 58
Topic starter  

@robotbuilder

Thanks for the reply,  I tried to put the code in the way that the video said to do it.  Not sure why it didn't come out right.  The second part was only what I was going to insert,  not a complete sketch.   I did use Pro and was fairly pleased with it.  Not as confusing as PlatformIO  but with some of the features. 

Thanks for any help

Greg


ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 2 years ago
Posts: 1004
 

@gam

Yes I realised you wanted to insert the second piece of code.
It wouldn't compile for other reasons,
uint8_tsensor_pins[SENSOR_N]
should have been,
uint8_t sensor_pins[SENSOR_N]

And it appears to be lacking a stepper motor object?
'stepper' was not declared in this scope

It might also help any coder to see the 'big' picture like the hardware setup?

I haven't used stepper motors so I don't really have the experience needed to really help.

 

 

/*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 stsytem should still operate the same.*/

//=============================================================

#define SENSOR_N 8
#define CURRENT_SAMPLE_PERIOD 200  //millis secends to sample current reading

uint8_t sensor_pins[SENSOR_N] = {A0,A1,A2,A3,A4,A5,A6,A7};
uint16_t sensor_thresholds[SENSOR_N] = {570,570,570,570,570,570,570,570};
uint8_t stepper_positions[SENSOR_N] = {00,45,90,135,180,225,270,315};


int analogValue = 0;  // stores ADC value read
unsigned long stopwatch = 0;  // track elapsed time

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

//=============================================================



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

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

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

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

  // set buttons as inputs, using buttons instead of ACS712 for now for testing
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
}

void loop() {

  //===============================================================================
  //this Step tru sensors
  for (uint8_t i = 0; i < SENSOR_N; i++) {
    analogValue + 0;  //initalize analog Valur to know state
    stopwatch = millis();  //store current time since program started exc
    
    while (millis() - stopwatch < CURRENT_SAMPLE_PERIOD) {
      analogValue = max(analogValue, analogRead(sensor_pins[i]));
    Serial.println(analogValue);
    }
    
    if (analogValue > sensor_thresholds[i]) {
      stepper.enable();  //energize coils
      stepper.rotate (stepper_positions[i]);  //move stepper to degrees noted in array
   }    // need to make this angle = to na (new angle)
  //===============================================================================
  
  int n;
  // Assign button degrees
  if ( digitalRead(A0) == HIGH) {
    na = 0;
  }
  else if ( digitalRead(A1) == HIGH) {
    na = 45;
  }
  else if ( digitalRead(A2) == HIGH) {
    na = 225;
  }
  else if ( digitalRead(A3) == HIGH) {
    na = 270;
  }

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

    //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 8 months ago by robotBuilder

ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 2 years ago
Posts: 1004
 

@gam

If only I could go back and edit a post after an hour!

uint8_tsensor_pins[SENSOR_N]
should have been,
uint8_t   sensor_pins[SENSOR_N]

This post was modified 8 months ago 2 times by robotBuilder

ReplyQuote
GAM
 GAM
(@gam)
Trusted Member
Joined: 11 months ago
Posts: 58
Topic starter  

@robotbuilder,   Thanks, looks like I missed typed that, was correct on original. I see a few others as well.  I guess at this point I should just insert it and see what happens (errors I get).  I think for sure I need to understand my original question.

 On the very bottom you will see the following code. 

stepper.rotate (stepper_positions[i]); //move stepper to degrees noted in array
} // need to make this angle = to na (new angle)

This is the stepper position (degrees) from the array called for that specific Analog
sensor. I need that degree to = na (new angle) for the rest of the code to use. I'm a little lost here as how to do this. Can I just make the statement read  ....(stepper position[i]); = na?   

The picture below is the basic plan, only showing 3 sensors and some future added options for after I get the first part working.  

image

This is a link to video of the first code working

https://onedrive.live.com/?cid=C48586446857BDEE&id=C48586446857BDEE%2117369&parId=C48586446857BDEE%21290&o=OneUp

 

Again, Thanks for your help

Greg


ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 2 years ago
Posts: 1004
 

@gam

Again, Thanks for your help

As I wrote in the first post, I haven't used stepper motors so I don't really have the experience needed to really help.  I mainly responded to suggest a better way to post source code. 

If there is anyone else who might be able to help the clearer the project is explained the more likely they will know what to do coding wise.

The link failed.
This item might not exist or is no longer available.
This item might have been deleted, expired or you might not have permission to view it. Contact the owner of this item for more information.

Of course like most error messages they say do x without saying how to do x.
I have no patience or perseverance for such nonsense.

[Go to my OneDrive]

No idea what that is and have no intention of pressing any buttons to find out.

 

 


ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 2 years ago
Posts: 1004
 

@gam
I couldn't even visualize what you were doing but found some examples online such as,

Not sure where your "analog sensors" are or the role they play.
Probably a complete layout of the actual physical structure would help anyone able to help with the code.

 


ReplyQuote
GAM
 GAM
(@gam)
Trusted Member
Joined: 11 months ago
Posts: 58
Topic starter  

@robotbuilder   Sorry the link didn't work.  I tried to attach it but was to large and zipping it didn't change it much.  Were you able to see the pasted fritz picture? 

 I have watched that video before and best I remember he is using a keypad to select the blast gate,  where I'm using current sensors to detect which tool is being used and that opens the blast gate.  Might watch that again see if I can find help there.  

  Not sure how I can make it clearer, but will give it ago if I make any progress 

Thanks for your efforts and suggestions.


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

@gam

I'm probably not understanding your problem properly, but if you can make it all work by pressing buttons, then maybe all you need to do, in code, is to make the same button pin go high or low (whatever it is) when a signal is received from your current sensor.  So the code is much the same,  and perhaps you could also keep your buttons as well.  This will give you the ability to either press a button or let the current sensor trigger the same pin to signal the blast gates.   However, I'm quite likely to be talking tosh.


ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 2 years ago
Posts: 1004
 

@gam

Not sure how I can make it clearer, but will give it ago if I make any progress.

Verbal descriptions are difficult for me as I tend to visualize rather than verbalize a description. Yes I can see your circuit but not what the stepper motor is actually doing physically.

Yes I understood you were not using a keypad.  The utube example was just to try and figure out what problem you were attempting to solve.

So instead of individual doors opening as in the utube using servo motors I am imagining a rotating thingy selecting each pipe being controlled by a single stepper motor.

Your actual question was:
"stepper.rotate (stepper_positions[i]); //move stepper to degrees noted in array

// need to make this angle = to na (new angle)

This is the stepper position (degrees) from the array called for that specific Analog
sensor. I need that degree to = na (new angle) for the rest of the code to use. I'm a little lost here as how to do this.

Can I just make the statement read ....(stepper position[i]); = na?"

My thought was, have you tried it?  Of course the semi colon would go after the na

 

 

This post was modified 8 months ago 3 times by robotBuilder

ReplyQuote
MadMisha
(@madmisha)
Reputable Member
Joined: 2 years ago
Posts: 319
 

@gam

I have worked in a few theatre shops and have never thought about how the dust collection systems work. They just did. Although I think most of the ones I worked with sensed vibration to turn on the system.

 

You want to move a servo to an angle to connect the correct pipe to the tools that is being used and if it is already on that tool to stay were it is? That's at least the impression I am getting but that doesn't seem to make sense in a design sense. Or are you opening the gates to the tools that are in use but if you have multiple gates then dividing how much each are open?

 

Is the program being reset or can you rely on a variable that is stored?


ReplyQuote
MadMisha
(@madmisha)
Reputable Member
Joined: 2 years ago
Posts: 319
 

Ok, I went back and looked at the original post and in it you said you just needed a variable to match the degrees in array?

 

To do just that try:

 

na = stepper_position[i];

 

This will make na the value of the stepper_position array data in that position of variable i.

 

That is unless I got that backwards, I don't know why you would want to change the position in the array. But, if that is the case then:

 

stepper_position[i] = na;

 

 

Also, as a side note, millis has some issues with math functions and has trouble with converting to int. It will also count up to a max number(I don't remember what off hand) and reset and that could cause problems if you are using it as a stopwatch. You might want tp look into the library called chronos. It uses millis in a way that is known to not have errors but accounts for the reset and has a start and stop function for a stopwatch.


ReplyQuote
GAM
 GAM
(@gam)
Trusted Member
Joined: 11 months ago
Posts: 58
Topic starter  

@byron,   Thanks for the reply,  I was working on this yesterday and it occurred to me that your suggestion might be the answer,

Remove the stepper position array part and just have the Analog pin go high,  Then keep the button setup part where I assign the degrees for each analog input.  And yes I think the button will still work if needed.  I fact I need a few buttons for when I just use the hose with no tool for cleanup.  


ReplyQuote
Page 1 / 8