Been trying to see where Im going wrong with this for a while now and cant seem to get the motor to do what I want . Its will most likely be due to my inexperience with the programming and despite trying to push through I cannot get the functions I am trying for.
Problem:
I have an analogue input (pot) and want to make a stepper spin clockwise if the value is >500 and counter-clockwise if it is <500.
Below are a couple of attempts within my code as well as the current iteration. Spinning cw is fine but the best I seem to manage is to stall the motor when trying reverse. Ideally I would like proportional control but getting this working using only my own skills and information gleaned from the interweb isnt working.
Hope someone can help.
#include <AccelStepper.h>
AccelStepper stepper;
const int Xdeadzone = 3, Ydeadzone = 3; // slack for pot values
#define ANALOG_IN A0
void setup()
{
Serial.begin(9600);
stepper.setMaxSpeed(1000);
stepper2.setMaxSpeed(1000);
// stepper.moveTo(ANALOG_IN);
// stepper.setSpeed(700);
//stepper2.setSpeed(700);
//stepper.moveTo(1024);
}
void loop()
{
int X = analogRead(ANALOG_IN);
Serial.println(X);
//int right = (X, 502, 907, 0, 1000);
//int left = map(X, 0, 496, 0, 255);
//int motorSpeed2 = map(sensorReading, 0, 320, 0, 100);
if (X >= (499 + Xdeadzone))
{
stepper.setSpeed(700);
stepper.runSpeedToPosition();
}
else if (X < (499 - Xdeadzone))
{
stepper.moveTo((-stepper.currentPosition()));
stepper.setSpeed(700);
stepper.runSpeedToPosition();
}
}
/*
if (X = (499 +- Xdeadzone));
stepper.setCurrentPosition(0);
if (X >= (499 + Xdeadzone));//Right
stepper.moveTo(ANALOG_IN);
stepper.setSpeed(700);
stepper.runSpeedToPosition();
if (X < (499 - Xdeadzone))
stepper.moveTo(-ANALOG_IN);
stepper.setSpeed(700);
}
/*
void cw()
{
if (stepper.distanceToGo() == X) {
stepper.moveTo(-stepper.currentPosition());
}
stepper.run();
}