Distance sensor interrupts stepper motor motion

Hi all. I am trying to power a stepper motor using the code below. Currently, I am struggling to control the stepper motor and make readings with my distance sensor. I want to know whether it is possible to make readings with the Arduino while not interrupting the motion of the stepper motor (e.g. that the stepper motor keeps rotating without a pause).

Currently, the stepper motor moves 50 steps, prints its position subsequently reads the analog pin A0 for the measurement of the distance sensor. However, the motor stops turning while making the measurement. I am not sure how to edit my code, could someone help me?

I am using a TB6600 stepper motor driver, Arduino Uno.

Thanks in advance.

//--------------------(Main Code)---------------------------//
void loop() {
 //if the system has not been homed, home system and save the home position
  if(homeStatus == notHomed  || homeStatus == pressedDuringHome){
      if(homeStatus == notHomed){
        debugln("Homing system, stay clear of machine");
        digitalWrite(stepperEnablePin, LOW);//Enable the motor
                        
        //run the stepper reverse until the rear limit switch is reached
        stepper.moveTo(homeTravelDistance);
        stepper.setMaxSpeed(stepperHomeSpeed);
        while(homeStatus == notHomed){
          stepper.run();
          }
        //set position as 0
        stepper.setCurrentPosition(0);
          }
        if(homeStatus == pressedDuringHome){  
          debugln("Rear limit hit during homing, backing off switch");
          digitalWrite(stepperEnablePin, LOW);//Enable the motor
                        
          //run the stepper reverse until the rear limit switch is reached
          stepper.moveTo(-stepsToGetOffLimitSwitchDuringHoming);
          debugln("susped");
          stepper.runToPosition();
          debugln("@");
          debugln("done backing off limit, ready to enter run");
          //set position as 0
          stepper.setCurrentPosition(0);
          homeStatus = homed;       
        }

      }

      //while homingStatus == homed, continiously run back-n-forth between the limits till loopcount is reached
      while(homeStatus == homed && loopcount < readings){
        loopcount++;
        Serial.print("Measurement :");
        Serial.println(loopcount);
        //On the first run, write to the serial monitor so we know
        if(debugStatus != runSequence){
        debugln("Entered run sequence for the first time");   
        debugStatus = runSequence;
      }

                
      //set the stepper speed and acceleration to the forward settings
      stepper.moveTo(-runDistance);
      stepper.setMaxSpeed(stepperForwardRunSpeed);
      stepper.setAcceleration(stepperForwardRunAcceleration);
                

      //run stepper until the runDistance has been traveled (UNLESS either limit is hit)
      while (stepper.distanceToGo() != 0 & homeStatus == homed){
        stepper.run();
                       
        if (stepper.currentPosition() % 50 == 0) {
        // Print a line of code for every 50 steps
          Serial.print("Pos : ");
          Serial.println(stepper.currentPosition()); 
          readvoltage();
          Serial.print("Raw : ");
          Serial.println(sum/repeats);
        }
      } 

      //this stops the motor as quickly as possible while using accel+decell (May take trial and error during setup to find a setting that doesnt hit the limit switch)
      stepper.stop(); // Stop as fast as possible: sets new target
      stepper.runToPosition(); 
               
      //Write to the serial monitor (see DEBUG.h tab)
      debugln("Forward movement complete, reversing");

      // Now, tell the stepper to reverse back to the home position
      stepper.moveTo(0);
      stepper.setMaxSpeed(stepperRearRunSpeed);
      stepper.setAcceleration(stepperRearRunAcceleration);

      // Full speed basck to 0 (UNLESS either limit switch is hit)
      while (stepper.distanceToGo() != 0 & homeStatus == homed){ 
        stepper.run();
      }
                               
      //this stops the motor as quickly as possible while using accel+decell
      stepper.stop(); // Stop as fast as possible: sets new target
      stepper.runToPosition();               

      //write to the serial monitor 
      debugln("Reverse movement complete, pausing for a second, then sending forward");
      delay(1000);
    }
        
  stepper.disableOutputs();         // Disables all voltages to the motor, prevent overheating etc. 

  //if an error occurs, disable the motor and wait for operator to check/reset
  while(homeStatus == error){
  debugln("An error has occured, check machine and reset when ready");
  digitalWrite(12, HIGH);//disable the motor
  delay(5000);
  }
} // end of void loop
 
void readvoltage() {
  sum = 0;
  for (int j = 0; j < repeats; j++) {
  sum += analogRead(0);
  delay(3); // choose a short delay that does not add to 20msec.
  }
}

As a start, don't write blocking code. Also, you need to be careful with choice of operators in if statements. '&' is bitwise, "&&" is logical and.

code is hard to follow. i would expect there to be one place in the code where the stepper is driven

if the stepper needs to be stopped immediately because of some sensor reading, the i would expect it to be stepped one step at a time after each sensor reading.

looks like readvoltage() is accumulating multiple sensor reading which may be time consuming, but i don't see where that sum is used beside in a print

also, homing should be done once in setup() and there no need to test whether the system is homed in loop()

Did you get any progress on it?

using the MobaTools-library instead of the stepper- or the accelStepper-library
offers to

set and forget making the stepper-motor move.

The MobaTools are able to do this through step-creation based on a timer-interrupt
still you can immidiately stop the stepper-motor at any time.

You can install the MobaTools with the library-manager of the arduino-IDE
best regards Stefan

1 Like

Yes, it worked. What do you want to know?

Bosr, I think I am having exactly the same problem ie. my distance sensor measurement is making the stepper motor stop momentarily.

FYI I am using the AccelStepper library with an Arduino ESP32 Nano baord.

What stepper library did you use? And can you post a code snippet that shows how you solved this problem?

thanks

The MobaTools-library solves the problem so to say
inside of the library
because step-pulse-creation is done by a timer-interrupt.

this enables to run code "in parallel"

best regards Stefan

Thanks for the info Stefan.

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