Two IR Distance Sensors only being sensed together not apart.

Hi All,

I am using two Sharp 20-150 cm Infrared Distance sensors to detect if either of the sensors gets a distance is between 0-100 cm then keypress letter ‘d’.

Whilst they both work, and the serial monitor shows correct distance for them both, they do not appear to act upon the if and else statements. Instead if you use the code below both have to be within minimum and maximum range even though I used ‘||’ statements so it should only be if one of them.

I thought maybe because they were both within the same statement so tried splitting into two if statements e.g.

if (dis >= maximumRange || dis <= minimumRange) { 
 if (dis1 >= maximumRange || dis1 <= minimumRange )

but this then just acts upon dis1 (sensor 2).

Does anyone know why this is happening and/or how to get them so that is either is sensed it will do the keypress?

Thank-you, as always, for taking the time to read and help :slight_smile:

#include <SharpIR.h> // Sharp Libary
#include <Keyboard.h> // Keyboard


#define ir A0
#define model 20150
#define LEDPin 13 // Onboard LED
#define ir1 A1

unsigned long  minSample, maxSample;

int maximumRange = 100; // Maximum range needed in cm
int minimumRange = 0; // Minimum range needed in cm

boolean shouldPress;
boolean dogDetected;
boolean dognotinrange;

SharpIR sharp(ir, 25, 93, model);
SharpIR sharp1(ir1, 25, 93, model);

// ir: the pin where your sensor is attached
// 25: the number of readings the library will make before calculating a mean distance
// 93: the difference between two consecutive measurements to be taken as valid
// model: an int that determines your sensor:  1080 for GP2Y0A21Y
//                                            20150 for GP2Y0A02Y
//                                            (working distance range according to the datasheets)


void setup(){
  
  Serial.begin(9600);
  pinMode (ir, INPUT);
  pinMode (ir1, INPUT);
  pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}


void loop(){

  unsigned long pepe1=millis();  // takes the time before the loop on the library begins

  int dis=sharp.distance();  // this returns the distance to the object you're measuring
  
  int dis1=sharp1.distance(); // Returns the distance of the second sensor (pin A1) 

  Serial.print("Mean distance sensor 1: ");  // returns it to the serial monitor
  Serial.println(dis);

  Serial.print("Mean distance sensor 2: ");  // returns it to the serial monitor
  Serial.println(dis1);
  
  unsigned long pepe2=millis()-pepe1;  // the following gives you the time taken to get the measurement
  Serial.print("Time taken (ms): ");
  Serial.println(pepe2);  

if (dis >= maximumRange || dis <= minimumRange || dis1 >= maximumRange || dis1 <= minimumRange ){
 {
    Serial.println("out of range");
    digitalWrite(LEDPin, LOW); 
    Keyboard.releaseAll();
    shouldPress = true;
  }
  }
  // If in range
  else {
    dogDetected = true;
    // sets dog detected as true
    if(dogDetected = true){
    Serial.println("Dog detected in range");
      if(shouldPress == true){
        // checks if keypress or LED needs to happen
        digitalWrite(LEDPin, HIGH); 
        Keyboard.press('d');
        shouldPress = false;
        // sets to false so that it has happened
      }
    }

  }
  }
if (dogDetected = true)

Perhaps you meant:-if (dogDetected == true)

You don’t need that conditional anyway. You already know that the dog has been detected:-

else
    {
        dogDetected = true;
        // sets dog detected as true
        if (dogDetected = true)
        {
            Serial.println("Dog detected in range");
            if (shouldPress == true)
            {
                // checks if keypress or LED needs to happen
                digitalWrite(LEDPin, HIGH);
                Keyboard.press('d');
                shouldPress = false;
                // sets to false so that it has happened
            }
        }

    }

And you have an extra pair of braces here:-
(Won’t affect the program, but unnecessary.)

if (dis >= maximumRange || dis <= minimumRange || dis1 >= maximumRange || dis1 <= minimumRange )
    {
        {
            Serial.println("out of range");
            digitalWrite(LEDPin, LOW);
            Keyboard.releaseAll();
            shouldPress = true;
        }
    }
    if(dogDetected = true){

Oops.

With the unnecessary braces and “dog detected” bits removed and unused variables commented, along with decent formatting, your code looks like this:-

#include <SharpIR.h> // Sharp Libary
#include <Keyboard.h> // Keyboard

#define ir A0
#define model 20150
#define LEDPin 13 // Onboard LED
#define ir1 A1

//unsigned long  minSample, maxSample;

const int maximumRange = 100; // Maximum range needed in cm
const int minimumRange = 0;   // Minimum range needed in cm

boolean shouldPress;
//boolean dogDetected;
//boolean dognotinrange;

SharpIR sharp(ir, 25, 93, model);
SharpIR sharp1(ir1, 25, 93, model);

// ir: the pin where your sensor is attached
// 25: the number of readings the library will make before calculating a mean distance
// 93: the difference between two consecutive measurements to be taken as valid
// model: an int that determines your sensor:  1080 for GP2Y0A21Y
//                                            20150 for GP2Y0A02Y
//                                            (working distance range according to the datasheets)

void setup()
{
    Serial.begin(9600);
    pinMode (ir, INPUT);
    pinMode (ir1, INPUT);
    pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop()
{
    unsigned long pepe1 = millis(); // takes the time before the loop on the library begins

    int dis = sharp.distance(); // this returns the distance to the object you're measuring
    int dis1 = sharp1.distance(); // Returns the distance of the second sensor (pin A1)

    Serial.print("Mean distance sensor 1: ");  // returns it to the serial monitor
    Serial.println(dis);

    Serial.print("Mean distance sensor 2: ");  // returns it to the serial monitor
    Serial.println(dis1);

    unsigned long pepe2 = millis() - pepe1; // the following gives you the time taken to get the measurement
    Serial.print("Time taken (ms): ");
    Serial.println(pepe2);

    if(dis >= maximumRange || dis <= minimumRange || dis1 >= maximumRange || dis1 <= minimumRange)
    {
        Serial.println("out of range");
        digitalWrite(LEDPin, LOW);
        Keyboard.releaseAll();
        shouldPress = true;
    }
    else    // If in range
    {
        Serial.println("Dog detected in range");
        if(shouldPress == true)     // checks if keypress or LED needs to happen
        {
            digitalWrite(LEDPin, HIGH);
            Keyboard.press('d');
            shouldPress = false;    // sets to false so that it has happened
        }
    }
}

Edit: I also made ‘maximumRange’ and ‘minimumRange’ constants. :wink:

This will actually give you the time taken for the measurements AND the serial prints. You need to move it up a few lines to immediately after the actual measuring:-unsigned long pepe2 = millis() - pepe1; // the following gives you the time taken to get the measurement

crazydoglady:
Hi All,

I am using two Sharp 20-150 cm Infrared Distance sensors to detect if either of the sensors gets a distance is between 0-100cm then keypress letter ‘d’.

Whilst they both work, and the serial monitor shows correct distance for them both, they do not appear to act upon the if and else statements. Instead if you use the code below both have to be within minimum and maximum range even though I used ‘||’ statements so it should only be if one of them.

Actually your if code check if there is at least one value out of range (too close or too distant) and if there is, out of range is printed.

What you want might be to check of either of the sensors has something in range.

if ((dis <= maximumRange && dis >= minimumRange) || (dis1 <= maximumRange && dis1 >= minimumRange )){

Gabriel_swe: What you want might be to check of either of the sensors has something in range.

Well spotted. I missed that. I was side-tracked by the other problems.

Thank-you very much for your helpful advice, OldSteve, Gabriel_swe and AWOL.

I took your code OldSteve and added Gabriels’ arrow switch into it - and joy it worked! I do not know why it wouldn’t work the other way but for now I am just happy it is working.

I put in the dog detected bit as I was stuck and trying to figure things out - maybe lesson learned not to add bits in hence the brackets etc. >.<

The measurement is much faster with the moving of that line - to thank-you.

Notes to anyone else doing this: 23 is inaccurate range reading I have reduced this to 5 as it stops random blimps (with 23 was getting like 12 per min).

I understand my code had a lot of errors in, so I just wanted a final thank-you for helping me fix this. :slight_smile: