I am trying to work on a program that requires my motor to do specific operations:-
Step 1) rotate the shaft till it reaches a limit switch.
Step 2) after step one triggers the limit switch, I want the shaft to move certain no. of steps in the
opposite direction.
Step 3) After step 2 I want the shaft to oscillate CW and CCW for a specific no. of steps.
Step 4) after step 3 i want the stepper to stop oscillating for a certain amount of time.
Step 5) Lastly I want step 3 and 4 to be done a certain no. of times or in a loop.
The program that I have got so far is given below:-
#include <Stepper.h>
const int dirPin =8;
const int pulPin = 9;
const int limit1 = 2; // limit switch
int stepsPerRev1 = 2740;
Stepper motor1 (stepsPerRev1,8,9);
void homing() // moving motor to find home sensor limit1
{ motor1.setSpeed(5000);
digitalWrite(dirPin, HIGH); // setup motor turn left
do
{
digitalWrite(pulPin , HIGH);
delayMicroseconds(500);
digitalWrite(pulPin , LOW);
delayMicroseconds(500);
}
while (digitalRead(limit1) == HIGH); // motor turn left until touches limit 1
delay(500);
motor1.step(-4000);
delay(1000);
}
void form()
{
int i=10;
do
{ motor1.setSpeed(5000);
motor1.step(stepsPerRev1);
delay(200);
motor1.step(-stepsPerRev1);
delay(200);
i=i-1;
}
while(i>0);
delay(5000);
}
void setup()
{ // Set up stepper motor pins
Serial.begin(9600);
pinMode(dirPin, OUTPUT);
pinMode(pulPin, OUTPUT);
pinMode(limit1 , INPUT_PULLUP); //Set up the limit switch
homing(); // call homing function;
}
void loop()
{
for(int n=5;n>=5;n--)
{
form();
}
}
So my questions are as follows:-
I run into a problem of executing step 5. Though I have put it into a 'FOR' loop for running the form function 5 times, It does not stop at 5, it keeps running. Am I using the For loop wrong?
Second question is just a doubt- Its about the 'homing' function- Though my limit switch 'limit1' is not pressed continuously, the state remains in 'HIGH' and my guess is, that is the reason for the while loop to run the statements in the do continuously- Is my assumption correct?
FYI- I have used a 10K pull up resistor for my limit switch.
Forget your code for right now. We can't see what you have or how you have it wired or how you are powering the whole project. How are you triggering the limit switch with the stepper motor rotation? Does that work if you trigger the limit switch by hand?
Hi, @bijoythomaschandy
I think we need a circuit diagram and possibly an image(s) of your project so we can see your component layout.
A hand drawn circuit will be fine, just make sure it includes powersupply as well.
You have set motor speed to 5000 RPM and steps per rev to 2740, that's 5000 * 2740 = 13700000 steps per minute or 13700000 / 60 = 228333 steps per second. Impossible without acceleration control and probably WITH. What is the gear ratio? What will the motor speed be if the output shaft is turning 5000 RPM.
The datasheet says about 13.73. Without acceleration and driving the gear train you might get 200 steps per second without missing steps, but that would only be about 4.4 RPM. How fast do you want the output shaft to turn?
WAIT, WAIT! That driver WILL NOT work with the stepper.h library.
PHEW! You need a library like AccelStepper that works with step / direction drives, Sorry.
ok. Thats Interesting, Any recommendations on how I need to modify my program? The acceleration function in the accel stepper library maybe ?
But I am still trying to get my head around how the loop does not stop even after I have placed it in the void loop() function with a for loop OR placing it in the setup() function. @JCA34F@TomGeorge@wildbill.
Hello bijoythomaschandy
Generally spoken your sketch needs some pure basic functions like motor control, fwd, rev and stop. A time handler and may be a FSM take care about the reqiured steps 1 to 5 specified in a structured array. Thus you are able to add new steps without changing the method to handle the actions the stepper motor.
Have a nice day and enjoy coding in C++.