accel stepper library

how do i program in accelstepper library to count the steps that taken in 28byj48 motor

how do i program in accelstepper library to count the steps that taken in 28byj48 motor

No need to. It does that already. You just need to use the appropriate method to get the data.

thank you for your reply can you please tell me how to use these methods . i just want know the steps that taken while button press or sensor read

my code

void loop(){

int sensor=digitalRead(2);

stepper1.moveTo(8000);

if (sensor==LOW){
delay(3000);

stepper1.move(8000-stepper1.distanceToGo());

delay(1000);

this code is not working

You can't use big delays with the AccelStepper library. You need to call its run() method thousands of times per second, so that it can issue the individual steps as required.

If you aren't familiar with doing timing without delay, look in your Examples folder for the BlinkWithoutDelay sketch.

In addition to what @MorganS has said you need to post a complete program and use the code button </> so your code looks like this and is easy to copy to a text editor.

The demo Several Things at a Time is an extended example of BWoD.

…R

The main purpose of the AccelStepper library is to allow two or more stepper motors to move at the same time. Why are you using it for one stepper motor?

PaulS:
The main purpose of the AccelStepper library is to allow two or more stepper motors to move at the same time.

Paul, I don't think that is correct. It's main purpose is to provide for acceleration and deceleration of stepper motors.

It does allow several motors to move at the same time but it is not suitable for synchronizing their movements as would be required in a CNC application.

It is also convenient for controlling motor drivers that need more than just step and direction signals.

...R

thank you guys here is my code . after “if” statement methods are not working

#include <AccelStepper.h>
#define HALFSTEP 8


#define motorPin1  3   
#define motorPin2  4     
#define motorPin3  5     
#define motorPin4  6     
#define motorPin5  8     
#define motorPin6  9     
#define motorPin7  10    
#define motorPin8  11    


AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);

void setup() {
  pinMode(2, INPUT_PULLUP);
  stepper1.setMaxSpeed(2000.0);
  stepper1.setAcceleration(150.0);
  stepper1.setSpeed(200);
  stepper2.setMaxSpeed(2000.0);
  stepper2.setAcceleration(150.0);
  stepper2.setSpeed(200);
}
void loop(){
  int sensor=digitalRead(2);
  stepper1.moveTo(7000);
  stepper2.moveTo(7000);

  if (sensor==LOW){
    stepper1.move(7000-stepper1.distanceToGo());
    stepper2.move(7000-stepper1.distanceToGo());
    
  }
  
  stepper1.run();
  stepper2.run();
  }

What sensor is connected to Pin 2

What is this code intended to do?

if (sensor==LOW){
    stepper1.move(7000-stepper1.distanceToGo());
    stepper2.move(7000-stepper1.distanceToGo());
   
  }

i just want know the steps that taken while button press or sensor read

If that was my problem I would move the motor one step at a time and count them.

I guess another way is to save the distanceToGo() value before and after the partial move and calculate the difference.

...R

thank you robin2 for your reply there is no any sensor connected to pin2 i’m using (https://www.arduino.cc/en/Tutorial/InputPullupSerial) switch mechanism for sensor value (on/off)

i want to calculate the distance until switch on state or (sensor==low) and use it . my above code is not working so how to program (sorry for my bad English)

himas:
i want to calculate the distance until switch on state or (sensor==low) and use it . my above code is not working

That sounds like a very different requirement compared to what is in your Original Post.

You need to provide a comprehensive description of what you want your project to achieve. Don't try to write code until you have a good description. When we understand what you are trying to make we can give better advice.

...R

after "if" statement methods are not working

The ones that expect something to be connected to the pin?

there is no any sensor connected to pin2

Is there, or is there not, something connected to pin 2?

i want to calculate the distance until switch on state

What distance? You can't measure distance with a switch or a donut or Tabasco sauce.

i want to know how to program if switch is pressed then calculate the distance that motor traveled

if switch is pressed then calculate the distance that motor traveled

Exactly how should that happen? Do you expect to know how many steps the motor has stepped at the time the switch is pressed?

How does the number of steps taken relate to distance?

himas:
i want to know how to program if switch is pressed then calculate the distance that motor traveled

What about the suggestions at the bottom of Reply #8?

...R

PaulS:
Exactly how should that happen? Do you expect to know how many steps the motor has stepped at the time the switch is pressed?

How does the number of steps taken relate to distance?

yes this is what i want to know (thank you for your replies)

Robin2:
What about the suggestions at the bottom of Reply #8?

...R

yeah..... how to do that in coding (thanks a lot for your replies )

himas:
yeah… how to do that in coding (thanks a lot for your replies )

You are not explaining what it is that you don’t understand. Saving the value in distanceToGo() seems straightforward.

Make your best attempt at the code and if it does not work post it and we will try to help.

…R

okay thank you i appreciate your help