Code not running all the way through if function is started after certain time

I am creating a program for a rc car to drive it self through an obstacle course. Currently all it is suppose to do is scan the environment with ultrasonic sensors, then run a function to calculate how much to turn and how far to go. The problem is when I have my Drive() function run after 100 times through the for loop, when I then call Drive() and then finddropoff() I have a for loop that again runs and will run until the last iteration before the program just stops. The weird thing is that the for loop will be completed but not move past it but if I only got through the first for loop 10 times before I call drive, it will work. I can get the code to print k=199 but it then won’t print “loop finishes.” Does anyone have any ideas on this?

My main code:
void loop() {
// int i=counter%400;
// step 2 revolution in one direction:
for(int i=1;i<400;i++){
radar.step(1);

//every quarter turn, calculate
if (i % 100 == 0) {
Drive();
}
ultrasonic(i);

counter=counter+1;
Serial.println(i);
}
}

The Drive method:
void Drive(){
Serial.println(“Does Drive”);
findDropOff();
long startDistance=Distance();
speedcontrol.write(110);
Turn(dropOff[0]);
double dis=0;
while(dis<dropOff[1]){
delay(5);
dis=Distance()-startDistance;
Serial.println(Distance());
}

speedcontrol.write(100);
Brake();
}

and my finddropoff method:
void findDropOff() {
Serial.println(“Does Drop off”);
int maxdistance = 280; //cm
int gap[2]; //0 is angle 1 is which side
gap[0] = 0;
gap[1] = 0; // 0 is go left 1 is go right

for (int k = 1; k < 200; k++) {
Serial.println(“k=”);
Serial.println(k);

if (abs(ultradata[k - 1] - ultradata[k]) > gap[1]) {
if (ultradata[k - 1] - ultradata[k] < 0) {
gap[1] = 1;
dropOff[1] = ultradata[k - 1] * 10;

}
else {
gap[1] = 0;
dropOff[1] = ultradata[k] * 10;

}
gap[0]=k;

}
Serial.println(“k=”);
Serial.println(k);
}
Serial.println(“Loop finishes”);
if (gap[1] = 0) {
dropOff[0] = gap[0] + 2;
}
if (gap[1] = 1) {
dropOff[0] = gap[0] - 2;
}
Serial.println(“Does all dropofff”);
}

