Armindo:
Robin2 Can you give an example, how you would incorporate this second part of code (Simple Stepper Program - Motors, Mechanics, Power and CNC - Arduino Forum) into this code (I am using code similar to this functions recvWithStartEndMarkers() and parseData() ): Serial Input Basics - Programming Questions - Arduino Forum
It will be much easier to help if you make an attempt at what you require and post your code if it does not do what you want.
If you want to experiment with simple acceleration you might like to try this program
// testing a stepper motor with a Pololu A4988 driver board or equivalent
// this version uses micros() to manage timing to allow high step rates to be tested
// and illustrates a simple method for accleration and deceleration
byte directionPin = 9;
byte stepPin = 8;
unsigned long curMicros;
unsigned long prevStepMicros = 0;
unsigned long slowMicrosBetweenSteps = 6000; // microseconds
unsigned long fastMicrosBetweenSteps = 1500;
unsigned long stepIntervalMicros;
unsigned long stepAdjustmentMicros;
int numAccelSteps = 100; // 100 is a half turn of a 200 step mmotor
int numSteps = 1000;
int stepsToGo;
byte direction = 1;
void setup() {
Serial.begin(115200);
Serial.println("Starting Stepper Demo with acceleration");
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
stepAdjustmentMicros = (slowMicrosBetweenSteps - fastMicrosBetweenSteps) / numAccelSteps;
stepIntervalMicros = slowMicrosBetweenSteps;
stepsToGo = numSteps;
digitalWrite(directionPin, direction);
}
void loop() {
moveMotor();
}
void moveMotor() {
if (stepsToGo > 0) {
if (micros() - prevStepMicros >= stepIntervalMicros) {
prevStepMicros += stepIntervalMicros;
singleStep();
stepsToGo --;
if (stepsToGo <= numAccelSteps) {
if (stepIntervalMicros < slowMicrosBetweenSteps) {
stepIntervalMicros += stepAdjustmentMicros;
}
}
else {
if (stepIntervalMicros > fastMicrosBetweenSteps) {
stepIntervalMicros -= stepAdjustmentMicros;
}
}
}
}
else {
direction = ! direction;
digitalWrite(directionPin, direction);
// next two lines just for the demo
delay(2000);
Serial.println("Changing direction");
stepsToGo = numSteps;
prevStepMicros = micros();
}
}
void singleStep() {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
...R
PS please post your program using the code button </> so it looks like the above. See How to use the Forum
Also please use the AutoFormat tool in the IDE to indent the code properly as that makes it much easier to read.