# Question about accumulative counting while working with stepper motors

Hi. I had a quick question. The short is i have a radial arm saw that goes back and forth along a piece of metal slowly cutting into it. I have a separate stepper motor that activates at the end of each pass and lowers the saw for it’s next cut. I’ll post my code below, But what i’m wanting to do for example, the stepper needs to make a total of 10000 steps before it will cut all the way thru the metal. At the beginning of each pass, on average the stepper moves 200-400 steps, this is set randomly by me, and i’m wanting to keep a accumulative tally of the steps taken each time the stepper is activated. and when the tally reaches 10000 it will stop.

I’ve counted by 1’s before and use them to keep track on how many passes i’ve made, and have counted individual steps. But i’ve never tried making an accumulative tally, and then comparing it to another value to proceed. I’ve inserted the new code i’ve written, but won’t get a chance to actually see if it works until tonight, and i’m unsure if i’ve did it right and was wanting to get a little input. Besides that everything else works perfectly

thanks

``````#include <Stepper.h>

Stepper stepper1(200, 6, 5);
Stepper stepper2(200, 13, 12);

const int stepPin1 = 5;     // stepper1 step pin
const int dirPin1  = 6;     // stepper1 direction pin
const int stepPin2 = 12;    // stepper2 step pin
const int dirPin2 = 13;     // stepper2 direction pin

const int relay =7;         // relay enable pin

const byte front_Switch = 8;    // frontStep switch
const byte back_Switch = 9;     // backStep switch
const byte start_Switch = 2;    // cutting switch
const byte feed_Switch = 3;    // feed switch

byte switch1State;             // cutting switch state
byte switch2State;             // frontStep switch state
byte switch3State;             // start Switch state
byte switch4State;             // feed switch state

int  stepSpeed;
int  feedStep;

int long feedCounter;
int long feedMax = 10000;

void setup() {

pinMode(relay, OUTPUT);                   // stepper2 enable output
pinMode( start_Switch, INPUT_PULLUP);     // start switch mode
pinMode( front_Switch, INPUT_PULLUP);     // frontStep switch mode
pinMode( back_Switch, INPUT_PULLUP);      // backStep switch mode
pinMode( feed_Switch, INPUT_PULLUP);     // feed switch mode
digitalWrite(relay, LOW);                // relay enable = low, off

while (digitalRead(feed_Switch)){

int feedReading = analogRead(A1);                          // analog signal for feed stepper
int feedStep = map(feedReading, 0, 1023, -200, 200);

if (feedStep > -200) {
stepper2.setSpeed(20);
stepper2.step(-feedStep);
delay(5);
}
}

while (digitalRead(start_Switch)){  }

while (digitalRead(front_Switch)) {         //CCW rotation

feedCounter=0;

int sensorReading = analogRead(A0);
int stepSpeed = map(sensorReading, 0, 1023, 20, 1600);

if (stepSpeed > 0) {
stepper1.setSpeed(stepSpeed);
}

stepper1.step(-20);
delayMicroseconds(20);
}
}

void loop() {

checkButtons();              // read buttons status
frontStep();                 // step  forward
backStep();                  // step  backward
stopStep();

}

void checkButtons() {

switch1State = digitalRead(front_Switch);  // reading frontStep button
switch2State = digitalRead(back_Switch);  //reading backStep button
switch3State = digitalRead(start_Switch);   // reading start button
switch4State = digitalRead(feed_Switch);    // reading feed switch

}

void frontStep(){                          // front cutting

if (switch1State == LOW){        // if front_Switch button activated

int feedReading = analogRead(A1);                          // analog signal for feed stepper
int feedStep = map(feedReading, 0, 1023, 75, 600);

//NEW CODE HERE

feedStep=feedCounter;
feedCounter++;

if  (feedCounter <= feedMax){

stepper2.setSpeed(20);
stepper2.step(feedStep);
}

else (stopStep());

//RESUME CODE HERE

while(digitalRead(back_Switch)) {

int sensorReading= analogRead(A0);
int stepSpeed = map(sensorReading, 0, 1023, 20, 1600);

if (stepSpeed > 0) {
stepper1.setSpeed(stepSpeed);
}

digitalWrite(relay, HIGH);
stepper1.step(20);
delayMicroseconds(20);
}
}
}

