issue with stepper motor and ultrasonic sensor

Hi everybody-
I found this code on this forum for activating a stepper motor with an ultrasonic sensor.
{Here}

I was super happy to find that it worked- when I wave my hand or walk in front of the sensor the stepper motor is activated. It was easy to figure out how to change the distance the sensor measured, the delays, etc.

But after leaving the program running for 30 minutes or so without activating the sensor, the sensor has effectively turned off. The serial monitor stops showing pings, the lights on the Arduino that were blinking to indicate the sensor stopped blinking.

I’m sure it’s a code issue, but I can’t figure out what to alter to fix the problem. Here’s the code:

#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11
#define STEPS_PER_ROTATION 4096

const int pingPin = 12;
const int echoPin = 13;
unsigned int duration, inches;


void setup()
{

  pinMode(12,OUTPUT);
  pinMode(13,INPUT);
  Serial.begin(9600);
  Serial.println("File From Distance Test");

}

void ping()  {
  digitalWrite(pingPin, LOW);        // Ensure pin is low
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       // Start ranging
  delayMicroseconds(5);              //   with 5 microsecond burst
  digitalWrite(pingPin, LOW);        // End ranging

  duration = pulseIn(echoPin, HIGH); // Read echo pulse
  inches = duration / 74 / 2;        // Convert to inches
  Serial.println(inches);            // Display result
  if ((inches>2) && (inches<36)) 
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  delay(400);

}

void loop() {
  delay(50);
  ping();
  if ((inches>2) && (inches<36)) {
    rotate(1.5);
    delay(400);
    rotate(-2);
    delay(400);
  }
}
  
  void rotate(float rotations) {
    rotate_steps(rotations * STEPS_PER_ROTATION);
  }

  int phase = 0;
  byte phases[] = { 
    1, 3, 2, 6, 4, 12, 8, 9   };

  void rotate_steps(int steps)
  {
    int dir = (steps > 0) - (steps < 0);
    steps *= dir;
    long laststep = 1000;
    for (int i = 0; i < steps;) {
      long now = micros();
      if (now - laststep < 1000) continue;
      laststep = now;
      stepper_writepins(phases[phase]);
      phase = (8 + phase + dir) % 8;
      i++;
    }
    stepper_writepins(0);
  }

  void stepper_writepins(int bitmap) {
    digitalWrite(IN1, bitmap & 8 ? HIGH : LOW);
    digitalWrite(IN2, bitmap & 4 ? HIGH : LOW);
    digitalWrite(IN3, bitmap & 2 ? HIGH : LOW);
    digitalWrite(IN4, bitmap & 1 ? HIGH : LOW);
  }

I think the problem is in the lines:

void rotate(float rotations) {
    rotate_steps(rotations * STEPS_PER_ROTATION);
  }

  int phase = 0;
  byte phases[] = { 
    1, 3, 2, 6, 4, 12, 8, 9   };

  void rotate_steps(int steps)
  {
    int dir = (steps > 0) - (steps < 0);
    steps *= dir;
    long laststep = 1000;
    for (int i = 0; i < steps;) {
      long now = micros();
      if (now - laststep < 1000) continue;
      laststep = now;
      stepper_writepins(phases[phase]);
      phase = (8 + phase + dir) % 8;
      i++;
    }
    stepper_writepins(0);

I’ve tried to change long laststep to a different value, I’ve tried to change now-laststep<1000 to a larger number… I’ve tried commenting out different lines.

If anything I would appreciate some clarification on what that section of the code is exactly saying.

Thanks for the help,
Caroline

after leaving the program running for 30 minutes or so without activating the sensor

long now = micros();Don’t use the long type to store the value of micros(). Use unsigned long

I did not read in details the rest but that’s not good, a long can store only up to 2,147,483,647 and micros will go up to 4,294,967,295. So after 2,147,483,647 microseconds (interestingly enough this is roughly 35 minutes) you’ll go in negative territory for your variable... not sure this is expected

(Same goes for laststep)

Thank you, very helpful! I am no longer at my desk with the arduino set up but will try that first thing tomorrow.