# please help me to rotate a stepper motor

hai friends

i am using AccelStepper.h library for rotating a stepper motor.

i have a sensor connected to the A0 pin of the arduino, i had set the thershold value of sensor as 150. ie when the reading in pin A0 is greater than 150 the motor should rotate clock wise 100 steps. when the reading in pin a0 becomes less than 150 the motor should rotate 100 step in anti clock wise direction.

Show us the code you’ve written so far.

What happens if the reading stays above 150? Should it rotate another 100 steps?

What if it goes above 150 for 1ms - should the motor rotate the full 100 steps before going back again? Or should it return to the starting point as soon as it notices?

You have to plan in detail the behaviour for all possibilities, which means you have to think them all through to decide what your code should do at each stage...

when the value goes above 150, the motor should rotate X steps in clock wise direction and when the reading goes below 150 the motor should rotate X steps in anti clock wise direction.

#include <AccelStepper.h>
#define THRESHOLD 150 //In order to determine the state of the hand (opened/closed)
AccelStepper stepper1(1, 9, 8);// Define steppers1 and the pins they will use.4 step 5 direction
int pos1,X;

void setup()
{
Serial.begin(115200); //BAUDRATE set to 115200, remember it to set monitor serial properly (used this BaudRate and “NL&CR” option to visualize the values correctly)
stepper1.setMaxSpeed(3000); // maximum speed with which the motor can run
stepper1.setAcceleration(1000);

}

void loop()
{

int value = analogRead(A0); //muscle sensor reading
if(value>THRESHOLD) //If the value of the sample is greater than THRESHOLD means that the hand has been closed
{closeFinger();}
else //Otherwise the hand is open
{openFinger();}
Serial.println(value); //You can use serial monitor to set THRESHOLD properly, comparing the values shown when you open and close your hand
}

//****************************************************//

void openFinger()// function to open finger 1 to 5
{

pos1 = -X;//taking negative of count for reverse rotation
stepper1.moveTo(pos1);
stepper1.run(); // commant to run the motor

}
void closeFinger()// function to close finger 1 to 5
{

stepper1.moveTo(1);//use a small val
stepper1.run();
int a = analogRead(A1);//reading force sensor value
X++;//count of number of steps

}

Please modify your post and use the code button </> `so your code looks like this` and is easy to copy to a text editor. Then I will study it. See How to use the Forum