Accelstepper counter

hello,
here is my code

#include <AccelStepper.h>

// Definicija motora i pinovi
AccelStepper stepper2(AccelStepper::FULL4WIRE, 4, 5, 6, 7);
AccelStepper stepper1(AccelStepper::FULL4WIRE, 8, 9, 10, 11);
#define home_switch 2 //pin 2 spojen na multiprekidač



// Stepper Travel Variables
long TravelX;  // Used to store the X value entered in the Serial Monitor
int move_finished=1;  // Used to check if move is completed
long initial_homing=-1;  // Used to Home Stepper at startup
int i=0;

void setup() {
   
   Serial.begin(11520);  // Start the Serial monitor with speed of 9600 Bauds
   pinMode(home_switch, INPUT_PULLUP);
   delay(5);  // cekanje da se modul probudi
  stepper1.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper1.setAcceleration(100.0);  // Set Acceleration of Stepper
  while (digitalRead(home_switch))
           {   
            stepper1.moveTo(initial_homing);  // Set the position to move to
            initial_homing--;  // Decrease by 1 for next move if needed
            stepper1.run();  // Start moving the stepper
            delay(5);
           }

  stepper1.setCurrentPosition(0);  // Set the current position as zero for now
  stepper1.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper1.setAcceleration(100.0);  // Set Acceleration of Stepper
  initial_homing=-1;
  

  while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
    stepper1.moveTo(initial_homing);  
    stepper1.run();
    initial_homing++;
    delay(5);
  }
  stepper1.setCurrentPosition(0);
  stepper1.setMaxSpeed(180);      // Set Max Speed of Stepper (Faster for regular movements)
  stepper1.setAcceleration(180);
  delay(1000);
  // Set Acceleration of Stepper
}
 void step1(){
    
    stepper1.setMaxSpeed(180);     
  stepper1.setAcceleration(180);  
  stepper1.moveTo(-153);
  stepper1.run();

  
 
  
  }
  void step2(){
    
    stepper1.setMaxSpeed(180);      // Set Max Speed of Stepper (Faster for regular movements)
     stepper1.setAcceleration(180);  // Set Acceleration of Stepper
    stepper1.moveTo(0);
    stepper1.run();
  
 
  }

void loop(){
  int stepCount = 0;
  if (stepCount <= 5)
   {
     do
       { 
      step1();
      
       } 
      while(stepper1.currentPosition() != -153); 
      delay(1000);  
    
     do
      { 
      step2();
      }
      while(stepper1.currentPosition() != 0);
      delay(1000);
      stepCount++;
   }
   
      if (stepCount == 5);
   {
        stepper1.stop();
        
  }}

The program works well, but i want to limit the movement of the motor to 5 repetition ( move to -153 and back to 0-home position ).
the movements go in infinity… if somebody have time to help me , tnx :slight_smile:

I think this will work and it seems a lot simpler

void loop(){
    static int repeatCount = 0; // note static so the value is remembered
    static int stepperDestination = -153;
    if (repeatCount <= 5) {
        if (stepper1.currentPosition() == 0) {
            delay(1000);
            if (stepperDestination == 0) {
            stepperDestination = -153);
            }
            else {
                stepperDestination = 0;
            }
            stepper1.moveTo(stepperDestination);
            repeatCount++;
        }
    }
    stepper1.run();
}

…R

tnank YOu for reply but this doesn't work, after program found home position, motor move to -153 and stop, nothing else happens

Replace

            if (stepperDestination == 0) {

with

            if (stepper1.distanceToGo() == 0) {

tnank you mark for replay but doesnt work… but i changed my old code with repeat Count, and now work correctly. thank you for help .

#include <AccelStepper.h>

// Definicija motora i pinovi
AccelStepper stepper2(AccelStepper::FULL4WIRE, 4, 5, 6, 7);
AccelStepper stepper1(AccelStepper::FULL4WIRE, 8, 9, 10, 11);
#define home_switch 2 //pin 2 spojen na multiprekidač



// Stepper Travel Variables
long TravelX;  // Used to store the X value entered in the Serial Monitor
int move_finished=1;  // Used to check if move is completed
long initial_homing=-1;  // Used to Home Stepper at startup
int i=0;

void setup() {
   
   Serial.begin(11520);  // Start the Serial monitor with speed of 9600 Bauds
   pinMode(home_switch, INPUT_PULLUP);
   delay(5);  // cekanje da se modul probudi
  stepper1.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper1.setAcceleration(100.0);  // Set Acceleration of Stepper
  while (digitalRead(home_switch))
           {   
            stepper1.moveTo(initial_homing);  // Set the position to move to
            initial_homing--;  // Decrease by 1 for next move if needed
            stepper1.run();  // Start moving the stepper
            delay(5);
           }

  stepper1.setCurrentPosition(0);  // Set the current position as zero for now
  stepper1.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper1.setAcceleration(100.0);  // Set Acceleration of Stepper
  initial_homing=-1;
  

  while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
    stepper1.moveTo(initial_homing);  
    stepper1.run();
    initial_homing++;
    delay(5);
  }
  stepper1.setCurrentPosition(0);
  stepper1.setMaxSpeed(180);      // Set Max Speed of Stepper (Faster for regular movements)
  stepper1.setAcceleration(180);
  delay(1000);
  // Set Acceleration of Stepper
}


void loop()
{
  {
    static int repeatCount = 0; // note static so the value is remembered
    if (repeatCount <= 5)
      do
       { 
      step1();
             } 
      while(stepper1.currentPosition() != -153); 
      delay(1000);  
    
     do
      { 
      step2();
      }
      while(stepper1.currentPosition() != 0);
      delay(1000);
      repeatCount++;
   }}
void step1()
{
                
               {
                 stepper1.setMaxSpeed(180);     
                 stepper1.setAcceleration(180);  
                 stepper1.moveTo(-153);
                 stepper1.run();
               }
            }
  void step2()
  {
                 
             {
               stepper1.setMaxSpeed(180);      // Set Max Speed of Stepper (Faster for regular movements)
               stepper1.setAcceleration(180);  // Set Acceleration of Stepper
               stepper1.moveTo(0);
               stepper1.run();
               {
               
             }        

  }  }

But you understand the original code wasn't testing for the motion having stopped in a correct way?

I didn't look beyond that, so there was prob another little flaw there too.

then how you say original code its not good for correct stoping ? what's is wrong in original code ? how to rebuild second code then to work fine ?

MarkT:
Replace

            if (stepperDestination == 0) {

with

            if (stepper1.distanceToGo() == 0) {

My "stepperDestination" is intended to be a variable, not a call to the AccelStepper library. But I think you were pointing in the right direction - See my next Reply

...R

marinac94:
tnank YOu for reply but this doesn’t work, after program found home position, motor move to -153 and stop, nothing else happens

Have you included any Serial.print() statements in your code to help you figure out what it is doing?

Try this version

void loop(){
    static int repeatCount = 0; // note static so the value is remembered
    static int stepperDestination = -153;
    if (repeatCount <= 5) {
        if (stepper1.distanceToGo() == 0) {
            delay(1000);
            if (stepperDestination == 0) {
            stepperDestination = -153);
            }
            else {
                stepperDestination = 0;
            }
            stepper1.moveTo(stepperDestination);
            repeatCount++;
        }
    }
    stepper1.run();
}

…R

i will try with serial prints . tank you see you later

I updated Reply #8 while you were sending Reply #9

...R

works super :smiley: counts every move in particular, but work :smiley: i need only put <=10 and this is 5moves left and 5 right, thank you roben you are boss :smiley: