problems with if codition and ir remote ,library

I want to make a device that would calculate and serial print distance on receiving a particular ir signal. Please refer to the code given below when i execute it, when it receives the signal for first time it goes well but after that it is going on to calculate the distance even when the ir signal is not received. Please help as soon as possible.

#include <IRremote.h>

const int trigPin = 4;
const int echoPin = 5;

// defines variables
long duration;
int distance;

const int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;


void setup() {
   
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication


irrecv.enableIRIn();

}

void loop() {
    if(irrecv.decode(&results)) //this checks to see if a code has been received
{
    if(results.value == 0x25D) //if the button press equals the hex value 0x25D
    {
      digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
Serial.println(results.value, HEX);

    }

}

 

}

Did you forget the irrecv.resume(); // Receive the next value line? See the IR receive demo example.

Karma for code tags on first post.
Use auto format (CTRL-T or Tools, Auto Format) to indent your code to make it easier to follow.

The sequence of setting trigpin LOW, trigpin HIGH and trigpin LOW can't be okey. That makes the next setting triggpin LOW useless.

i tried putting irrecv.resume(); it goes well for the first time but later on it stops working even if the signal is recieved. please refer to my new code below and the attached screenshot of serial monitor.

#include <IRremote.h>

const int trigPin = 4;
const int echoPin = 5;

// defines variables
long duration;
int distance;

const int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;


void setup() {
   
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication


irrecv.enableIRIn();

}

void loop() {
    if(irrecv.decode(&results)) //this checks to see if a code has been received
{
    if(results.value == 0x25D) //if the button press equals the hex value 0x25D
    {
      digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
Serial.println(results.value, HEX);

    }

irrecv.resume(); 

}

 

}

Capture.PNG

if(results.value == 0x25D)

0x25D seems to be a very short number for an IR code.

I wired up the circuit with an Uno, IR decoder and HC-SR04 and slightly modified your code as shown. Run it and copy the whole IR code received from the serial monitor to the if statement and recompile, upload (in serial monitor, highlight the code, press CTRL-C to copy). This works with my remote (up arrow) code. Each press of up arrow gets a new range.

#include <IRremote.h>

const int trigPin = 4;
const int echoPin = 5;

// defines variables
long duration;
int distance;

const int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;


void setup()
{

   pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
   pinMode(echoPin, INPUT); // Sets the echoPin as an Input
   Serial.begin(9600); // Starts the serial communication


   irrecv.enableIRIn();

}

void loop()
{
   if (irrecv.decode(&results)) //this checks to see if a code has been received
   {
      unsigned long irVal = results.value;
      Serial.println(irVal, HEX);  // copy the code from serial monitor
      if (irVal == 0x4EB322DD) //and paste in place of my 0x4EB322DD Don't forget the 0x for hex.
      {
         digitalWrite(trigPin, LOW);
         delayMicroseconds(2);

         // Sets the trigPin on HIGH state for 10 micro seconds
         digitalWrite(trigPin, HIGH);
         delayMicroseconds(10);
         digitalWrite(trigPin, LOW);

         // Reads the echoPin, returns the sound wave travel time in microseconds
         duration = pulseIn(echoPin, HIGH);

         // Calculating the distance
         distance = duration * 0.034 / 2;

         // Prints the distance on the Serial Monitor
         Serial.print("Distance: ");
         Serial.println(distance);
         Serial.println(results.value, HEX);
      }
      irrecv.resume();
   }
}

It worked .thank you very much for your help and quick reply.

now i tried to send the signal again if the distance is less than 20 but when the distance is less than 20 it sends the signal but the loop stops working. please help

#include <IRremote.h>

IRsend irsend;

const int trigPin = 4;
const int echoPin = 5;

// defines variables
long duration;
int distance;

const int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;


void setup()
{

   pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
   pinMode(echoPin, INPUT); // Sets the echoPin as an Input
   Serial.begin(9600); // Starts the serial communication
   
 

   irrecv.enableIRIn();

}

void loop()
{
   if (irrecv.decode(&results)) //this checks to see if a code has been received
   {
      unsigned long irVal = results.value;
      Serial.println(irVal, HEX);  // copy the code from serial monitor
      if (irVal == 0x25D) //and paste in place of my 0x4EB322DD Don't forget the 0x for hex.
      {
         digitalWrite(trigPin, LOW);
         delayMicroseconds(2);

         // Sets the trigPin on HIGH state for 10 micro seconds
         digitalWrite(trigPin, HIGH);
         delayMicroseconds(10);
         digitalWrite(trigPin, LOW);

         // Reads the echoPin, returns the sound wave travel time in microseconds
         duration = pulseIn(echoPin, HIGH);

         // Calculating the distance
         distance = duration * 0.034 / 2;

         // Prints the distance on the Serial Monitor
         Serial.print("Distance: ");
         Serial.println(distance);
         Serial.println(results.value, HEX);

         
      if (distance > 20 ) {
       
        delay(8000);
        Serial.print("greater distance");
        
      } else {
        irsend.sendSony(0xFFA25D, 12);
        Serial.print("smaller distance");
      }
      }
      irrecv.resume();
   }
}

It has something to do with the irsend(). It works with irsend() commented out. I have no experience with irsend so can’t be of any help with this. Sorry.

anyone please help

#include <IRremote.h>

IRsend irsend;

const byte trigPin = 4;
const byte echoPin = 5;
const byte RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
   pinMode(trigPin, OUTPUT); 
   digitalWrite (trigPin, LOW);
   pinMode(echoPin, INPUT);
   Serial.begin(9600);
   irrecv.enableIRIn();
}

void loop()
{
   if (irrecv.decode(&results))
   {
      unsigned long irVal = results.value;
      Serial.println(irVal, HEX);
      if (irVal == 0x25D) // THIS SEEMS TO BE A VERY UNLIKELY VALUE
      {
        digitalWrite(trigPin, HIGH);
        delayMicroseconds(10);
        digitalWrite(trigPin, LOW);
        unsigned long duration = pulseIn(echoPin, HIGH);
        unsigned long distance = duration * 0.034 / 2;

        Serial.print(F("Distance: "));
        Serial.println(distance);
        Serial.println(results.value, HEX);
        if (distance > 20 ) {
          delay(8000);
          Serial.print(F("greater distance"));
        } else {
          irsend.sendSony(0xFFA25D, 12);
          Serial.print(F("smaller distance"));
        }
      }
      irrecv.resume();
   }
}

(Uncompiled, untested)

its not working. the problem is still existing. Please help

its not working

What does that mean?

i tried to send the signal again if the distance is less than 20 but when the distance is less than 20 it sends the signal but the loop stops working. please help

} else {
          irsend.sendSony(0xFFA25D, 12);
          irrecv.enableIRIn();  //********************* inserted
          Serial.print(F("smaller distance"));
        }

Inserting that line makes it work on with my test setup. It seems that after send you need to re-enable the receiver.

I don't know if it is actually sending, though. Comment out the annoying delay when you test.

It worked. Thank you very much for your help.