Hi all,
I am building a camera slider that is controlled by an arduino. It works by pressing a combination of four buttons which correspond to a travel time for the 200mm long slider. The stepper is supposed to make 1 step, wait for a certain time until he makes the next step. By changing the stepdelay, he will take different lengths of time to reach the end of the slider. In this case 30s, 5s or 15s.
I am using an Arduino Micro and A4988 Stepper Driver with a Nema 17 Attached.
The electronics are setup allright. The Serial Monitor also states the mode to be "s2". But the Stepper does not make a single step. Its only energized and stationary.
Here is my code:
int Pin2 = 2; //Pin 1 cannot be used for this. Start at 2.
int Pin3 = 3;
int Pin4 = 4;
int Pin5 = 5;
int Pin6 = 6;
int Pin7 = 7;
int SliderLength = 200; //Travel length in mm
int PulleyDiameter =6; //Pulley Diameter in mm
int TravelTime; //Used for calculations in the loop section.
double Steps; //Steps required to move along the entire Rail.
double StepDelay;
bool s2; ///s2 stands for switch2_status. It starts at 2 so we can match the pin.
bool s3;
bool s4;
bool s5;
bool s6;
bool s7;
const int stepPin = 8; //defines the step pin
const int dirPin = 9; //defines the direction pin
void setup()
{
Serial.begin(9600); //Required for Serial Monitor
pinMode(Pin2, INPUT);
pinMode(Pin3, INPUT);
pinMode(Pin4, INPUT);
pinMode(Pin5, INPUT);
pinMode(Pin6, INPUT);
pinMode(Pin7, INPUT);
Steps = 200*(SliderLength/(3.14*PulleyDiameter)); ///Calculates the Steps required to move along the entire Rail.
Serial.println("");
Serial.print("Steps = ");
Serial.println(Steps);
digitalWrite(dirPin, HIGH); //Defines the direction of travel. LOW reverses it.
}
void MoveStepper()
{
digitalWrite(stepPin, HIGH); //This moves the stepper by 1 step.
delayMicroseconds(1000); //Depends on Motor 500-1000 is good
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
// the loop routine runs over and over again forever:
void loop()
{
s2 = digitalRead(Pin2); ///This checks the status of the switches.
s3 = digitalRead(Pin3);
s4 = digitalRead(Pin4);
s5 = digitalRead(Pin5);
s6 = digitalRead(Pin6);
s7 = digitalRead(Pin7);
if (s2 == LOW && (s3 & s4& s5 & s6) == HIGH) ///LOW means switch is pressed/closed/conductive.
{
Serial.println("Mode = s2");
TravelTime = 30; //The slider will move the distance in 30s
StepDelay = ((TravelTime/Steps)-0.002)*1000; //This calculates the delay required in ms between steps to make the journey in the desired amount of time. 0.001 Seconds are deducted because the MoveStepper Method includes 2x 500microseconds delays.
MoveStepper();
Serial.print("Stepdelay = ");
Serial.println(StepDelay);
// THis is in miliseconds (ms) Not MICRO!
delay (StepDelay);
}
if (s3 == LOW && (s2 & s4& s5 & s6) == HIGH) ///LOW means switch is pressed/closed/conductive.
{
Serial.println("Mode = s3");
TravelTime = 5; //The slider will move the distance in 30s
MoveStepper();
StepDelay = ((TravelTime/Steps)-0.001)*1000; //This calculates the delay required in ms between steps to make the journey in the desired amount of time. 0.001 Seconds are deducted because the MoveStepper Method includes 2x 500microseconds delays.
Serial.print("Stepdelay = ");
Serial.println(StepDelay);
delay (StepDelay); // THis is in miliseconds (ms) Not MICRO!
}
if (s4 == LOW && (s2 & s3& s5 & s6) == HIGH) ///LOW means switch is pressed/closed/conductive.
{
Serial.println("Mode = s3");
TravelTime = 15; //The slider will move the distance in 30s
MoveStepper();
StepDelay = ((TravelTime/Steps)-0.001)*1000; //This calculates the delay required in ms between steps to make the journey in the desired amount of time. 0.001 Seconds are deducted because the MoveStepper Method includes 2x 500microseconds delays.
Serial.print("Stepdelay = ");
Serial.println(StepDelay);
delay (StepDelay); // THis is in miliseconds (ms) Not MICRO!
}
}