75Views2Replies

Author Options:

Is this the right way to check for object detection with a IR sensor? Answered

Sensor: Link

Hi, i'm using the sensor to test for detection. (eg when my hand is on top of the sensor, it will send a signal to a buzzer). Not sure if this is the right way for detection. Any improvements/tips are welcome.

int sensorPin = 1;
int lastDist = 1000; //set to a high amount to prevent false detection when powering up
int currentDist = 0;
int thresh = 25;

void setup()
{
  // initialise serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop()
{
  currentDist = analogRead(sensorPin);
  Serial.println(currentDist);
  // compare currentDist with lastDist & thresh to determine if any objects is detected
  if (currentDist > lastDist + thresh)
  {
    Serial.println("detection alert!");
  } else {
    Serial.println("no detection");
  }
  lastDist = currentDist;
}

Comments

The forums are retiring in 2021 and are now closed for new topics and comments.
0
steveastrouk
steveastrouk

Best Answer 5 years ago

Looks feasible. Try it. It won't detect the same thing in the same place twice though.

0
iceng
iceng

Answer 5 years ago

+1