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);
}
}
}
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();
}
}
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();
}
}
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.
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