need help with sonar code

hello, i am stuck with the code of my project. i am trying to use sonar to measure distance and turn on led. but the led will only turn on when the distance is descending. can any1 help me with the code??

What code? You didn't post any

AWOL: What code? You didn't post any

i am asking what would b the code to turn on led when the distance is descending using a sonar

That would be your code, which again, you didn't post.

AWOL: That would be your code, which again, you didn't post.

if i had known the code i wouldnt be posting here for help -_-

#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 4) {  // This is where the LED On/Off happens
    digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
  digitalWrite(led2,LOW);
}
  else {
    digitalWrite(led,LOW);
    digitalWrite(led2,HIGH);
  }
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

Moderator edit: CODE TAGS. Why is it so hard to understand?

OK, that code just performs a simple threshold. To work out if an object is getting closer/further away, you need to keep a record of at least one other reading, and test to see if the difference between the current and previous reading(s) is getting larger or smaller.

ibriju:
hello, i am stuck with the code of my project. i am trying to use sonar to measure distance and turn on led. but the led will only turn on when the distance is descending. can any1 help me with the code??

void loop() {
    static int oldDistance = 0;
    int distance = getDistance();
    if (distance < oldDistance) {
        // Object is getting closer!
        digitalWrite(LED, HIGH);
    } else {
        // Object is getting further (or the same)
        digitalWrite(LED, LOW);
    }
    oldDistance = distance;
}
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10

int buffer;


void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  buffer = distance;  //here i buffer
 //then whe read again and compare with buffer saved 10 microsecs before ! 
  delayMicroseconds(50); 
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
 
  if (distance > buffer) {               // This wil onley be done if distance is below 4 ! they mentiod    
    digitalWrite(led,HIGH);        // so you need the value comming in ,buffer it check it is less then before 
  digitalWrite(led2,LOW);        // ore more then the buffer ,, so yes then do this or that ,
}                                       //Johnwasser did this in his way ,, i wil do it in your style ,,
  else if (distance < buffer) {         // but now whe have a problem you wil see what i mean ,, now it reacts on movement 
    digitalWrite(led,LOW);              // so you need a point where you can say this is start point and if i am bellow or above react ,,
    digitalWrite(led2,HIGH);
  }
  if (distance >= 200 || distance <= 0){  // why 200 and 0 , max and min for your system ?
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

but you want to measure from a starting point ?