Hi Guys,
I have the below code which is a small stepper motor which is controlled by the X axis of a joystick and when i move the joystick the motor moves but when i let go the motor return to its original point. Can someone help me, i need the motor to not return. I need the motor to move forward and backwards with the joystick movements. I hope someone can help me.
Thanks
#include "Stepper.h"
#define STEPS 32 // Number of steps for one revolution of Internal shaft
// 2048 steps for one revolution of External shaft
#define X_pin A0 // Pin A0 connected to joystick x axis
#define LED1 5
#define LED2 6
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
Stepper small_stepper(STEPS, 8, 10, 9, 11);
int steps = 1025; // Assumes the belt clip is in the Middle
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
Serial.begin(9600);
}
void loop() {
small_stepper.setSpeed(900);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
Serial.println(analogRead(X_pin));
while (analogRead(X_pin) >= 0 && analogRead(X_pin) <= 100) {
if (steps > 0) {
small_stepper.step(-1);
steps--;
}
}
small_stepper.setSpeed(900);
while (analogRead(X_pin) > 100 && analogRead(X_pin) <= 400) {
if (steps < 512) {
small_stepper.step(1);
steps++;
}
if (steps > 512) {
small_stepper.step(-1);
steps--;
}
}
small_stepper.setSpeed(900);
while (analogRead(X_pin) > 401 && analogRead(X_pin) <= 600) {
if (steps < 1025) {
small_stepper.step(1);
steps++;
}
if (steps > 1025) {
small_stepper.step(-1);
steps--;
}
}
small_stepper.setSpeed(900);
while (analogRead(X_pin) > 601 && analogRead(X_pin) <= 900) {
if (steps < 1535) {
small_stepper.step(1);
steps++;
}
if (steps > 1535) {
small_stepper.step(-1);
steps--;
}
}
small_stepper.setSpeed(900);
while (analogRead(X_pin) > 900 && analogRead(X_pin) <= 1024) {
if (steps < 2050) {
small_stepper.step(1);
steps++;
}
}
}