# Compare varibles from a distance sensor over a set time period

I have a robotic boat moving about my pool. It uses three HC-SRO4 distance sensors. If the boat stops moving over a period of time I want it to reverse the motors using a function I set up for that called Reverse() and back away from whatever is blocking it. To do this I thought I would assign a variable to hold a measurement from the center distance sensor. The function that gets the center HC-SO4 distance readings is called CenterDistance(). I would then wait a period of time (say 10 seconds) then take another measurement from CenterDistance(). If the boat was stuck the first measurement from the distance sensor would be close (but not exactly the same) as the second sensor reading 10 seconds later indicating the boat had barely moved in 10 seconds and needs run a reverse to get out of that jam.

I tried to grab a distance sensor value and place it in a variable, grab another distance sensor value 10 seconds later and place it in a second variable then compare the two. I would then set up an if statement to put the boat in reverse if the two values indicated little boat movement in 10 seconds. I was unable to find a good example to follow on comparing a variables value over a set time period. Here is what I tried and failed with.

The way I declared the variables:

int leftDistance;
int rightDistance;
int centerDistance;

int startingCenterDistance;
int centerDistanceNow;

unsigned long interval = 10000; // the time we need to wait
unsigned long previousMillis = 0; // millis() returns an unsigned long.

The bit of code I attempted to use to compare the values from the function CenterDistance() 10 seconds apart and do something:

startingCenterDistance = centerDistance;//Get an initial value from the center distance sensor

unsigned long currentMillis = millis(); // grab current time;

// check if “interval” time has passed (10000 milliseconds, 10 seconds)
if ((unsigned long)(currentMillis - previousMillis) >= interval) {

centerDistanceNow = centerDistance;//Get a value from the center distance sensor 10 second later

previousMillis = millis();//reset counter
}
if (centerDistanceNow - startingCenterDistance < 20) {//Compare the two sensor readings 10 seconds apart if the boat has not moved at least 20 cm in 10 seconds, reverse
Reverse();
}
Serial.print("Starting Center Distance: ");
Serial.println(startingCenterDistance);
delay(1000);
Serial.print("Center Distance Now: ");
Serial.println(centerDistanceNow);
delay(1000);

I am not getting an initial distance assigned to one variable and a different distance reading into the second variable 10 seconds later they stay the same value over time with the boat moving. I tried the Millis() function and tried delay(10000) with the same result. I am sure how I time 10 seconds is not my error. Thanks in advance for any advice.

From your description and your bit of code, I can guess that you are just guessing at the problem. Please use Serial.print() to show the values you are receiving from the sensor and the values you are comparing. Then you will have data, not assumptions.

Paul

Don't post snippets (Snippets R Us!)

is it safe to assume you read the 3 sensors in the loop continuously?