it's suppose to just move one step at a time. try setting the speed higher or advancing more that 1 step at a time
The speed is set to 800 if you look at the full code i posted, that should be plenty fast. I think its just stepping one time then doing the loop again then stepping and then loop so and so on. i will try to change some stuff and update
if each iteration of loop without the step takes roughly the same amount of time to step the motor, then the speed will be half of what is specified. so when stepping one step at a time, the speed might would need to be doubled to reduce the time it takes to step the motor along with the time to loop()
Good find, So what i am seeing is that the inputs to the motors are coming from 1Y through 4Y? The input MobaTools library needs is the pins from the arduino. the only pins that actually do something to the L293D are pins D12, D8, D7 and D4 in my eyes. Could these control the whole thing?
You wrote that you have the V1.2
in a later posting you posted a link to the new motorshield V2.0
So what does this mean in numbers?
If you use your original code how much rpm is it estimated?
If you use gc's-code how many rpm is it estimated?
if you look up the accelstepper-documentation
http://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#adfb19e3cd2a028a1fe78131787604fd1
you can set the acceleration to different values.
It seems that accelstepper uses the startspeed if you let it do a single step.
void AccelStepper::setAcceleration ( float acceleration )
Sets the acceleration/deceleration rate.
Parameters
[in] acceleration The desired acceleration in steps per second per second. Must be > 0.0.
each second increase step-pulse-frequency for "x" steps per second
If this value is set to a high value the accelleration is high.
Though as this code calls repeatedly for a single step the code always starts at the speed of starting a move.
This means you should set a higher speed.
anyway once the IO-pin-definition is clear using the mobaTools will make life much much easier
best regards Stefan
Measured it and its 30 rpm but it can be faster still if set to a higher speed.
With gc's code its 1,5 rpm havent yet found a way to set it faster. this is still with the same speed settings as the original code.
I set the acceleration from 100 up to 25000 to try things out, it did not like the 25000 but it does seem to go the tiniest bit faster. anything in between either works a little faster like maybe 2 rpm and slower then about 500 does little to nothing it goes even slower then gc's code.
I have this new thought tho. is there a way i can make the motor turn the steps it takes to turn 1 degree on the compass? so there will be a range of 0 to 359 "steps" for the motor to rotate a full turn? instead of 0 to 2048 ofcourse ive tried adjusting the motorrange to set it to 0 to 359 but then it will only take 359 steps and not do the full turn.
acceleration has an impact on a run with a lot of steps. Acceleration means get faster while rotating.
If each rotating ends after one step. There is no accelerartion possible.
This means the speed must be increased.
If increasing the speed does not help
there is something different in your code that is slowing down.
The main difference between a single call with which the stepper-motor does hundreds of steps and gc's code is
stepper1.runToNewPosition(100);
does loop INSIDE the function-call runToNewPosition
which is blocking
to avoid blocking there are two ways
-
using the mobatools-library
-
looping OUTSIDE function runToNewPosition()
with calling the function
runToNewPosition(1)
repeatedly to get the result of rotating again 100 steps
This is what gc's code is doing.
If this results in a low rpm like 1,5-2 rpm
there is something in your code that is slowing down the repeated call of
runToNewPosition(1)
This is just another case of the thousands of cases where posting the
actual and complete sketch
will reveal the bug
.
So please post the actual
and
the complete sketch
as a code-section
best regards Stefan
The third post is the full code im using with gc's Loop part but i will post it again. with changes to speed, acceleration and names of the stepper to show better difference where im using the AFMotor library and AccelStepper library
#include <AccelStepper.h>
#include <AFMotor.h>
#include <Multistepper.h>
const int Home = 2; // Home switch for calibration
const int Led = 13; // Led for identifying when calibration is done
const int sensorPin = A0; // Potentiometer for turning the motor
const int sensorMin = 1; // Potentiometers minimum
const int sensorMax = 1020; // Potentiometers maximum
int Speed = 1250; // Value for speed of the stepper
int MaxSpeed = 1250; // Value for max speed of the stepper
const int motorMin = 0; // Minimum value for the map of the stepper
const int motorMax = 359; // Maximum value for the map of the stepper
AF_Stepper stepperAF(102.5, 2); // Identifies the stepper motor
void forwardstep() { // Forward step for AccelStepper to work
stepperAF.onestep(BACKWARD, DOUBLE); // Backward step at DOUBLE speed because it has to be reversed for some reason
}
void backwardstep() { // Backward step for AccelStepper to work
stepperAF.onestep(FORWARD, DOUBLE); // Forward step at DOUBLE speed because it has to be reversed for some reason
}
AccelStepper stepperAS(forwardstep, backwardstep); // Makes the AccelStepper work with the steppermotor connected to the shield
void calibrateStepper() { // Calibration sequence
Serial.println("Calibrating..."); // Prints Calibration start
while (digitalRead(Home) == LOW) { // Inserts a low value when the stepper is not at home
digitalWrite(Led, LOW); // Turns LED off when the stepper is not at home
stepperAF.step(1, FORWARD, MICROSTEP); // Makes the stepper move at double speed until its at home
}
digitalWrite(Led, HIGH); // Turns LED on when the stepper is home
stepperAS.setCurrentPosition(0); // Calibrates current position as home
Serial.println("Calibration done"); // Prints Calibration succesful
delay(1500); // Wait for 1.5 seconds
digitalWrite(Led, LOW); // Turns LED off and stops calibration sequence
}
void setup() {
Serial.begin(9600); // Begins serial input
pinMode(Home, INPUT); // Home = input
pinMode(Led, OUTPUT); // LED = output
calibrateStepper(); // Starts calibration sequence
stepperAS.setAcceleration(2000); // Sets Acceleration at max
stepperAS.setSpeed(Speed); // Sets speed of stepper
stepperAS.setMaxSpeed(MaxSpeed); // Sets max speed of stepper
}
void loop() {
int sensorReading = analogRead(sensorPin);
int range = map(sensorReading, sensorMin, sensorMax, 0, 359);
Serial.println(range);
int Motorrange = map(range, motorMin, motorMax, 0, 2048);
if (Motorrange > 2048) {
Motorrange -= 2048;
}
Serial.println("Motorrange:");
Serial.println(Motorrange);
static int pos;
if (pos < Motorrange)
stepperAS.runToNewPosition (++pos);
else if (pos > Motorrange)
stepperAS.runToNewPosition (--pos);
}
I would really like to try the mobatools library but
1 most of the test stuff is in german which i can barely speak let alone read.
2 im not good in coding at all so even knowing or finding out which pin i have to choose to make the motor do anything is going to take alot of time and effort.
3 the motor shield really does not have an easy way to input anything else to a library then "forwardstep" and "backwardstep" seen used for the accelstepper library. therefore i have no clue how to make the mobatools library work with it.
Ive tried the D12 D8 D7 and D4 method but that made the Led flash a couple times like it was drawing power but not do anything with the motor. i used one of the example codes to try this out.
I guess the slow rpm is caused by the serial printing
here is your code where I added time-measuring
Run this code-version once with
Serial.begin(9600);
and once with changed baudrate
Serial.begin(115200);
You have to change the baudrate in the serial monitor to to 115200
#include <AccelStepper.h>
#include <AFMotor.h>
#include <Multistepper.h>
const int Home = 2; // Home switch for calibration
const int Led = 13; // Led for identifying when calibration is done
const int sensorPin = A0; // Potentiometer for turning the motor
const int sensorMin = 1; // Potentiometers minimum
const int sensorMax = 1020; // Potentiometers maximum
int Speed = 1250; // Value for speed of the stepper
int MaxSpeed = 1250; // Value for max speed of the stepper
const int motorMin = 0; // Minimum value for the map of the stepper
const int motorMax = 359; // Maximum value for the map of the stepper
unsigned long BeforePrint;
unsigned long AfterPrint;
unsigned long BeforeRunPos;
unsigned long AfterRunPos;
AF_Stepper stepperAF(102.5, 2); // Identifies the stepper motor
void forwardstep() { // Forward step for AccelStepper to work
stepperAF.onestep(BACKWARD, DOUBLE); // Backward step at DOUBLE speed because it has to be reversed for some reason
}
void backwardstep() { // Backward step for AccelStepper to work
stepperAF.onestep(FORWARD, DOUBLE); // Forward step at DOUBLE speed because it has to be reversed for some reason
}
AccelStepper stepperAS(forwardstep, backwardstep); // Makes the AccelStepper work with the steppermotor connected to the shield
void calibrateStepper() { // Calibration sequence
Serial.println("Calibrating..."); // Prints Calibration start
while (digitalRead(Home) == LOW) { // Inserts a low value when the stepper is not at home
digitalWrite(Led, LOW); // Turns LED off when the stepper is not at home
stepperAF.step(1, FORWARD, MICROSTEP); // Makes the stepper move at double speed until its at home
}
digitalWrite(Led, HIGH); // Turns LED on when the stepper is home
stepperAS.setCurrentPosition(0); // Calibrates current position as home
Serial.println("Calibration done"); // Prints Calibration succesful
delay(1500); // Wait for 1.5 seconds
digitalWrite(Led, LOW); // Turns LED off and stops calibration sequence
}
void setup() {
//Serial.begin(115200); // Begins serial input
Serial.begin(9600);
pinMode(Home, INPUT); // Home = input
pinMode(Led, OUTPUT); // LED = output
calibrateStepper(); // Starts calibration sequence
stepperAS.setAcceleration(2000); // Sets Acceleration at max
stepperAS.setSpeed(Speed); // Sets speed of stepper
stepperAS.setMaxSpeed(MaxSpeed); // Sets max speed of stepper
}
void loop() {
int sensorReading = analogRead(sensorPin);
int range = map(sensorReading, sensorMin, sensorMax, 0, 359);
// printing at 9600 baud means for each character
// serial.print needs
// 10 bits / 9600 bits per second = 0.00104 seconds
BeforePrint = micros();
Serial.println(range);
int Motorrange = map(range, motorMin, motorMax, 0, 2048);
if (Motorrange > 2048) {
Motorrange -= 2048;
}
Serial.println("Motorrange:");
Serial.println(Motorrange);
AfterPrint = micros();
AfterRunPos;
static int pos;
if (pos < Motorrange) {
BeforeRunPos = micros();
stepperAS.runToNewPosition (++pos);
AfterRunPos = micros();
}
else if (pos > Motorrange) {
BeforeRunPos = micros();
stepperAS.runToNewPosition (--pos);
AfterRunPos = micros();
}
else {
Serial.print("time to print=");
Serial.print(AfterPrint - BeforePrint);
Serial.println("microseconds");
Serial.print("time for RunPos=");
Serial.print(AfterRunPos - BeforeRunPos);
Serial.println("microseconds");
}
}
best regards Stefan
The Adafruit motorshield V1.2 uses
a shift-register 74-595 to create the signals
I did not analyse how accelstepper is able to deal with this special kind of bit-banging to create the signals that make the motor rotate
Another idea is to setup a timer-interrupt that calls this part of the code
if (pos < Motorrange)
stepperAS.runToNewPosition (++pos);
else if (pos > Motorrange)
stepperAS.runToNewPosition (--pos);
How about buying this stepper-driver?
best regards Stefan
I tried the code you sent and the motor doesnt turn any faster then gc's code. its saying in the serial that it takes around 1500 to 2000 microseconds to print and 20000 to 22000 microseconds to "RunPos" removing all the serial print stuff doesnt make it faster either...
I have just found out the the stepper really doesnt have to turn fast at all. but i still think that what its doing as of rightnow is not good for the motor. but how slow it is going right now is still too slow. it has to be around 5-10 rpm for the application it will be used for.
I will buy the driver module you sent and try that out.
This code is not about making the code-execution faster it is just to measure how much time takes it to print to the serial monitor.
As the printing takes around 2000 microseconds this means
only once every 2 milliseconds a new step-pulse is created.
This means with this code the step-pulse-frequency is around 500 to 600 steps per second
The motor should run faster if all serial printing is really commented out.
Can you please post all code-variants as complete sketches that you have tested
Additionally you should post the content that is printed to the serial monitor as a code-sections too.
best regards Stefan
this is the main code i used to remove all the unnecessary code and make it the fastest it could be. this is also with the setSpeed setMaxSpeed and set Acceleration. the library said that it should be called alot. even without these settings it is as slow as the code with all the serial print stuff.
void loop() {
int sensorReading = analogRead(sensorPin);
int range = map(sensorReading, sensorMin, sensorMax, 0, 359);
int Motorrange = map(range, motorMin, motorMax, 0, 2048);
stepperAS.setAcceleration(Acceleration);
stepperAS.setSpeed(Speed);
stepperAS.setMaxSpeed(MaxSpeed);
static int pos;
if (pos < Motorrange)
stepperAS.runToNewPosition (++pos);
else if (pos > Motorrange)
stepperAS.runToNewPosition (--pos);
}
So i dont think?? might be wrong but i dont think this is how you thought it would work? its moving to the point i set it to but then only printing the RunPos when it has already went to the position. so its not counting the time it took from the starting point to the point i set it to.
Also this is back to 9600 baudrate using the code you gave me
this is a bit of the serial idk how to put more in because when i scroll the selection goes away.
time to print=23772microseconds
time for RunPos=296microseconds
111
Motorrange:
633
112
Motorrange:
638
113
Motorrange:
644
113
112
Motorrange:
638
112
Motorrange:
638
time to print=23468microseconds
time for RunPos=296microseconds
112
Motorrange:
638
time to print=23764microseconds
time for RunPos=296microseconds
113
Motorrange:
644
113
Motorrange:
644
112
Motorrange:
638
112
Motorrange:
638
112
Motorrange:
638
time to print=23460microseconds
time for RunPos=304microseconds
112
Motorrange:
638
time to print=23772microseconds
time for RunPos=304microseconds
113
Motorrange:
644
113
Motorrange:
644
It is very easy to post a complete sketch
With Arduino-IDE 1.8.X
Do a rightklick with the mouse
choose copy for forum
With Arduino IDE 2.0.X
and then
change back to browser with forum and press Ctrl-V to insert the complete sketch.
That will be even faster than trying to mark just the function void loop()
What you have posted is not a complete sketch.
With just the code of void loop() I am unable to check to what values you have set
stepperAS.setAcceleration(Acceleration);
stepperAS.setSpeed(Speed);
stepperAS.setMaxSpeed(MaxSpeed);
2nd it is unnescessary to do these function-calls with every iteration of loop.
You tried to reduce the code in a way that slows down the code-execution for step-pulse-creation.
This makes it very likely that you have done more things wrong in the code that is outside void loop()
This is the reason why you should post
EXACTLY
that
COMPLETE SKETCH
that you used for your latest test.
best regards Stefan
Thanks for explaining here is the code. this is without the settings in it and the latest version without all the serial printing.
#include <AccelStepper.h>
#include <AFMotor.h>
#include <Multistepper.h>
const int Home = 2; // Home switch for calibration
const int Led = 13; // Led for identifying when calibration is done
const int sensorPin = A0; // Potentiometer for turning the motor
const int sensorMin = 1; // Potentiometers minimum
const int sensorMax = 1020; // Potentiometers maximum
int Speed = 800; // Value for speed of the stepper
int MaxSpeed = 800; // Value for max speed of the stepper
int Acceleration = 1500;
const int motorMin = 0; // Minimum value for the map of the stepper
const int motorMax = 359; // Maximum value for the map of the stepper
const int stepsPerRevolution = 2048;
AF_Stepper stepperAF(102.5, 2); // Identifies the stepper motor
void forwardstep() { // Forward step for AccelStepper to work
stepperAF.onestep(BACKWARD, DOUBLE); // Backward step at DOUBLE speed because it has to be reversed for some reason
}
void backwardstep() { // Backward step for AccelStepper to work
stepperAF.onestep(FORWARD, DOUBLE); // Forward step at DOUBLE speed because it has to be reversed for some reason
}
AccelStepper stepperAS(forwardstep, backwardstep); // Makes the AccelStepper work with the steppermotor connected to the shield
void calibrateStepper() { // Calibration sequence
Serial.println("Calibrating..."); // Prints Calibration start
while (digitalRead(Home) == LOW) { // Inserts a low value when the stepper is not at home
digitalWrite(Led, LOW); // Turns LED off when the stepper is not at home
stepperAF.step(1, FORWARD, MICROSTEP); // Makes the stepper move at double speed until its at home
}
digitalWrite(Led, HIGH); // Turns LED on when the stepper is home
stepperAS.setCurrentPosition(0); // Calibrates current position as home
Serial.println("Calibration done"); // Prints Calibration succesful
delay(1500); // Wait for 1.5 seconds
digitalWrite(Led, LOW); // Turns LED off and stops calibration sequence
}
void setup() {
Serial.begin(9600); // Begins serial input
pinMode(Home, INPUT); // Home = input
pinMode(Led, OUTPUT); // LED = output
calibrateStepper(); // Starts calibration sequence
stepperAS.setAcceleration(Acceleration); // Sets Acceleration of the stepper
stepperAS.setSpeed(Speed); // Sets speed of stepper
stepperAS.setMaxSpeed(MaxSpeed); // Sets max speed of stepper
}
void loop() {
int sensorReading = analogRead(sensorPin);
int range = map(sensorReading, sensorMin, sensorMax, 0, 359);
int Motorrange = map(range, motorMin, motorMax, 0, 2048);
static int pos;
if (pos < Motorrange)
stepper1.runToNewPosition (++pos);
else if (pos > Motorrange)
stepper1.runToNewPosition (--pos);
}
Saw it wrong in the Accelstepper library accidentally. just read that for example "setAcceleration" should be called as little as possible... oops
Thanks alot
exit status 1
'stepper1' was not declared in this scope
The code you have posted above is
not
the code that was running on your arduino
I can tell this
101% sure
because the code that you have posted does not compile
Sorry its really early still im barely awake...
#include <AccelStepper.h>
#include <AFMotor.h>
#include <Multistepper.h>
const int Home = 2; // Home switch for calibration
const int Led = 13; // Led for identifying when calibration is done
const int sensorPin = A0; // Potentiometer for turning the motor
const int sensorMin = 1; // Potentiometers minimum
const int sensorMax = 1020; // Potentiometers maximum
int Speed = 800; // Value for speed of the stepper
int MaxSpeed = 800; // Value for max speed of the stepper
int Acceleration = 1500;
const int motorMin = 0; // Minimum value for the map of the stepper
const int motorMax = 359; // Maximum value for the map of the stepper
const int stepsPerRevolution = 2048;
AF_Stepper stepperAF(102.5, 2); // Identifies the stepper motor
void forwardstep() { // Forward step for AccelStepper to work
stepperAF.onestep(BACKWARD, DOUBLE); // Backward step at DOUBLE speed because it has to be reversed for some reason
}
void backwardstep() { // Backward step for AccelStepper to work
stepperAF.onestep(FORWARD, DOUBLE); // Forward step at DOUBLE speed because it has to be reversed for some reason
}
AccelStepper stepperAS(forwardstep, backwardstep); // Makes the AccelStepper work with the steppermotor connected to the shield
void calibrateStepper() { // Calibration sequence
Serial.println("Calibrating..."); // Prints Calibration start
while (digitalRead(Home) == LOW) { // Inserts a low value when the stepper is not at home
digitalWrite(Led, LOW); // Turns LED off when the stepper is not at home
stepperAF.step(1, FORWARD, MICROSTEP); // Makes the stepper move at double speed until its at home
}
digitalWrite(Led, HIGH); // Turns LED on when the stepper is home
stepperAS.setCurrentPosition(0); // Calibrates current position as home
Serial.println("Calibration done"); // Prints Calibration succesful
delay(1500); // Wait for 1.5 seconds
digitalWrite(Led, LOW); // Turns LED off and stops calibration sequence
}
void setup() {
Serial.begin(9600); // Begins serial input
pinMode(Home, INPUT); // Home = input
pinMode(Led, OUTPUT); // LED = output
calibrateStepper(); // Starts calibration sequence
stepperAS.setAcceleration(Acceleration); // Sets Acceleration of the stepper
stepperAS.setSpeed(Speed); // Sets speed of stepper
stepperAS.setMaxSpeed(MaxSpeed); // Sets max speed of stepper
}
void loop() {
int sensorReading = analogRead(sensorPin);
int range = map(sensorReading, sensorMin, sensorMax, 0, 359);
int Motorrange = map(range, motorMin, motorMax, 0, 2048);
static int pos;
if (pos < Motorrange)
stepperAS.runToNewPosition (++pos);
else if (pos > Motorrange)
stepperAS.runToNewPosition (--pos);
}
Ill try not making a mistake like that again. the code above i have tried and worked on my arduino. still with the same problem of it going too slow..
Are you still using this motorshield???
This motorshield requires to use the library AFMotor.h as a must.
This motorshield drives the L293D through a shift-register.
It is very very very unlikely that the AccelStepper-library can drive a stepper-motor with a adafruit-motorshield V1.2
You include the adafruit-library
#include <AFMotor.h>
AF_Stepper stepperAF(102.5, 2); // Identifies the stepper motor
but your code uses the accelstepper-library
AccelStepper stepperAS(forwardstep, backwardstep); // Makes the AccelStepper work with the steppermotor connected to the shield
AccelStepper stepperAS(forwardstep, backwardstep);
if (pos < Motorrange)
stepperAS.runToNewPosition (++pos);
else if (pos > Motorrange)
stepperAS.runToNewPosition (--pos);
...
if (pos < Motorrange)
stepperAS.runToNewPosition (++pos);
else if (pos > Motorrange)
stepperAS.runToNewPosition (--pos);
...