Hello again,
I have to warn you, I am pretty new to Arduino and will have many failures before I get success. So don't assume anything I do or say to be accurate, or to be the best way to tackle a problem, or to even work!
Having said that, this is what I did to get my stepper motor spinning in both directions, it was a modification of the stepper_speedControl example provided in the IDE:
#include <Stepper.h>
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
int MotorDriver = 12; // Use this pin to disable motor driver at low speeds (to save power)
int led = 13; // There is a LED on the Arduino board connected at pin 13.
int stepCount = 0; // number of steps the motor has taken
int SpeedPin = A1; // Analog input for speed control
int SpeedReading;
int MotorSpeed;
void setup() {
pinMode(MotorDriver, OUTPUT);
pinMode(led, OUTPUT);
digitalWrite(MotorDriver, LOW); // initialise Motor Driver to be off
digitalWrite(led, LOW); // debug, to see what MotorDriver is doing
}
void loop() {
// read the sensor value:
SpeedReading = analogRead(SpeedPin);
// map it to a range from ...
MotorSpeed = map(SpeedReading, 0, 1023, -200, 200);
if (MotorSpeed > 10) {
digitalWrite(MotorDriver, HIGH); // Enable Motor Driver
digitalWrite(led, HIGH); // debug
myStepper.setSpeed(MotorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution/100);
}
else if (MotorSpeed < (-10)) {
digitalWrite(MotorDriver, HIGH);
digitalWrite(led, HIGH);
myStepper.setSpeed(-MotorSpeed);
myStepper.step(-stepsPerRevolution/100);
}
else {
// Turn off the Motor Driver so as not to waste power. Use pin 12 to drive Motor Controller Enable
digitalWrite(MotorDriver, LOW);
digitalWrite(led, LOW);
}
}
I deleted some comments, but I think it's all there.
So I mapped the analog input to a range from -X to +X, not just 0 to +X. I had a look at Stepper.ccp to try to figure out how to reverse the motor. From memory I think the myStepper.setSpeed function requires the number passed to it to be positive (because it determined the pulse delay... and a negative delay doesn't make sense), so when MotorSpeed is negative, it must be made positive. The reversal of direction is then achieved by making the value passed to the myStepper.step function negative. Whatever the case, the above code worked.
But, as I have mentioned, I am ditching the stepper motor for a DC motor. I plan to use the same L298N to drive it. I have ordered this motor:
http://www.ebay.com.au/itm/130580506675?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649
but I expect it will still be a couple of weeks away.
A friend of mine bought one to drive a 16mm camera mechanism. At 12V it gives the 300rpm or so. A quick check (not precise) showed it drew around about 100mA with no load, but when holding onto the shaft it rose to about 400mA (depending how hard you hold it). And it turns both ways. Should be ok I think. For $10 it's worth a shot (it's just a shame I have to wait so long to get it).