#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
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();
{
}
} }
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