Try running the IDE's auto-format tool over your code, and then post all of it, but using code tags instead of colour tags.

  if (gap[1] = 0) {

Oops.

Here is all of it.

//Libraries
#include <Encoder.h>
#include <Servo.h>
#include <Stepper.h>

//Stepper radar
const int stepsPerRevolution = 400;
Stepper radar(stepsPerRevolution, 2, 3, 4, 5);

//Ultrasonic Sensors and variables
int delaytime = 40;
const int pingPin = 10;
const int pingPin2 = 11;
const int pingPin3 = 12;
const int pingPin4 = 13;
long ultradata[200];
double dropOff[2]; //first is angle, second is distance
//Declaring Pins for encoder, speed controller, and servo
Encoder encode(26, 24);
Servo speedcontrol;
Servo steer;


//variable for calculating path
int far[2];
int near[2];


int counter = 1;

void setup() {
  // initialize the serial port:
  Serial.begin(9600);


  speedcontrol.attach(9);

  steer.attach(8);
  // set the speed of radar at 13 rpm:
  radar.setSpeed(15);

  //Initialize steering to straight which is 103
  steer.write(103);

  //Initialize zero on speed controller as 100 and wait for 3 seconds
  speedcontrol.write(100);
  delay(3000);


}




void loop() {
  // int i=counter%400;
  // step 2 revolution  in one direction:
  for (int i = 1; i < 400; i++) {
    radar.step(1);

    //every quarter turn, calculate
    if (i % 100 == 0) {
      Drive();
    }
    ultrasonic(i);

    counter = counter + 1;
    Serial.println(i);
  }
}
void Turn(int carangle) {
  Serial.println("Does Turn");
  int turnDistance = 200;
  int turnFactor = (((100 - carangle) * .9 / turnDistance) + .386) * 1000 / 3.727;
  long start = Distance();

  Serial.println(turnFactor);
  steer.write(turnFactor);
  while (Distance() - start < turnDistance) {
  }
  steer.write(103);
  Serial.println("Does Turn");
}

void Drive() {
  Serial.println("Does Drive");
  findDropOff();
  long startDistance = Distance();
  speedcontrol.write(110);
  Turn(dropOff[0]);
  double dis = 0;
  while (dis < dropOff[1]) {
    delay(5);
    dis = Distance() - startDistance;
    Serial.println(Distance());
  }

  speedcontrol.write(100);
  Brake();
}


void Brake() {
  speedcontrol.write(70);
  delay(1000);
  speedcontrol.write(100);
}
void CalculateNearFar() {

  far[0] = 0;
  far[1] = 0;
  near[0] = 300;
  near[1] = 300;
  for (int j = 0; j < 200; j++) {
    if (ultradata[j] > far[1]) {
      far[0] = j;
      far[1] = ultradata[j];
    }
    if (ultradata[j] < near[1]) {
      near[0] = j;
      near[1] = ultradata[j];
    }
  }

  Serial.println("Far:");
  Serial.println(far[0]);
  Serial.println(far[1]);

  Serial.println("Near:");
  Serial.println(near[0]);
  Serial.println(near[1]);
}

void findDropOff() {
  Serial.println("Does Drop off");
  int maxdistance = 280; //cm
  int gap[2]; //0 is angle 1 is which side
  gap[0] = 0;
  gap[1] = 0; // 0 is go left 1 is go right

  for (int k = 1; k < 200; k++) {
    Serial.println("k=");
    Serial.println(k);

    if (abs(ultradata[k - 1] - ultradata[k]) > gap[1]) {
      if (ultradata[k - 1] - ultradata[k] < 0) {
        gap[1] = 1;
        dropOff[1] = ultradata[k - 1] * 10;

      }
      else {
        gap[1] = 0;
        dropOff[1] = ultradata[k] * 10;

      }
      gap[0] = k;


    }
    Serial.println("k=");
    Serial.println(k);
  }
  Serial.println("Loop finishes");
  if (gap[1] == 0) {
    dropOff[0] = gap[0] + 2;
  }
  if (gap[1] == 1) {
    dropOff[0] = gap[0] - 2;
  }
  Serial.println("Does all dropofff");
}


//void TakeData(){
//   //set distance and time array
//   counter++;
//  distime[0][counter%100]=Distance();
//  distime[1][counter%100]=1000*millis();
//
//// acceleration data
//  spetime[0][counter%100]=Speed();
//  spetime[1][counter%100]=1000*millis();
//
////gyroscope data
//     gyro.read();
//    double temp;
//    temp=millis();
//    angle=angle+gyro.data.x*(time1-temp);
//    time1=temp;
//}

First_Program.ino (1.22 KB)

kartner: The problem is when I have my Drive() function run after 100 times through the for loop, when I then call Drive() and then finddropoff() I have a for loop that again runs and will run until the last iteration before the program just stops. The weird thing is that the for loop will be completed but not move past it but if I only got through the first for loop 10 times before I call drive, it will work.

Try re-writing that explanation more clearly.

As it stands I would have a better chance of understanding an auctioneer at a livestock sale.

...R

And why didn't you post the whole code? Just one block with all the code.

And why didn't you run the auto format tool? The code is still all over the place....

Sorry I have never posted before and have never encountered a problem like this while coding before.
There are two cases:
Case 1: In the main loop if my for loop only runs 10 times before calling Drive() the code will run all the way through.

if (i % 10 == 0) {
      Drive();
    }

Case 2: I set it to run 100 times before calling Drive()

if (i % 100 == 0) {
      Drive();
    }

For this case the function Drive() will be called. It then runs and calls findDropOff(). This is where the problem occurs. I have a for loop with 200 (decreasing this value doesn’t help) iterations that finds the largest drop off between two values. The loop will run to k=199 (sometimes it is 196-198) and then stop. It won’t print out the “Loop Finishes” which I put directly after the loop. I am not sure what is happening as I have never experienced this while coding in Java.
Thanks for helping out.

//Libraries
#include <Encoder.h>
#include <Servo.h>
#include <Stepper.h>

//Stepper radar
const int stepsPerRevolution = 400;
Stepper radar(stepsPerRevolution, 2, 3, 4, 5);

//Ultrasonic Sensors and variables
int delaytime = 40;
const int pingPin = 10;
const int pingPin2 = 11;
const int pingPin3 = 12;
const int pingPin4 = 13;
long ultradata[200];
double dropOff[2]; //first is angle, second is distance
//Declaring Pins for encoder, speed controller, and servo
Encoder encode(26, 24);
Servo speedcontrol;
Servo steer;

//variable for calculating path
int far[2];
int near[2];
int counter = 1;

void setup() {
  // initialize the serial port:
  Serial.begin(9600);
  speedcontrol.attach(9);
  steer.attach(8);
  // set the speed of radar at 13 rpm:
  radar.setSpeed(15);
  //Initialize steering to straight which is 103
  steer.write(103);
  //Initialize zero on speed controller as 100 and wait for 3 seconds
  speedcontrol.write(100);
  delay(3000);
}

void loop() {
  
  // step 1 revolution  in one direction:
  for (int i = 1; i < 400; i++) {
    radar.step(1);
    //every quarter turn, calculate
    if (i % 100 == 0) {
      Drive();
    }
    //run ultrasonic sensors
    ultrasonic(i);
    Serial.println(i);
  }
}
void Turn(int carangle) {
  Serial.println("Does Turn");
  int turnDistance = 200;
  int turnFactor = (((100 - carangle) * .9 / turnDistance) + .386) * 1000 / 3.727;
  long start = Distance();
  Serial.println(turnFactor);
  steer.write(turnFactor);
  while (Distance() - start < turnDistance) {
    delay(5);
  }
  steer.write(103);
  Serial.println("Does Turn");
}

void Drive() {
  Serial.println("Does Drive");
  findDropOff();
  long startDistance = Distance();
  speedcontrol.write(110);
  Turn(dropOff[0]);
  while (Distance() - startDistance < dropOff[1]) {
    delay(5);
    Serial.println(Distance());
  }
  speedcontrol.write(100);
  Brake();
}


void Brake() {
  speedcontrol.write(70);
  delay(1000);
  speedcontrol.write(100);
}
void findDropOff() {
  Serial.println("Does Drop off");
  int maxdistance = 280; //cm
  int gap[2]; //0 is angle 1 is which side. This stores where biggest dropoff is temporarily. 
  gap[0] = 0;
  gap[1] = 0; // 0 is go left 1 is go right

  for (int k = 1; k < 200; k++) {
    Serial.println("k=");
    Serial.println(k);
    if (abs(ultradata[k - 1] - ultradata[k]) > gap[1]) {
      if (ultradata[k - 1] - ultradata[k] < 0) {
        gap[1] = 1;
        dropOff[1] = ultradata[k - 1] * 10;
      }
      else {
        gap[1] = 0;
        dropOff[1] = ultradata[k] * 10;
      }
      gap[0] = k;
    }
    Serial.println("k=");
    Serial.println(k);
  }
  Serial.println("Loop finishes");
  if (gap[1] == 0) {
    dropOff[0] = gap[0] + 2;
  }
  if (gap[1] == 1) {
    dropOff[0] = gap[0] - 2;
  }
  Serial.println("Does all dropofff");
}
long Distance() {
  return encode.read() / 1.2979;
}
void ultrasonic(int angle) {
  if (angle < 200) {
    ultrasonic1(angle);
  }
  if (angle < 300 && angle > 99) {
    ultrasonic2((angle + 100) % 400);
  }
  if (angle > 199 && angle < 400) {
    ultrasonic3((angle + 200) % 400);
  }
  if (angle > 299 || angle < 100) {
    ultrasonic4((angle + 300) % 400);
  }
  Serial.println(angle);
}


void ultrasonic1 (int angle) {
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  ultradata[angle] = cm;
  if (angle > 50 && angle < 150 && cm < 10) {
    Brake();
    Serial.println("stopped");
    delay(1000000);
  }

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(delaytime);
}
void ultrasonic2 (int angle) {
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin2, OUTPUT);
  digitalWrite(pingPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin2, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin2, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin2, INPUT);
  duration = pulseIn(pingPin2, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  ultradata[angle] = cm;
  if (angle > 50 && angle < 150 && cm < 10) {
    Brake();
    Serial.println("stopped");
    delay(1000000);
  }

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(delaytime);
}
void ultrasonic3 (int angle) {
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin3, OUTPUT);
  digitalWrite(pingPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin3, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin3, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin3, INPUT);
  duration = pulseIn(pingPin3, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  ultradata[angle] = cm;
  if (angle > 50 && angle < 150 && cm < 10) {
    Brake();
    Serial.println("stopped");
    delay(1000000);
  }

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(delaytime);
}
void ultrasonic4 (int angle) {
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin4, OUTPUT);
  digitalWrite(pingPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin4, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin4, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin4, INPUT);
  duration = pulseIn(pingPin4, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  ultradata[angle] = cm;
  if (angle > 50 && angle < 150 && cm < 10) {
    Brake();
    Serial.println("stopped");
    delay(1000000);
  }

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(delaytime);
}
long microsecondsToInches(long microseconds) {

  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {

  return microseconds / 29 / 2;
}
const int pingPin = 10;
const int pingPin2 = 11;
const int pingPin3 = 12;
const int pingPin4 = 13;

If you can’t use an array (you could), the least you can do is number ALL the variables that seem to form a set.

You have functions that are doing too much. There is no reason for ultrasonic1() to be diddling with motors.

You do not need 4 functions that do the same thing, except on a different pin. You need one function that takes a pin number.

  if (angle < 200) {
    ultrasonic1(angle);
  }
  if (angle < 300 && angle > 99) {
    ultrasonic2((angle + 100) % 400);
  }

The selection of which function to call doesn’t make sense to me. If angle is 120, for instance, ultrasonic1() and ultrasonic2() will be called. Is this right?

I would have expected 0 to 99 to map to one function, 100 to 199 to map to another function, 200 to 299 to map to a third, and 300+ to map to the fourth function (or, to one function with 4 different pin numbers in the call).

Yeah I understand the code for the ultrasonic sensors is a little messy. I was planning on cleaning that up later but do you think it could be causing the problems now? So for my project two ultrasonic sensors will be going at the same time. The radar is spinning and I want the sensors to be on while facing forward( 0-180 degrees or 0-200 steps). I don’t think there are any problems in that unless there is some sort of memory leak going on that I don’t understand. I really don’t understand why the code would just stop running though.

Thanks for helping out.

The explanation in Reply #5 is better. But you still have not posted the entire program as a single block I'm not going to join things up and risk making a mistake in the process.

Help us to help you.

...R

I believe I put all of the code in my reply as a single block. It is a little difficult though because I have different tabs in my program. I will try attaching the program as a file.

First_Program.ino (1.2 KB)

Disregard the previous post, it only uploaded one tab.
The attached should be all the code in one program.

Program1.ino (7.19 KB)

Why not just post the code like below so we don’t have to download it and clutter up our hard disks?

//Libraries
#include <Encoder.h>
#include <Servo.h>
#include <Stepper.h>

//Stepper radar
const int stepsPerRevolution = 400;
Stepper radar(stepsPerRevolution, 2, 3, 4, 5);

//Ultrasonic Sensors and variables
int delaytime = 40;
const int pingPin = 10;
const int pingPin2 = 11;
const int pingPin3 = 12;
const int pingPin4 = 13;
long ultradata[200];
double dropOff[2]; //first is angle, second is distance
//Declaring Pins for encoder, speed controller, and servo
Encoder encode(26, 24);
Servo speedcontrol;
Servo steer;

//variable for calculating path
int far[2];
int near[2];
int counter = 1;

void setup() {
  // initialize the serial port:
  Serial.begin(9600);
  speedcontrol.attach(9);
  steer.attach(8);
  // set the speed of radar at 13 rpm:
  radar.setSpeed(15);
  //Initialize steering to straight which is 103
  steer.write(103);
  //Initialize zero on speed controller as 100 and wait for 3 seconds
  speedcontrol.write(100);
  delay(3000);
}

void loop() {
  
  // step 1 revolution  in one direction:
  for (int i = 1; i < 400; i++) {
    radar.step(1);
    //every quarter turn, calculate
    if (i % 10 == 0) {
      Drive();
    }
    //run ultrasonic sensors
    ultrasonic(i);
    Serial.println(i);
  }
}

void Turn(int carangle) {
  Serial.println("Does Turn");
  int turnDistance = 200;
  int turnFactor = (((100 - carangle) * .9 / turnDistance) + .386) * 1000 / 3.727;
  long start = Distance();
  Serial.println(turnFactor);
  steer.write(turnFactor);
  while (Distance() - start < turnDistance) {
    delay(5);
  }
  steer.write(103);
  Serial.println("Does Turn");
}

void Drive() {
  Serial.println("Does Drive");
  findDropOff();
  long startDistance = Distance();
  speedcontrol.write(110);
  Turn(dropOff[0]);
  while (Distance() - startDistance < dropOff[1]) {
    delay(5);
    Serial.println(Distance());
  }
  speedcontrol.write(100);
  Brake();
}


void Brake() {
  speedcontrol.write(70);
  delay(1000);
  speedcontrol.write(100);
}

void findDropOff() {
  Serial.println("Does Drop off");
  int maxdistance = 280; //cm
  int gap[2]; //0 is angle 1 is which side. This stores where biggest dropoff is temporarily. 
  gap[0] = 0;
  gap[1] = 0; // 0 is go left 1 is go right

  for (int k = 1; k < 200; k++) {
    Serial.println("k=");
    Serial.println(k);
    if (abs(ultradata[k - 1] - ultradata[k]) > gap[1]) {
      if (ultradata[k - 1] - ultradata[k] < 0) {
        gap[1] = 1;
        dropOff[1] = ultradata[k - 1] * 10;
      }
      else {
        gap[1] = 0;
        dropOff[1] = ultradata[k] * 10;
      }
      gap[0] = k;
    }
    Serial.println("k=");
    Serial.println(k);
  }
  Serial.println("Loop finishes");
  if (gap[1] == 0) {
    dropOff[0] = gap[0] + 2;
  }
  if (gap[1] == 1) {
    dropOff[0] = gap[0] - 2;
  }
  Serial.println("Does all dropofff");
}

long Distance() {
  return encode.read() / 1.2979;
}

void ultrasonic(int angle) {
  if (angle < 200) {
    ultrasonic1(angle);
  }
  if (angle < 300 && angle > 99) {
    ultrasonic2((angle + 100) % 400);
  }
  if (angle > 199 && angle < 400) {
    ultrasonic3((angle + 200) % 400);
  }
  if (angle > 299 || angle < 100) {
    ultrasonic4((angle + 300) % 400);
  }
  Serial.println(angle);
}


void ultrasonic1 (int angle) {
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  ultradata[angle] = cm;
  if (angle > 50 && angle < 150 && cm < 10) {
    Brake();
    Serial.println("stopped");
    delay(1000000);
  }

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(delaytime);
}
void ultrasonic2 (int angle) {
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin2, OUTPUT);
  digitalWrite(pingPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin2, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin2, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin2, INPUT);
  duration = pulseIn(pingPin2, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  ultradata[angle] = cm;
  if (angle > 50 && angle < 150 && cm < 10) {
    Brake();
    Serial.println("stopped");
    delay(1000000);
  }

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(delaytime);
}
void ultrasonic3 (int angle) {
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin3, OUTPUT);
  digitalWrite(pingPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin3, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin3, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin3, INPUT);
  duration = pulseIn(pingPin3, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  ultradata[angle] = cm;
  if (angle > 50 && angle < 150 && cm < 10) {
    Brake();
    Serial.println("stopped");
    delay(1000000);
  }

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(delaytime);
}
void ultrasonic4 (int angle) {
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin4, OUTPUT);
  digitalWrite(pingPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin4, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin4, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin4, INPUT);
  duration = pulseIn(pingPin4, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  ultradata[angle] = cm;
  if (angle > 50 && angle < 150 && cm < 10) {
    Brake();
    Serial.println("stopped");
    delay(1000000);
  }

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(delaytime);
}
long microsecondsToInches(long microseconds) {

  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {

  return microseconds / 29 / 2;
}

…R

From Reply #5

For this case the function Drive() will be called. It then runs and calls findDropOff(). This is where the problem occurs. I have a for loop with 200 (decreasing this value doesn't help) iterations that finds the largest drop off between two values.

As far as I can see in both your cases the function Drive() is called and in both cases so is findDropOff(). Is that correct ?

In the second case where Drive() is only called after 100 steps of the stepper do you know if the problem happens the first time Drive() is called or the second time. My guess is that it is not the first time and that the problem lies in the data that findDropOff() is working with. But it is only a guess.

Also, the sort of problem you describe suggests that there is data corruption such as writing beyond the bounds of an array.

...R

but do you think it could be causing the problems now?

Yes, I do.

Robin2: As it stands I would have a better chance of understanding an auctioneer at a livestock sale.

Have you been to many of those?

[quote author=Nick Gammon date=1457684826 link=msg=2657539] Have you been to many of those? [/quote] I have only seen them on TV, and never understood a word that was said :)

Are you suggesting that I would have understood them better in the flesh ?

...R

No, I just liked the analogy. :)

[quote author=Nick Gammon link=msg=2658517 date=1457730331] No, I just liked the analogy. :) [/quote] I thought maybe livestock auctions are more comprehensible when everyone is upside down :)

...R

Not to me. :)