I'm working with an Arduino Mega 2560, using a AF_MotorShield and a small 28BYJ-48 Stepper.
I'm interested in using AF_Motor + AccelStepper together, and I need to control the motor via Serial. The motor should go forward if the value is positive and backwards if negative. Or that is what I get from the move() function in the AccelStepper lib. Anyhow, the motor moves nicely but always reaches the value going FORWARD. Is that easy to achive given this CODE?:
- Code: Select all
#include <AccelStepper.h>
#include <AFMotor.h>
AF_Stepper motor1(64, 2);
float azimuth;
long pos= 0, lastPos = pos;
int REVOLUTION = 2048;///Somehow found out that this is the value that works for this motor
boolean control = true;
String inString = "";
void forwardstep() {
motor1.onestep(FORWARD, DOUBLE);
}
void backwardstep() {
motor1.onestep(BACKWARD, DOUBLE);
}
AccelStepper stepper(forwardstep, backwardstep);
void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Serial to Stepper!");
stepper.setMaxSpeed(320.0);
stepper.setAcceleration(100.0);
//stepper.moveTo(pos);
}
void loop()
{
if(control){
stepper.move(mapToRange());
control = false;
}
while (Serial.available() > 0) {
float inChar = Serial.read();
//if (isDigit(inChar)) {
// convert the incoming byte to a char
// and add it to the string:
inString += (char)inChar;
//}
// if you get a newline, print the string,
// then the string's value:
if (inChar == '\n') {
control = true;
azimuth=inString.toFloat();
Serial.print("Value:");
Serial.println(mapToRange());
Serial.print("String: ");
Serial.println(inString);
// clear the string for new input:
inString = "";
}
}
stepper.run();
lastPos = pos;
}
long mapToRange(){
pos = map(azimuth, 0, 360, 0, REVOLUTION);
return pos-lastPos;
}
I haven't been able to and am getting a bit clueless.The ideal would be to work with absolute positioning (moveTo(pos)), but that also doesn't work. It will keep on moving FORWARD. Any hints?

