Stepper Motor Step Count using For Loop

Hi all,

I am new to Arduino. I am hoping someone can help with some simple code.

I have a stepper motor that feeds a label roll until the next label hits a laser beam (at which point the motor stops).

Code for this as follows:

void loop() {
// put your main code here, to run repeatedly:

short DetectFL = analogRead(FLReceiver);

if(DetectFL > 900){

digitalWrite(dirPinX,HIGH);
for(int x = 0; x < 2; x++) {
digitalWrite(stepPinX,HIGH);
delayMicroseconds(100);
digitalWrite(stepPinX,LOW);
delayMicroseconds(100);

How do I add a variable that counts the number of steps the motor takes in the above process? Essentially, how many times the for loop function occurs?

Help would be greatly appreciated!

How many steps should the motor make for a feed?
This number must be used in your for loop

Hi Rudolf

The number of steps are dynamic. The label spool moves by a stepper motor winding up the excess paper after the label is removed. So at the beginning of the label spool, more steps are required to get the label to the laser sensor but as I move through the roll of labels, the powered shaft diameter will increase (as more paper is wound onto it) and less steps will be required

Hope this explanation makes sense

  static unsigned long stepsTaken = 0;
  digitalWrite(dirPinX,HIGH);
  for(int x = 0; x < 2; x++) 
  {
    stepsTaken++;
    digitalWrite(stepPinX,HIGH);
    delayMicroseconds(100);
    digitalWrite(stepPinX,LOW);
    delayMicroseconds(100);
  }

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.