How can I stop a Stepper Motor activate with an ultrasonic sensor

Hey! I've been working on a lab for school, but I found myself at a loss as to how to modify my code.

Basically what I'm trying to do is the following:
First, the ultrasonic sensor detects presence at less than or equal to 10cm and this activates the motor in the clock hands for 2 seconds and stops, this until the sensor stops detecting presence, and the motor does not activate until 2 seconds have passed without detect presence, then the motor rotates in the opposite direction for 2 seconds.

#include <NewPing.h>
#include <Stepper.h>
#define stepsPerRevolution 2048
Stepper myStepper(stepsPerRevolution, 25, 26, 27, 14);
int dt= 2000;

#define TRIGGER_PIN  4  // Pin que se usara para el TRIGGER que recibe el sensor
#define ECHO_PIN     2  // Pin que se usara el el ECHO que enviara el sensor
#define MAX_DISTANCE 400 // Distancia maxima que puede medir el sensor

NewPing ultrasonico(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Se configura el objeto del sensor que usara los datos

void setup() {
  myStepper.setSpeed(30);
  Serial.begin(115200);

}

float duracion, distancia;

void loop() {

  duracion = ultrasonico.ping();

  distancia = (duracion / 2) * 0.0343;
  Serial.print("Distancia: ");

  if (distancia >= 10 || distancia <= 2) {
    Serial.println("Fuera de rango.");
    myStepper.step(stepsPerRevolution);
    delay(dt);
    
  } else{
    Serial.print(distancia);
    Serial.println(" cm");
    myStepper.step(-stepsPerRevolution);
    delay(dt); 
  }
}

my code so far can only make it turn in different directions depending on whether it detects presence or not, I'm not really sure how to stop the motor

Read the sensor between every step and stop pulsing at the desired value read from the sensor.

Your sketch says,

"If distancia is over 10 or under 2, move forward one revolution then wait two seconds"
else
(implied "distancia is under 10 and over 2") "move backward one revolution and wait"

Which means, the motor will always be moving. Make a third "if" condition...

The first "if" should test "if distancia is under 2, move motor backward"
The second "if" should be "if distancia is over 10, do not move motor (or turn)"
The third "if" should be "if distancia is under 10, move motor (turn? forward?)"

Experiment.

The Stepper library step() function blocks. That is if you tell it to take 2048 steps it will do so and ignore anything telling it differently until the steps are complete.

If you want to be able to interrupt the stepping, you will need to use a library that has non-blocking movement functions. The AccelStepper, MobaTools stepper and other libraries have such functions. I prefer the MobaTools library. It is available for installation via the IDE library manager.

Here is a demo using the MobaTools library that will run the motor if the range is over 10cm and will quickly stop if the range falls under 10cm. Change pin numbers as necessary.

#include <MobaTools.h>
#include <NewPing.h>

const byte TRIGGER_PIN = 2;
const byte ECHO_PIN = 4;
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

MoToStepper stepper(2048, FULLSTEP);
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);


void setup()
{
   Serial.begin(115200);

   stepper.attach( 8, 9, 10, 11);
   stepper.setSpeed( 150 );
   stepper.setRampLen( 10);
   stepper.setZero();
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 100;
   if (millis() - timer >= interval)
   {
      timer = millis();
      int distance = sonar.ping_cm();
      Serial.print("Ping: ");
      Serial.print(distance); // Send ping, get distance in cm and print result (0 = outside set distance range)
      Serial.println("cm");
      if (distance > 15)
      {
         stepper.rotate(1);
      }
      else
      {
         stepper.rotate(0);
      }
   }
}

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