void backStep(){         //CCW rotation step counting

if (switch2State==LOW)  {     // if back_Switch button activated

int feedReading = analogRead(A1);                          // analog signal for feed stepper
int feedStep = map(feedReading, 0, 1023, 75, 600);

stepper2.setSpeed(40);
stepper2.step(-75);
delay(750);
stepper2.setSpeed(8);
stepper2.step(feedStep);

//NEW CODE HERE
feedStep = feedCounter;
feedCounter++;

if  (feedCounter <= feedMax){

stepper2.setSpeed(20);
stepper2.step(feedStep);
}

else (stopStep());

//RESUME CODE HERE

while(digitalRead(front_Switch)) {

int sensorReading= analogRead(A0);
int stepSpeed = map(sensorReading, 0, 1023, 20, 1600);

if (stepSpeed > 0) {
stepper1.setSpeed(stepSpeed);
}

digitalWrite(relay, HIGH);
stepper1.step(-20);
delayMicroseconds(20);
}

}
}

void stopStep(){

digitalWrite(relay,LOW);

}
``````

Maybe I don't fully understand but just use a variable to store the steps and just add the number of steps to the variable with the number of steps for each pass.

initialize stepvar to zero pass#1: add steps to stepvar i is stepvar 10000 ? if yes stop else continue

pass#2: add steps to stepvar

and so on...

Yeah, it sounds like your getting what i’m asking. because that’s basically the code i written… but I was unsure if the code i wrote is correct for what i’m attempting to do. I’ll break it down and ignore any code that’s not relevant

On the top i have 2 variables listed.

int long feedCounter;
int long feedMax = 10000;

in the setup, after i manually move the feedscrew where it needs to be to start i declare

feedCounter = 0;

In the first function, frontStep(), it reads the pot and creates the feedStep and moves the stepper to the desired position, and the code i made to add and compare the new variables is:

feedStep=feedCounter; //unsure if this is the correct code to be used
feedCounter++; //unsure if this is the correct code to be used

if (feedCounter <= feedMax){ // thinking it will work, but unsure

//Start stepping and cutting

}

else (stopStep()){

//stopStep() is to turn off the relay, and do nothing with the steppers.
}

If it continues, it will automatically hit the next function switch, and start over.

thanks

I think it should be

because feedstep is the increment each pass, right ? so feedCounter just keeps track of the total feedsteps.

and when feedCount is equal to 10000 then you should be done. If I am following you.

`````` feedCounter = feedCounter + feedstep  ;
``````

wildcat99s: On the top i have 2 variables listed.

int long feedCounter; int long feedMax = 10000;

in the setup, after i manually move the feedscrew where it needs to be to start i declare

feedCounter = 0;

I would write that as

``````long feedCounter;
``````

I'm not a C++ expert and I don't know how the compiler would treat the combination of int long

Also, if you declare those variables within setup() they will not be available to the rest of the program. They should be declared before setup().

...R

Thanks for the replies. Last night i wrote a reply but it didn’t go through for some reason, but i was just saying that the code i tried didn’t work.

So i’ve modified the code with both of your suggestions, which looks like it should work, but i won’t get the opportunity to check it until tonight when i get home. it sucks not having an extra arduino setup so i can test the code to see if it works.

Below is my modified code. and i’m only unsure about one line of code now.

In both frontstep() and backstep() I changed the feed counter line to: feedCounter = feedStep + feedCounter; // feedCounter tally

But do i still need to have feedCounter++; below it ? Or would the above line automatically save and add it with the code line above it?

``````#include <Stepper.h>

Stepper stepper1(200, 6, 5);
Stepper stepper2(200, 13, 12);

const int stepPin1 = 5;     // stepper1 step pin
const int dirPin1  = 6;     // stepper1 direction pin
const int stepPin2 = 12;    // stepper2 step pin
const int dirPin2 = 13;     // stepper2 direction pin

const int relay =7;         // relay enable pin

const byte front_Switch = 8;    // frontStep switch
const byte back_Switch = 9;     // backStep switch
const byte start_Switch = 2;    // cutting switch
const byte feed_Switch = 3;    // feed switch

byte switch1State;             // cutting switch state
byte switch2State;             // frontStep switch state
byte switch3State;             // start Switch state
byte switch4State;             // feed switch state

int  stepSpeed;                // lead screw stepper speed
int  feedStep;                 // feed screw step count
int passCount;                 //  how many passes taken

long feedCounter;              // tally of steps taken by feed screw
long feedMax = 10000;          // max steps to be taken to cut through metal

