Hi, I am trying to run a stepper motor to a given position from home position. First, the motor goes to the home position (I did this using a hall sensor, where at a specific point the sensor becomes LOW and the motor stops cause I set the speed(0) using the interrupt function. till here works fine. after when I enter values from 1 to 24 since I have limits and each value is equal to 200 steps. so if I give 3, 3 * 200 = 600 steps which is stored in destination variable. till here everything works fine. but the motor won't move. I don't know what to do. Can anyone help me? (Please feel free to ask me for more information about the setup or logic)
the code:
#include <AccelStepper.h> //accelstepper library
#define dirPin 4 // Direction pin
#define stepPin 3 // Pulse/Step pin
#define enPin 5 // Enable pin
const unsigned long event_1 = 10000;
const int hallPin = 2;
int countHP = 0;
const float stepAngle = 1.8; // Stepper motor step angle
const int stepsPerRevolution = 1600; // Steps required for 360 degree rotation
bool homeposition = false;
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin, enPin);
void setup()
{
pinMode(hallPin, INPUT_PULLUP); // internal pullup resistor (debouncing)
attachInterrupt(digitalPinToInterrupt(hallPin), hallValueChanged, FALLING); //do not change it to 'CHANGE'
Serial.begin(9600); //defining some baud rate
//---------------------------------------------------------------------------
stepper.setMaxSpeed(1500);
stepper.setAcceleration(1000);
stepper.setSpeed(500);
stepper.moveTo(50000);
//---------------------------------------------------------------------------
}
void loop()
{
if (homeposition)
{
if (homeposition)
{
stepper.setCurrentPosition(0);
Serial.println("Homing Completed");
Serial.println("");
while (Serial.available() > 0)
{
int serialValue = Serial.parseInt(); // Read position from Serial Monitor
Serial.print("got the value");
moveToPosition(serialValue);
}
}
}
else
{
if (countHP == 0)
{
stepper.run();
homePosition();
}
}
}
void moveToPosition(int value)
{
int stepsPerLine = 200;
int homeValue = 12;
int currentPosition = homeValue;
if (value)
{
if (value)
{
int testPosition = value;
if (0 <= testPosition && testPosition <= 24)
{
Serial.print("HII");
if (currentPosition < testPosition)
{
//direction = 0;
Serial.print("Clockwise");
int destination = testPosition - currentPosition;
destination = destination * stepsPerLine;
stepper.setMaxSpeed(1500);
stepper.setAcceleration(1000);
stepper.setSpeed(500);
stepper.moveTo(destination);
currentPosition = testPosition;
}
if (currentPosition > testPosition)
{
//direction = 1;
Serial.print("AntiClockwise");
int destination = currentPosition - testPosition;
destination = destination * stepsPerLine;
stepper.setMaxSpeed(1500);
stepper.setAcceleration(1000);
stepper.setSpeed(500);
stepper.moveTo(-destination);
currentPosition = testPosition;
}
}
}
}
}
void homePosition()
{
if(homeposition == true && countHP == 0 )
{
stepper.setSpeed(0);
countHP = +1;
}
}
void hallValueChanged()
{
homeposition = true;
}