Resetting stepper motor count each full rotation

Hi,

I am trying to figure how to reset the step count after one full rotation. I want to be able to do this, as eventually i will have 2+ motors starting at 0 steps and than all rotate at different speeds until they all return back to 0.

I’m currently using the Accelstepper library and can get it to count up to 200 steps and reset to 0 but i am failing to figure out what i’m doing wrong to allow it to start counting again.

All my searching of the internet the past couple of days has been unsuccessful, maybe because i’m searching for the wrong term. Any help would be much appreciated.

#include <AccelStepper.h>
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
int pos;

void setup()
{  
   stepper.setMaxSpeed(1000);
   stepper.setSpeed(30);  
   Serial.begin (9600);   
    
}
void loop()
{
   
  stepper.runSpeed();
  
   long thisPosition = stepper.currentPosition();
   Serial.println(thisPosition);
  
   if (thisPosition = 199) {
    stepper.setCurrentPosition(0);}
   }

"thisPosition = 199"

Try '==' instead of '='

Yes tried that.

When i view the serial monitor it counts up to 200 like expected and than resets to 0 but than continually prints .

Bails:
I'm currently using the Accelstepper library and can get it to count up to 200 steps and reset to 0 but i am failing to figure out what i'm doing wrong to allow it to start counting again.

I don't understand what you want to achieve. If you just want a motor to move one revolution then tell AccelStepper to move 200 steps.

The AccelStepper library is not really intended for counting individual steps - unless you just instruct it to move one step at a time and control the interval between steps yourself.

What stepper motor driver are you using?

...R
Stepper Motor Basics
Simple Stepper Code

Only print when position is greater than 0. if( thisPosition > 0 ) SerialPrintln( thisPosition );

Robin2:
I don’t understand what you want to achieve. If you just want a motor to move one revolution then tell AccelStepper to move 200 steps.

The AccelStepper library is not really intended for counting individual steps - unless you just instruct it to move one step at a time and control the interval between steps yourself.

What stepper motor driver are you using?

…R
Stepper Motor Basics
Simple Stepper Code

Sorry, probably not explaining well enough or over complicating what i want to achieve.
I want to count full rotations accurately so i can have 2+ motors rotating at different speeds say, motor one at 1 RPM and motor two at 2 RPM (1:2 ratio) to stop when they both return to the exact same position at the same time.

My thought process to complete this was to count the steps (200 steps and resetting the count each rotation) and have them stop when they meet up at 0 again.

I have read your stepper guides which have been very hand in the past Thanks, (probably have all the information to my question but I might be over complicating things :-[ )

I will be using dvr 8825 that i have laying around but, for now i’m using a basic L298n to test my code.

As pointed out by @Southpark

if (thisPosition = 199)

is not what you intended.
The operators '=' and '==' are not the same. "if(thisPosition = 199)" does not mean "If this Position equals 199".

You also need to look at all the methods of the class AccelStepper.
Where in your sketch are you telling the stepper where you want it to go.

Bails:
I want to count full rotations accurately so i can have 2+ motors rotating at different speeds say, motor one at 1 RPM and motor two at 2 RPM (1:2 ratio) to stop when they both return to the exact same position at the same time.

Rather than try to figure that out when it happens I reckon it would be easier to plan up front to make it happen.

For example, figure out the interval between steps for the motor that will make the largest number of steps (or the longest time) - to illustrate, suppose it is to make 400 steps in 60 seconds then the interval between steps would be 150 millisecs. If the other motor is to make 200 steps in the same time then its interval between steps would be 300 millisecs. Then set up a counter for the motor with the larger number of steps and step both motors at their respective intervals until the count reaches 400.

…R

Thanks all,

Ill will approach this differently based on your suggestions.