I can move the stepper fine when I don't use Serial print, but the problem comes when I am trying to move the stepper and do Serial print at the same time(the stepper spins really slow). I am guessing it's because the delay caused by serial commands. So, is it possible to do this?
What I am trying to do is created a closed loop system where I can move the stepper to certain angle with the help of a Rotary Encoder. I am also thinking to use Arduino Due, as i read that it can do multiple loops at the same time.
Any ideas will be appreciated. THANKS
The Arduino Due cannot do multiple loops at the same time any more than any other Arduino. It really depends upon YOUR programming and what you mean by "at the same time".
Some software can give the appearance of "at the same time".
Some software uses delay(...) or other blocking techniques and destroys the illusion of "at the same time".
this is a test code of moving stepper and serial print at the same time. The stepper moves really slow.. but, if I remove the
Serial.println("TEST")
stepper moves fine.
// defines pins numbers
const int stepPin = 10;
const int dirPin = 11;
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(1500);
digitalWrite(stepPin,LOW);
delayMicroseconds(1500);
Serial.println("test");
}
}
You generate text faster than it can be transferred, so Serial.println() blocks and slows your loop down.
With Serial.begin(9600) you can transfer roughly 1000 chars a second, your for loop generates 200 x 6, taking longer than a second.
If you want do do things in parallel, delay is not very useful, see BlinkWithoutDelay.
Whandall:
You generate text faster than it can be transferred, so Serial.println() blocks and slows your loop down.
With Serial.begin(9600) you can transfer roughly 1000 chars a second, your for loop generates 200 x 6, taking longer than a second.
If you want do do things in parallel, delay is not very useful, see BlinkWithoutDelay.
If you use the BWoD technique (also illustrated in Several Things at a Time) you can arrange so that the Serial.print() is only called (say) once per second and that way the serial output buffer will not overflow and block.