Will PAYPAL for answer!! Ultrasonic Sensor & IR REMOTE

Hi I have ran a code that activates the Ultrasonic Sensor when the (ok) button is pressed. The problem is the code for the Ultrasonic sensor only runs when i press the ok button rather than looping. So i figured it would be beneficial to put the IR Input in the setup and have the ultrasonic sensor in the loop alone. How would i accomplish this?

I will happily pay someone via Paypal for an answer that works! Have been stuck on this for hours! Thankyou!

Here is the code below:

#include <IRremote.h>

const int receiver = 11;
const int led = 13;
const int echo = 10;
const int trig = 9;

long duration;
int distance;

IRrecv ir_receiver(receiver);
decode_results results;

void setup()
{
Serial.begin(9600);
ir_receiver.enableIRIn();
pinMode(led, OUTPUT);
pinMode(echo, INPUT);
pinMode(trig, OUTPUT);
}

void loop()
{
if (ir_receiver.decode(&results))

{
Serial.println(results.value, HEX);
translateIR();
ir_receiver.resume();
delay(200);
}

}

void translateIR()

{
int sensorValue=0;
sensorValue = digitalRead(trig);

switch(results.value){

case 0xB520CEC:
if (sensorValue==0){

Serial.println(" ON ");
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);

duration = pulseIn (echo, HIGH);
distance= duration*0.034/2;
if (distance <= 30)
{
Serial.println ("INTRUDER");
Serial.print ("Distance= ");
Serial.println (distance);
digitalWrite (led, HIGH);
delay(10);
}
else {
Serial.println ("Clear");
Serial.print ("Distance= ");
Serial.println (distance);
digitalWrite (led, LOW);
delay (10);
}
ir_receiver.resume();

}
}
}

@jakiim has also posted about this in the "Sensors" board:
https://forum.arduino.cc/index.php?topic=676351

What is the purpose of the IR control ?
Is it to activate the alarm, putting it in a "ready" state or suppress it (switch off the led) when triggered or what ?

6v6gt:
What is the purpose of the IR control ?
Is it to activate the alarm, putting it in a "ready" state or suppress it (switch off the led) when triggered or what ?

The purpose is to activate the Sensor putting it in a ready state. Then when the sensor is triggered by someone breaching the 30cm distance the LED should turn on.

Why don't you just leave the ultrasonic sensor running (that is, pinging away happily) all the time?

Then if there are times when you don't care if anyone is close or not, just don't use the reading? Just use the remote to cause it to check the distance and do the "intruder" thing.)

(I prefer to use the new ping library, much "tidier" code, and use blink-without-delay-thinking to time the pulses every 50ms or so.)

I've just restructured your code but not tested it. Your 'OK' IR button will activated it. Any other button will deactivate it.
I'm assuming the ultrasonic part worked before. Don't forget that the ultrasonic senso may need a stabilisation time.

#include <IRremote.h>


const int receiver = 11;
const int led = 13;
const int echo = 10;
const int trig = 9;

long duration;
int distance;

bool irActive = false ;

IRrecv ir_receiver(receiver);
decode_results results;

void setup()
{
  Serial.begin(9600);
  ir_receiver.enableIRIn();
  pinMode(led, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(trig, OUTPUT);
}


void loop()
{
  if (ir_receiver.decode(&results))
  {
    Serial.println(results.value, HEX);
    delay(200);
    if ( results.value ==  0xB520CEC ) {
      irActive = true ;
    }
    else {
      irActive = false ;
      digitalWrite (led, LOW); 
    }
    ir_receiver.resume();
  }

  if ( irActive == true ) translateIR();

}

void translateIR()

{
  int sensorValue = 0;
  sensorValue = digitalRead(trig);

  if (sensorValue == 0) {

    Serial.println(" ON ");
    digitalWrite(trig, HIGH);
    delayMicroseconds(10);
    digitalWrite(trig, LOW);

    duration = pulseIn (echo, HIGH);
    distance = duration * 0.034 / 2;
    if (distance <= 30)
    {
      Serial.println ("INTRUDER");
      Serial.print ("Distance= ");
      Serial.println (distance);
      digitalWrite (led, HIGH);
      delay(10);
    }
    else {
      Serial.println ("Clear");
      Serial.print ("Distance= ");
      Serial.println (distance);
      digitalWrite (led, LOW);
      delay (10);
    }
  }
}

6v6gt You are amazing! Please send me your paypal details or feel free to comment them back and i will happily transfer you $30 :slight_smile: if thats enough! Look forward to your assistance on future projects too! Thankyou!

I'm pleased that worked out too. Check your personal messages for the details.