user10741: I need your suggestions and help with my Ping Sensor.
I have an Arduino Uno with a Parallax Ping sensor and a motor.
My ping sensor is set to to start the motor when the nearest object is <= 10cm. That works fine.
Now, I'd like the motor to start only if the values of the sensor are progressing in a decreasing manner i.e 15, 14, 13, 12, 11, 10 but do nothing if the values are progressing in a increasing manner.
I was thinking if I stored the last 3 pings as variables and using an if/else statement but I don't know how to implement it.
How can I implement this, or if you have any other suggestion, please help me.
Thank you
Here is what i have so far:
/* Ping))) Sensor

   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.

   The circuit:
    * +V connection of the PING))) attached to +5V
    * GND connection of the PING))) attached to ground
    * SIG connection of the PING))) attached to digital pin 7

   http://www.arduino.cc/en/Tutorial/Ping

   created 3 Nov 2008
   by David A. Mellis
   modified 30 Aug 2011
   by Tom Igoe

   This example code is in the public domain.

 */

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;
const int motorPin = 9;
//Yomis code
//int pings[3] = {-1,-1,-1};
//int i;
//bool Increasing(int p1, int p2, int p3)
//{
//  if(p1 < p2 && p2 < p3){
//    return true;
//  }
//}
//
//bool Decreasing(int p1, int p2, int p3)
//{
//  if(p1 > p2 && p2 > p3){
//    return true;
//  }
//}
//
//end of yomis code
void setup() {
  // Set up the motor pin to be an output:

  pinMode(motorPin, OUTPUT);

  // initialize serial communication:
  Serial.begin(9600);
}


void loop()
{

  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  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(1000);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(2000);
  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);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(1000);

//        //Yomis code       
//        if (cm <= 10)
//        {
//          Serial.println();
//          Serial.println(pings[0]);
//              Serial.println(pings[1]);
//              Serial.println(pings[2]);
//          Serial.println();
//          if(Increasing(pings[0],pings[1],pings[2]))
//          {
//            int Speed1 = 100;  // between 0 (stopped) and 255 (full speed)
//            int Time1 = 3000;  // milliseconds for speed 1    
//            analogWrite(motorPin, Speed1);  // turns the motor On
//          }    
//        }
//        else
//        {
//          analogWrite(motorPin, LOW);
//        }
//      
//        //End of yomis code

  if (cm <= 10) {

    int Speed1 = 150;  // between 0 (stopped) and 255 (full speed)
    int Time1 = 2000;  // milliseconds for speed 1

    int Speed2 = 250;   // between 0 (stopped) and 255 (full speed)
    int Time2 = 3000;  // milliseconds to turn the motor off

    analogWrite(motorPin, Speed1);  // turns the motor On
    delay(Time1);                   // delay for onTime milliseconds
//    analogWrite(motorPin, Speed2);  // turns the motor Off
//    delay(Time2);                   // delay for offTime milliseconds
  }
  else if (cm > 10) {
    analogWrite(motorPin, LOW);  // turn the motor off
  }
  else {
    Serial.print(inches);
    Serial.print("in, ");
    Serial.print(cm);
    Serial.print("cm");
    Serial.println();

    delay(100);
  }

}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.m
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}


user10798: Okay but why is everyone arguing about const and #define. That's clearly not the real problem that the person who posted the question is asking, so i'm kind of lost. @chaaarlie2. What exactly does averaging the averages do, since the distances have already been averaged to find the correct distance.

10741: <p>I need your suggestions and help with my Ping Sensor.
I have an Arduino Uno with a Parallax Ping sensor and a motor.</p>

<p>My ping sensor is set to to start the motor when the nearest object is &lt;= 10cm. That works fine.
Now, I'd like the motor to start only if the values of the sensor are progressing in a decreasing manner i.e 15, 14, 13, 12, 11, 10 but do nothing if the values are progressing in a increasing manner.</p>

<p>I was thinking if I stored the last 3 pings as variables and using an if/else statement but I don't know how to implement it.</p>

<p>How can I implement this, or if you have any other suggestion, please help me.
Thank you</p>

<p>Here is what i have so far:</p>

<pre><code>/* Ping))) Sensor

   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.

   The circuit:
    * +V connection of the PING))) attached to +5V
    * GND connection of the PING))) attached to ground
    * SIG connection of the PING))) attached to digital pin 7

   http://www.arduino.cc/en/Tutorial/Ping

   created 3 Nov 2008
   by David A. Mellis
   modified 30 Aug 2011
   by Tom Igoe

   This example code is in the public domain.

 */

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;
const int motorPin = 9;
//Yomis code
//int pings[3] = {-1,-1,-1};
//int i;
//bool Increasing(int p1, int p2, int p3)
//{
//  if(p1 &lt; p2 &amp;&amp; p2 &lt; p3){
//    return true;
//  }
//}
//
//bool Decreasing(int p1, int p2, int p3)
//{
//  if(p1 &gt; p2 &amp;&amp; p2 &gt; p3){
//    return true;
//  }
//}
//
//end of yomis code
void setup() {
  // Set up the motor pin to be an output:

  pinMode(motorPin, OUTPUT);

  // initialize serial communication:
  Serial.begin(9600);
}


void loop()
{

  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  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(1000);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(2000);
  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);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(1000);

//        //Yomis code       
//        if (cm &lt;= 10)
//        {
//          Serial.println();
//          Serial.println(pings[0]);
//              Serial.println(pings[1]);
//              Serial.println(pings[2]);
//          Serial.println();
//          if(Increasing(pings[0],pings[1],pings[2]))
//          {
//            int Speed1 = 100;  // between 0 (stopped) and 255 (full speed)
//            int Time1 = 3000;  // milliseconds for speed 1    
//            analogWrite(motorPin, Speed1);  // turns the motor On
//          }    
//        }
//        else
//        {
//          analogWrite(motorPin, LOW);
//        }
//      
//        //End of yomis code

  if (cm &lt;= 10) {

    int Speed1 = 150;  // between 0 (stopped) and 255 (full speed)
    int Time1 = 2000;  // milliseconds for speed 1

    int Speed2 = 250;   // between 0 (stopped) and 255 (full speed)
    int Time2 = 3000;  // milliseconds to turn the motor off

    analogWrite(motorPin, Speed1);  // turns the motor On
    delay(Time1);                   // delay for onTime milliseconds
//    analogWrite(motorPin, Speed2);  // turns the motor Off
//    delay(Time2);                   // delay for offTime milliseconds
  }
  else if (cm &gt; 10) {
    analogWrite(motorPin, LOW);  // turn the motor off
  }
  else {
    Serial.print(inches);
    Serial.print("in, ");
    Serial.print(cm);
    Serial.print("cm");
    Serial.println();

    delay(100);
  }

}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.m
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
</code></pre>
 10798: <p>Okay but why is everyone arguing about const and #define. That's clearly not the real problem that the person who posted the question is asking, so i'm kind of lost. @chaaarlie2. What exactly does averaging the averages do, since the distances have already been averaged to find the correct distance.</p>
