Stepper motor speed control by encoder

Hi here, I am a newbee in programming. Trying to control stepper with rotary encoder. Checked many sources but did find exactly what I need, Can somebody have a look and advise. I like to increase/decrease the speed by 1 RPM every time I am turning the encoder left/right.

Thanks

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

volatile boolean TurnDetected;  // need volatile for Interrupts
volatile boolean rotationdirection;  // CW or CCW rotation

const int PinCLK=2;   // Generating interrupts using CLK signal
const int PinDT=3;    // Reading DT signal
const int PinSW=4;    // Reading Push Button switch

int rpm = 60;   // counter for rpm, start value = 60


// Interrupt routine runs if CLK goes from HIGH to LOW
void isr ()  {
delay(4);  // delay for Debouncing
   if (digitalRead(PinCLK))
    rotationdirection= digitalRead(PinDT);
  else
    rotationdirection= !digitalRead(PinDT);
TurnDetected = true;
}

void setup ()  {
  
pinMode(PinCLK,INPUT);
pinMode(PinDT,INPUT);  
pinMode(PinSW,INPUT);
digitalWrite(PinSW, HIGH); // Pull-Up resistor for switch
attachInterrupt (0,isr,FALLING); // interrupt 0 always connected to pin 2 on Arduino UNO

 // set the speed at default rpm:
  myStepper.setSpeed(rpm);

}


void loop ()  {

myStepper.step(stepsPerRevolution); //starts stepper ar default speed

// Runs if rotation was detected
if (TurnDetected) {
TurnDetected = false; // do NOT repeat IF loop until new rotation detected

  
// increase/decrease the speed
    if (rotationdirection) { // incerease speed
    rpm++;
    myStepper.setSpeed(rpm);
    myStepper.step(stepsPerRevolution);
    rpm = rpm++;  // save current rpm value into previous variable 

    }

    if (!rotationdirection) { { // decrease speed
    rpm--;
    myStepper.setSpeed(rpm);
    myStepper.step(stepsPerRevolution);
    rpm = rpm--;  // save current rpm value into previous variable 
    }
  }
}
}

You have not told us what actually happens when you run your program.

You don't seem to have any Serial.print() statements in your program that would allow you to see if the encoder movements are being properly detected.

My wild guess is that the problem lies with the standard Stepper library because it blocks the Arduino from doing anything else until all the steps are complete. If that is the problem either try moving the motor one step at a time with your program controlling the interval between steps or look at the more comprehensive AccelStepper library

...R
Stepper Motor Basics
Simple Stepper Code

Hi Robin, thanks for the reply.
Actually I did not tested the program yet, as I have my test bench my work only.
The blocking of Arduino in this case should not be an issue. I don't want to do anything else while running the stepper (this is actually a peristaltic pump). Only when encoder is turned, I like to change rpms of the stepper.
In general do you think that this approach will work? Mostly concerned about the variable "RPM" changing based on rotation of encoder?
thanks

peitroc:
Hi Robin, thanks for the reply.
Actually I did not tested the program yet, as I have my test bench my work only.

And what if your program is working properly - then we would all be wasting our time.

The blocking of Arduino in this case should not be an issue. I don't want to do anything else while running the stepper (this is actually a peristaltic pump). Only when encoder is turned, I like to change rpms of the stepper.

Does that mean that you only want to dial in another speed when the motor is stationary? If so, then blocking stepper code won't matter.

...R