Hello,
I'm fairly new to stepper motors and need to use this for a project. I'm trying to code a stepper motor that has a lead screw to move from 0 to around 15,000 steps then back to 0. If I want to decrease the set distance to 10,000 steps in increments of 100, how would I do so? For example, the stepper motor would move from 0 to 15,000 to 0 to 14,900 to 0 to 14,800 to 0 etc. I am using a sketch that I found that enables the stepper motor to move from 0 to 15,000 steps but I do not know where to go from there.
int Distance = 0; // Record the number of steps we've taken
void setup() {
pinMode(8, OUTPUT); // Direction Control
pinMode(9, OUTPUT); // Step Control
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
void loop() {
digitalWrite(9, HIGH);
delayMicroseconds(100);
digitalWrite(9, LOW);
delayMicroseconds(100);
Distance = Distance + 1; // record this step
// Check to see if we are at the end of our move
if (Distance == 15000)
{
delay(500); // Pauses for 0.5s at 15,000 steps
// We are! Reverse direction (invert DIR signal)
if (digitalRead(8) == LOW)
{
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
delay(1500); // Pauses for 1.5s at 0
}
// Reset our distance back to zero since we're
// starting a new move
Distance = 0;
}
}
Thank you for the quick response. I looked up what a static variable does and I understand that that's the route I should go in but I'm still unsure how to edit the code to incorporate it. Would I need to delete the "if" statement since it calls for 15,000 as a constant?
is what is referred to as a "magic number". That's because there is no indication where it came from or what it represents. Replace the number with a suitable variable - for example
So I have been trying to work on it and implementing your guys feedback and revised the code to this:
int Distance = 0; // Record the number of steps we've taken
int SetDistance = 15000;
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
void loop() {
digitalWrite(9, HIGH);
delayMicroseconds(100);
digitalWrite(9, LOW);
delayMicroseconds(100);
Distance = Distance + 1; // record this step
// Check to see if we are at the end of our move
if (Distance == SetDistance)
{
delay(500); // Pauses for 0.5s at 15,000 steps
// We are! Reverse direction (invert DIR signal)
if (digitalRead(8) == LOW)
{
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
delay(1500); // Pauses for 1.5s at 0
}
SetDistance = SetDistance - 1000;
// Reset our distance back to zero since we're
// starting a new move
Distance = 0;
}
}
After running this it seems that the Distance of which the stepper motor travels shortens by 1000 steps incrementally at both distances starting at 0 and 15000. To explain, it seems that it reads my loop as going from 0 to 15000 initially, then the set distance is changed to 14000 steps to which it completes that loop going in the reverse direction and my new starting distance is at 1000 instead of back to 0. How would I be able to revise the code to which SetDistance is only being subtracted by 1000 every other loop?