Problems Combining Two Working Sketches

I have one sketch that will rotate a stepper motor in one direction, and another sketch that will read the position of an absolute encoder.

When I combine the two sketches, the stepper motor does not turn properly due to the two serial.print lines in the void loop. When I comment these two lines out, the stepper motor will turn proprly but I obviously cannot get a position reading.

Why are these two serial.print lines not allowing the stepper to turn properly?

#include<Stepper.h>

//Stepper
const int StepperStepPin = 10;
const int StepperDirectionPin = 9;
const int StepperMaxRPM = 100;
Stepper stepper(800, StepperStepPin, StepperDirectionPin);

//Encoder
int val; 
int encoder0PinA = 6;
int encoder0PinB = 7;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

void setup() {

   stepper.setSpeed(StepperMaxRPM);
   
   pinMode (encoder0PinA,INPUT);
   pinMode (encoder0PinB,INPUT);
   Serial.begin (9600);
   

}

void loop() {
  
       stepper.step(1);
  
      
       n = digitalRead(encoder0PinA);
       if ((encoder0PinALast == LOW) && (n == HIGH)) {
       if (digitalRead(encoder0PinB) == LOW) {
       encoder0Pos--;
       } else {
       encoder0Pos++;
       }
       Serial.print (encoder0Pos);//Line causing prolbem
       Serial.print ("/");//Line causing problem
       } 
       encoder0PinALast = n;
       
       
}

wickedhurricane: When I combine the two sketches, the stepper motor does not turn properly due to the two serial.print lines in the void loop. When I comment these two lines out, the stepper motor will turn proprly but I obviously cannot get a position reading.

What do you mean by "does not turn properly"? I suspect that your motor still turns, but at a slower speed. Is that what happens?

You could try setting a higher baud rate for your serial comms. Try 115200 or 57600 baud and see if it makes a difference.

Yes, the motor would run slower and make some strange sounds.

Thank you, increasing the baud rate to 115200 helped, but its still not running as good as when the the serial.print lines were omitted.

Why does incresing the baud rate make a difference when both sketches were independently able to run at 9600?

And are there any other options beside increasing the baud rate? I have other working scripts that I would like to incorporate which are currently running at 9600 and I'm not sure how this increased rate will affect everything.

wickedhurricane: Why does incresing the baud rate make a difference when both sketches were independently able to run at 9600?

How fast was the encoder spinning when used independently?

If there're other things going on in the program, you can't accurately read fast turning encoders by polling the pins. You will probably need to use interrupts to read the encoder.

wickedhurricane:
Why are these two serial.print lines not allowing the stepper to turn properly?

Serial.print() is a slow function and reading an encoder can require a lot of Arduino MCU cycles so the print statements are probably slowing the code too much.

As @DuaneDegn has said you should probably use interrupts to detect the encoder. And don’t have any print statements inside the Interrupt Service Routine (ISR)

Also, do not expect to print the value of the encoder for every single encoder position unless the shaft with the encoder is turning slowly.

…R

i'll look into the interrupts.

thank you both, this was very helpful.