Seeking help understanding WHY I am getting the results I am getting from my code.
Hardware:
Arduino Uno rev3, duinotech XC4422 joystick, 2 Duinotech XC-4492 motor controller modules, and 2 NEMA 17 stepper motors.
Wires:
Joystick to Arduino on analog 0 and 1, and using 5v and one of two ground sockets on the same row of connectors.
Arduino to first motor controller on digital pins ~3,4,~5,6 to controller pins IN4, IN3, IN2, IN1, in that exact-match order.
Arduino to second motor controller on pins 7,8,~9,~10 to controller pins IN4, IN3, IN2, IN1, again in order.
Each motor controller wired to it's own 4-wire stepper motor in identical patterns.
Prior to attempting to add speed control, this code WORKED. The joystick more or less functioned the same way a set of four switches would (either MOVING or NOT MOVING, at the initially set speed), but it WORKED. Speed was set to 20, and both steppers went both directions as instructed by joystick movement at that fixed speed.
I looked at a lot of posts/web pages/etc about speed control from analog input on arduinos and pretty much everything seemed to be related to some specific driver or other specific hardware. It also all seemed over-done (way too much code to do a basic thing).
So I thought a simple combo of the map() to change scale and abs() to kill negatives would get me a 0 to 100 speed scale pretty easy and then I could feed the result into the stepper instance. (See code)
Um, nope.
Just focusing on the Y axis for the following:
The map() is correctly converting between the scales and outputting to the variable.
Serial printing lets me see that this is so.
But, I am trying to use StepperY.setSpeed(YSpeed) to get the damn speed value INTO the stepper instance, and I can't. The speed is totally stuck at 20.
So please, if you can, help me understand why this method of passing the value stored in YSpeed to the actual instance of the stepper driver is not working. I'm seriously missing something, and I don't know what it is that I AM missing!
Seriously - is this all because of how I am sending the step(1) instructions at the bottom and not even related to the speed code I wrote?
I have no idea at this point and am plenty confused.
Thanks.
Code follows (Yes I know X-axis speed code is not good.)
// First stab at using an Arduino to control a light fixture's direction of projection.
// Joystick control of two stepper motors.
#include <Stepper.h>
// initialize stepper instances for each axis' motor control:
Stepper StepperY(200, 2,3,4,5);
Stepper StepperX(200, 6,7,8,9);
// Why the F*** doesn't the stepper.h documantation tell us HOW it uses these four pins - as in "which pin is doing what"... Ugh. No Sh** they go to the motor, you boobs.
//________________________________________________________________________________________
void setup() {
// run the serial monitor so I can print to the computer's screen
Serial.begin(9600);
// Now set the initial speed of the motors
// I really don't know why I am setting this here versus up in the creation of the instance above or left undeclared until assigned in the loop.
// but it was done in void setup in the sample code I saw so I am putting it here.
StepperX.setSpeed(20); // set first stepper speed
StepperY.setSpeed(20); // set second stepper speed
}
//________________________________________________________________________________________
void loop() {
// Find out where the joystick is 'naturally' sitting and set
// starting values where joystick movement will trigger resposes
// from the Arduino and add a small padding to disallow jitter at the natural dead zone
// The joystick is NOT at the same value every time you power up the arduino,
// so I needed this 'grab and set' routine to compensate.
// 'static' is used so these don't recalculate every loop cycle.
static int XnegStart= (analogRead(A0)-10);
static int XposStart = (analogRead(A0)+10);
static int YnegStart = (analogRead(A1)-10);
static int YposStart = (analogRead(A1)+10);
// set up speed values by mapping the joystick analog input from 0 to 1023 scale against a 0 to 100 scale to generate a useful speed value
// NOTE- I am unsure about my stepper motor's actual max RPM so am being very conservative with this 100 RPM scale
/*
int XSpeed = analogRead(A0);
XSpeed = map(XSpeed,0,1023,-100,100);
if ( abs(XSpeed) < 20 ) {XSpeed=20;}
else ( StepperX.setSpeed(abs(XSpeed)) );
*/
int YSpeed = analogRead(A1);
YSpeed = abs(map(YSpeed,0,1023,-100,100));
if ( YSpeed < 20 ) { YSpeed = 20; }
if ( YSpeed > 20 ) { StepperY.setSpeed(YSpeed); }
// grabbing values from joystick so I don't call analogRead a ton of times in the if statements below
int JoystickXinput = analogRead(A0);
int JoystickYinput = analogRead(A1);
///*
// Print to screen hoping to see what he hell is going on here
// Serial.println("X");
// Serial.println(JoystickXinput);
// Serial.println(abs(XSpeed));
Serial.println(" ");
Serial.println(" ");
Serial.println("Y");
Serial.println(JoystickYinput);
Serial.println(YSpeed);
Serial.println(" ");
Serial.println(" ");
//*/
// Make steppers move !
if (JoystickXinput < XnegStart) { StepperX.step(1); } // step X left
if (JoystickXinput > XposStart) { StepperX.step(-1); } // step X right
if (JoystickYinput < YnegStart) { StepperY.step(1); } // step Y up
if (JoystickYinput > YposStart) { StepperY.step(-1); } // step Y down
}