void setup() {
Serial.begin(9600);
pinMode(relay, OUTPUT);                   // stepper2 enable output
pinMode( start_Switch, INPUT_PULLUP);     // start switch mode
pinMode( front_Switch, INPUT_PULLUP);     // frontStep switch mode
pinMode( back_Switch, INPUT_PULLUP);      // backStep switch mode
pinMode( feed_Switch, INPUT_PULLUP);     // feed switch mode
digitalWrite(relay, LOW);                // relay enable = low, off

while (digitalRead(feed_Switch)){

int feedReading = analogRead(A1);                          // manual setup for feedscrew before starting
int feedStep = map(feedReading, 0, 1023, -200, 200);

if (feedStep > -200) {
stepper2.setSpeed(40);
stepper2.step(-feedStep);
delay(1);
}
}

while (digitalRead(start_Switch)){  }        //  do nothing until button is pressed

while (digitalRead(front_Switch)) {         //CCW rotation

int sensorReading = analogRead(A0);
int stepSpeed = map(sensorReading, 0, 1023, 20, 1600);       // lead screw speed code

if (stepSpeed > 0) {                      // lead screw set speed
stepper1.setSpeed(stepSpeed);
}

stepper1.step(-20);                   // stepper moving towards starting position
delayMicroseconds(20);
}

feedCounter = 0;                     // reset feed counter
}

void loop() {

checkButtons();              // read buttons status
frontStep();                 // step  forward
backStep();                  // step  backward
stopStep();                   // stop saw and steppers

}

void checkButtons() {

switch1State = digitalRead(front_Switch);  // reading frontStep button
switch2State = digitalRead(back_Switch);  //reading backStep button
switch3State = digitalRead(start_Switch);   // reading start button
switch4State = digitalRead(feed_Switch);    // reading feed switch

}

void frontStep(){                          // front cutting

if (switch1State == LOW){        // if front_Switch button activated

passCount++;                        //  passCount counter

Serial.print("Pass ");
Serial.println(passCount);

int feedReading = analogRead(A1);                          // feed screw adjustment for each pass
int feedStep = map(feedReading, 0, 1023, 75, 600);

feedCounter = feedStep + feedCounter;                           //  feedCounter tally
feedCounter++;

Serial.print("Feed Steps Taken ");
Serial.println(feedCounter);

if  (feedCounter < feedMax){                   // if feedCounter is lower than the feedmax, continue

stepper2.setSpeed(25);                   // lower saw into next position
stepper2.step(feedStep);
}

else (stopStep());                      // if feedMax is reached, go to stopStep()

while(digitalRead(back_Switch)) {        // awaiting next limit switch

int sensorReading= analogRead(A0);                        // lead screw speed code
int stepSpeed = map(sensorReading, 0, 1023, 20, 1600);

if (stepSpeed > 0) {                    // set lead screw speed
stepper1.setSpeed(stepSpeed);
}

digitalWrite(relay, HIGH);            //cutting while moving towards limit switch
stepper1.step(20);
delayMicroseconds(20);
}
}

}

void backStep(){         //CCW rotation stepping

if (switch2State==LOW)  {     // if back_Switch button activated

passCount++;             // passes taken

Serial.print("Pass ");
Serial.println(passCount);

int feedReading = analogRead(A1);                         // feed screw adjustment for each pass
int feedStep = map(feedReading, 0, 1023, 75, 600);

stepper2.setSpeed(40);                                  // at the end of the pass, raise saw and then lower into position
stepper2.step(-70);
delay(750);
stepper2.setSpeed(10);
stepper2.step(feedStep);

feedCounter = feedStep + feedCounter - 70;                             // feedCounter tally
feedCounter++;

Serial.print("Feed Steps Taken ");
Serial.println(feedCounter);

if  (feedCounter < feedMax){                     // if feedCounter is lower than the feedmax, continue

stepper2.setSpeed(20);
stepper2.step(feedStep);                    // lower saw into next position
}

else (stopStep());

while(digitalRead(front_Switch)) {

int sensorReading= analogRead(A0);                         // lead screw speed code
int stepSpeed = map(sensorReading, 0, 1023, 20, 1600);

if (stepSpeed > 0) {                            // leadscrew set speed
stepper1.setSpeed(stepSpeed);
}

digitalWrite(relay, HIGH);                  // cutting while stepping
stepper1.step(-20);
delayMicroseconds(20);
}

}

}

void stopStep(){

digitalWrite(relay,LOW);                   //  turn off saw
stepper2.setSpeed(100);
stepper2.step(-feedCounter);               // return feed screw to original starting position, no lead screw movement

}
``````

I don't understand this in your frontStep() function

``````  int feedReading = analogRead(A1);                          // feed screw adjustment for each pass
int feedStep = map(feedReading, 0, 1023, 75, 600);

feedCounter = feedStep + feedCounter;
``````

I had the impression from your earlier description that at the start you manually adjust the stepper motor so the saw is touching (or nearly touching) the workpiece. And following that the blade would descend by a fixed amount each traverse of the saw until the maximum depth is reached.

If my understanding is correct I don't know why you are updating the value of feedStep. Also, what is connected to A1 that is providing the input.

