StepperMotor + OLEDwithEncoder Navigation - Interruption Issues

Hi Community!

I have a new Project where I am working on.
With an ESP32-Wroom Board I try to implement the functionality of an Stepper Motor which should spin in different directions. For Settings//Navigation and so on, i also added an KY-040 Rotary Encoder and an I2C OLED Display. Also, a active BLE Connection is active.

So far so good - developement was done in two different branches, the encoder+the navigation in the OLED Display works great and also the stepper functionality is like it should be in combination with BLE.

My Problem is now, if I am marrying those two parts together everything works as excepted BUT ONLY(!!!) if the Encoder stays untouched! The ISR used to check the encoder state is interrupting in such a way, that the stepper stucks visibile and turns like it should if the encoder is in its final position :frowning:

Does somebody else have experience with an behaviour like this and can help me to solve that problem?
My first thought was, I move the Stepper Motor Control function to core 1. But I am not sure how to do this and another aspect is, that the whole BLE functionality is still running in core 1…

Stepper Functionality in main loop is quite simple:

if(StepperRunning && StepperDirection){
way = TURNFULL;
StepperDirection = false;
//DEBUG_SERIAL.println(“Direction CLK”);
StepperRunning = false;
}else if (StepperRunning && !StepperDirection){
way = -TURNFULL;
StepperDirection = true;
//DEBUG_SERIAL.println(“Direction CCLK”);
StepperRunning = false;
}

if (!StepperRunning){
StepperRunning = myStepper.runToPosition(way);
}

the runToPosition Class Function i use is:

bool StepperControl::runToPosition(long absolut){

if(_positionNEW != absolut){
    _Stepper->setCurrentPosition(0);
    _Stepper->moveTo(absolut);
    _positionNEW = absolut;
    DEBUG_SERIAL.print("[Stepper] New Position Recieved: ");
    DEBUG_SERIAL.println(_positionNEW);
}

if((_positionNEW > stepperPosition() && _positionNEW >= 0) || (_positionNEW < stepperPosition() && _positionNEW < 0)){
    
    if((_Stepper->speed() >= _maxSpeed && _positionNEW >= 0) || (abs(_Stepper->speed()) >= _maxSpeed && _positionNEW <0)){
        _state = RSPD;
    }

    switch (_state){
        case RSPD:
            _Stepper->runSpeed();
            break;
        case RJSTR:
            _Stepper->run();
            break;
        default:
            break;
        }

    return false;
}else{
    _state = RJSTR;
    _positionNEW = 0;
    DEBUG_SERIAL.print("[Stepper] Position reached! ");
    DEBUG_SERIAL.println(_Stepper->currentPosition());
    return true;
}
};

Please edit your post to include all the code, using code tags. The error is very often in the part not posted.

In detail, i do not think it is an coding issue at all, i think it is an project-strategy issue:

I did more detail thinking about it:
If i use an ISR for Encoder reading, it will for sure ALWAYS Interrupting my main loop and my stepper motor moving (lol for sure that is ISR doing -__-).

So i think i need an alternative way for one of two thinks:

  1. Keep controlling the stepper if the source code is interrupted. The stepper uses an ULN2003 driver stage.
  2. Avoid Interrupting sourcecode during reading the Encoder. ENCODER Reading is done with the esp32 encoder libary and is based on ISR.
#include <ESP32Encoder.h> // https://github.com/madhephaestus/ESP32Encoder.git 

ESP32Encoder encoder;
  encoder.attachHalfQuad(encoderPinB,encoderPinA);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.