Stepper Motor controlled by Ultrasonic sensor -- Help with code?

Hello,

I have an old Ultrasonic sensor and a 28BYJ - stepper motor that I wanted to be controlled by the sensor. For some reason the code I have currently does not work.

My goal is for the machine is for the sensor to sense something, then run the stepper motor for 5 full rotations. Afterwards, I want it to delay for 5 minutes, and then only be able to start when the sensor senses something again (close by).

*(currently it will start right up as in the stepper will run or it will hit the delay then start stepper...).

I feel like I am just missing one part? or a bunch, I am just unsure and I have been re-writing this code/messing with the thing for about 5 hours straight! Any help is appreciated!


//defines pins numbers

#include <Stepper.h>

const int stepsPerRevolution = 120; //RPM
int x; // int
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

const int trigPin = 1;
const int echoPin = 2; //Where I connected all the pins on the arduino
const int ledPin = 3;

// defines variables
long duration;
int distance;
int safetyDistance;
int previous = 0;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(ledPin, OUTPUT);
myStepper.setSpeed(120);
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

safetyDistance = distance;

if (safetyDistance <= 5 && x !=10)
{
digitalWrite(ledPin, HIGH);
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
x = x + 1;

}
else
{
digitalWrite(ledPin, LOW);
delay(2000);
x
=0;
}

Ultrasonic.ino (1.28 KB)

Hello,

28BYJ-48, unipolar, 5V power, one full turn 4096 steps

[ Ultrasonic-HC-SR04/Ultrasonic/examples/Serial/Serial.ino]
Ultrasonic-HC-SR04 library

Stepper One Revolution

See if these routines help with something:

int sensorLimit = 20; // centimeters

void setup() {
  Serial.begin(9600);
  Serial.println("Begin");

  motorInit();

  sensorInit();
}

void loop() {
  int distance = sensor(); // Ultrasonic sensor

  Serial.print(distance); // 

  if (distance < sensorLimit) {
    Serial.print("\r\nDistance: "); // print message
    Serial.print(distance);
    Serial.print(" < Sensor limit: ");
    Serial.println(sensorLimit);
    Serial.println("\r\nGo!");
    
    rotations(); // 5 full rotations
    myDelay(); // delay for 5 minutes
  } else {
    Serial.print(", ");
  }
}

void motorInit() {
  //
}

void sensorInit() {
  randomSeed(analogRead(0)); // ultrasonic sensor simulator
}

// Ultrasonic sensor
int sensor() {
  delay(100); // simulation of sensor response time
  delay(5000); // simulation of something approaching the sensor
  return random(300); // ultrasonic sensor simulator
}

// 5 full rotations
void rotations() {
  int i = 5; // rotations

  while (i--) {
    Serial.print("Rotation: ");
    Serial.println(i);
    delay(1500); // simulation of motor response time
  }
}

// delay for 5 minutes
void myDelay() {
  int i = 1; // minutes, 1 for test

  while (i--) {
    int j = 60; // seconds

    while (j--) {
      delay(1000);
      Serial.print("Wait: ");
      Serial.print(i);
      Serial.print("min ");
      Serial.print(j);
      Serial.println("sec");
    }
  }
}