Having said all that I agree that the line feedCounter++; is probably not needed. I'm assuming that the motor needs to move more than a single step to bring the saw down for the next cut.

...R

Yes, you are correct. I adjust the feedscrew to where the blade just touches the piece and then i start cutting. and it will continue until the maximum depth is cut.

I have a pot connected to the A1 input that allows me to adjust the feedscrew mid cut, but it doesn't take effect until i have already reached the next limit switch. This was done like this to allow me to test different depth cuts at different traverse speeds. Which are both Serial printed so i can determine what works the best. There are several benefits of doing it this way as i can go fast and cut a little, or go slow and cut a lot. Which will come in handy whenever i start cutting different thicknesses of metal or different materials.

that's just one fine tuning options i included to get it cutting perfectly while i figure out it's limits. I have to figure out the fastest and deepest i can cut each pass, and i can adjust accordingly.

For the longest time i had problems at the beginning of the backStep() where the saw would eat more than it handle and would either bind up the saw, or start bouncing which messed up my cut and ruined a couple blades, but i found out that at the end of that cycle, instead of the feedscrew going straight down to the next position. If i would raise it up just a little, and then lower it down into position it eliminated all of my bouncing problems.

At the moment the feedStep has been set around 350, which is roughly .040" cut, but within the backStep(), it is cutting about 75% of what the frontStep() does. and that's because of rotation of the saw, and the direction of it's going. In frontStep(), and saw is being pulled and doesn't bind any, but in backStep(), the saw is being pushed into the metal and tends to take a meatier cut and can bind or slow if i attempt to cut too deep..... which if it does, the blade tends to flex a little and messes up the cut line.

I found out that if the backStep is slightly higher than the frontStep then i get a better cut. but i planned on seeing how deep i can go and try to eliminate the difference between front and back cutting depth.

so far i've only made one cut though the 5/8 plate x 21" , and it was square, but had a minor.. .050" hump in the middle of the plate, halfway thru the cut. But that was my fault. that was where i had to remove the saw to make a modification, and reinstalled the saw slightly off square, but i've already reground that hump off, and the variable feedStep helped with that.

But within the next day or two, i'll be making another cut to verify it's squareness, and see how well the program works before i actually put it too work cutting out my pieces.

thanks for your help

wildcat99s: I have a pot connected to the A1 input that allows me to adjust the feedscrew mid cut, but it doesn't take effect until i have already reached the next limit switch. This was done like this to allow me to test different depth cuts at different traverse speeds.

Thanks. I guess most of the time the value won't change. From reading the code I had imagined that it would change on every traverse.

...R

Well last night i spent a good amount of time fiddling with my saw and the code and still couldn't get the new changes to take effect. But after thinking about it more, i don't think the code i wanted to use would work very well in the long run for one single fact. I'm using an abrasive blade, which does wear down randomly while i'm cutting, which would mess up my feedscrew counter, but more importantly whenever i have to change the blade, it will effect my counter and would just slow me down in the long run.

So i'm gonna keep tinkering with the code to see if i can get it working for future purposes, or educational purposes at the least. I've been thinking about trying out one of those diamond tipped cutoff saw blades which wouldn't wear down and then the code would be useful. but for the moment, i'm gonna just let it cut for a while and then go from there.

thanks for all your help.

Maybe a spring used for keeping somewhat of a constant tension on the work piece. I think its going to get complicated now that you have to adjust for wear of the wheel. For me I am more comfortable with the electronic side of things rather than mechanical. I think mechanical takes up 75% of the project time.

noweare: Maybe a spring used for keeping somewhat of a constant tension on the work piece. I think its going to get complicated now that you have to adjust for wear of the wheel. For me I am more comfortable with the electronic side of things rather than mechanical. I think mechanical takes up 75% of the project time.

Yeah, the mechanical part of it takes up the most time, but that's what makes it a challenge. I posted this in the gallery section of the forum, and once on another question i asked a couple days ago about the programming. But here's a short video of my saw and plan to make a better video of it and explain it a little. But it already has a tension spring.

Last night i started a new cut and made it halfway thru the plate before stopping for the night... which really sucked because it was cutting beautifully and was cutting faster than it has before. by just fiddling around with it in the past week getting all the little kinks worked out, i expected it to take about 2 hours to make a cut, but i made it halfway through it in about 30 min, and had virtually no wear on the wheel, which surprised me, but also made me happy because it seems to be running straight, but the real test will be the 2nd half of the cut, because once the wheel gets deeper into the groove, any misalignment will be shown and slow things down and start wearing out the wheel faster. but one disadvantage of cutting during the day, is i'm not able to see the sparks on the wheel as well to see exactly where the point of contact is.