How to rotate stepper motor clockwise and anti-clockwise in a loop

Hi to everyone, I am doing for a project a with stepper motor and I’m a little stuck. I want to rotate the stepper motor clockwise and anti-clockwise in a loop (Suppose stepper motor rotate clockwise 500 steps and then delay 5 sec after that it rotates 400 steps anti-clockwise and then again rotate clockwise 500 steps and anti-clockwise 400 steps). So, how to modify my code? Here the code is…

#include<Stepper.h>
const float referenceVolts = 5.0; // the default reference on a 5-volt board
const int batteryPin = 0; // battery is connected to analog pin 0
const int stepsPerRevolution = 200; // change this to the number of steps on your motor
Stepper myStepper(stepsPerRevolution, 9, 10, 11, 12); // input pin in the stepper motor (you change this pin also)
int stepCount = 0; // initial condition for stepper motor
unsigned long int milli_time; //variable to hold the time
void setup()
{
while (!Serial);
Serial.begin(9600); // for serial monitor & plotter(Data accumulation)
Serial.println(“CLEARDATA”); //This string is defined
// to get data in excel sheet using PLX DAQ
Serial.println(“LABEL,Computer Time,Time (Milli Sec.),Stepcount,Degree,Deg,Voltage”);
}
void loop()
{
int val = analogRead(batteryPin); // read the value from the sensor
float volts = (val / 1023.0) * referenceVolts; // calculate the ratio
myStepper.setSpeed(30); // speed control of stepper motor
myStepper.step(1); // initilazie the step of stepper motor
milli_time = millis(); // take time if need if not then off this line
Serial.print(“DATA,TIME,”); // to get data in excel sheet using PLX DAQ
Serial.print(milli_time);
Serial.print(",");
//Serial.print(“step”);
Serial.print(stepCount); // step of stepper motor
Serial.print(",");
//Serial.print(“degree”);
Serial.print(1.8stepCount); // how many degree rotate the motot
Serial.print(",");
Serial.print(0.09
stepCount);
Serial.print(",");
//Serial.print(“voltage”);
Serial.println(volts); // print the value in volts (get data from detector)
stepCount ++;
delay(1000);
}

Hi, Some examples here:

https://arduinoinfo.mywikis.net/wiki/SmallSteppers#Test_Sketch:_Rotate_1_turn_in_each_direction.2C_repeat.

Thanks a lot. It's work very well.

Hi to everyone, I am doing for a project a with stepper motor and I’m a little stuck. I want to stop the stepper motor when the condition satisfies (Actually the stepper motor rotate continuously step by step and a sensor detector detect voltages through A0 (in voltage) with parallel to stepper motor rotation). Now, I want to stop the stepper motor when the sensor value comes to the minimum value (let’s suppose 0.03 or 0.02 volts) but I don’t know what is the minimum value (If I know the minimum value then I use the “For Loop”). So, how can I modify my code ?? Here is my code…

#include<Stepper.h>
const float referenceVolts = 5.0; // the default reference on a 5-volt board
const int batteryPin = 0; // battery is connected to analog pin 0
const int stepsPerRevolution = 200; // change this to the number of steps on your motor
Stepper myStepper(stepsPerRevolution, 9, 10, 11, 12); // input pin in the stepper motor (you change this pin also)
int stepCount = 0; // initial condition for stepper motor
unsigned long int milli_time; //variable to hold the time
void setup()
{
while (!Serial);
Serial.begin(9600); // for serial monitor & plotter(Data accumulation)
Serial.println(“CLEARDATA”); //This string is defined
// to get data in excel sheet using PLX DAQ
Serial.println(“LABEL,Computer Time,Time (Milli Sec.),Stepcount,Degree,Deg,Voltage”);
}
void loop()
{
int val = analogRead(batteryPin); // read the value from the sensor
float volts = (val / 1023.0) * referenceVolts; // calculate the ratio
myStepper.setSpeed(30); // speed control of stepper motor
myStepper.step(1); // initilazie the step of stepper motor
milli_time = millis(); // take time if need if not then off this line
Serial.print(“DATA,TIME,”); // to get data in excel sheet using PLX DAQ
Serial.print(milli_time);
Serial.print(",");
//Serial.print(“step”);
Serial.print(stepCount); // step of stepper motor
Serial.print(",");
//Serial.print(“degree”);
Serial.print(1.8stepCount); // how many degree rotate the motot
Serial.print(",");
Serial.print(0.09
stepCount);
Serial.print(",");
//Serial.print(“voltage”);
Serial.println(volts); // print the value in volts (get data from detector)
stepCount ++;
delay(1000);
}

This seems to be the same question as in your other Thread. Double Posting just wastes everyone's time. I am suggesting to the Moderator to merge them.

...R

PS ... this also looks very like your third Thread