Hello guys, I have a question to ask about the stepper motor. I'm using AccelStepper library for this project. The code I type didn't working as expected. This is what I wanted to do.
- I set a target for the motor, for eg: 2048 steps.
- For every 200 steps, the current steps will be save into an array. For eg: 200, 400, 600, 1000, 1200, 1400, 1600, 2000, 2048.
How can I possibly do this? I have a piece of code, of course it is not working. Forgive me, I need your help.
#include <AccelStepper.h>
#include <MultiStepper.h>
#define blue 8
#define pink 9
#define yellow 10
#define orange 11
AccelStepper stepper(AccelStepper::FULL4WIRE, orange, pink, yellow, blue); //clockwise
int target = 2048;
int steps = 0;
int checkpoint = 200;
void setup()
{
Serial.begin(9600);
stepper.setSpeed(125);
stepper.setAcceleration(40);
stepper.setMaxSpeed(1000);
}
void loop()
{
int n = 0;
int reading[] = {};
target = stepper.targetPosition();
steps = stepper.currentPosition();
if(steps <= target)
{
for(steps = 0; steps <= checkpoint; steps++)
{
stepper.moveTo(steps);
stepper.run();
}
reading[n] = steps;
n++;
Serial.println(reading[n]);
}
}
The array reading give me so random number and the motor didn't work as I expected. Hope you guys can help me >.< Thanks.
int reading[] = {};
This declares an array with no elements. When you save data to it you use memory that is not part of the array.
Declare the array with the number of elements that you will use remembering that they are numbered from zero not one
I don't understand what your code should do. Actually it's bogus, and why use an array if you only use the last value in it?
UKHeliBob:
int reading[] = {};
This declares an array with no elements. When you save data to it you use memory that is not part of the array.
Declare the array with the number of elements that you will use remembering that they are numbered from zero not one
So it must declare some numbers only it will work?
DrDiettrich:
I don't understand what your code should do. Actually it's bogus, and why use an array if you only use the last value in it?
Actually I wanted to save step for my stepper motor. For example, my motor need to move 2048 steps to the target. For every 400 steps, the reading will save into an array. 400 1600 2000 2048 something like this. Like I said, if my code is working, I won't be here for asking zzz.
So it must declare some numbers only it will work?
In order to store say ints in an array it needs to be declared like this
int anArray[6];
Whether this will make your code do what you want I don't know
If you know where the stepper will stop then why bother saving the values ?
menloon:
- I set a target for the motor, for eg: 2048 steps.
- For every 200 steps, the current steps will be save into an array. For eg: 200, 400, 600, 1000, 1200, 1400, 1600, 2000, 2048.
Saving values like this into an array isn't going to do anything useful for you, right?
You're probably better off just counting the number of step signals that you send out .... such as count each step instruction and keep track of direction... ie. clockwise or anti-clockwise direction.
Assuming there's no issues like missed steps, then the method of counting the steps might be ok.
If angle is important, then you might be able to get a particular kind of a stepper motor that has two shafts ... and fit an absolute encoder onto one of the shafts.
Messir7:
Hello,
I need for use structure on Arduino
Thanks
And what is that supposed to mean?
menloon:
- I set a target for the motor, for eg: 2048 steps.
- For every 200 steps, the current steps will be save into an array. For eg: 200, 400, 600, 1000, 1200, 1400, 1600, 2000, 2048.
This sounds like a typical XY Problem. Please describe the project you are hoping to implement.
It doesn't make any sense to me to save the values into an array after the moves have happened. Why not just populate the array when you write the program?
If you command the stepper to move 2048 steps then that is what it will do. If the load is too great and the motor misses steps the steps will still be executed and you will have no means to know how many steps have been missed.
...R
Stepper Motor Basics
Simple Stepper Code
Robin2:
This sounds like a typical XY Problem. Please describe the project you are hoping to implement.
To me it sounds more like a way overdue school assignment, that is, if OP is actually still working on it after 7 months