Sharp GP2Y0A02 - use it as a on off switch

Hello! Im pretty new to this, i have a Sharp GP2Y0A02 that can detect distances. I would like to use this script, but modify it to Serial.println(“Motion detected”); whenever it detects a reading within a certain range… in example: reading of above 300 writes Motion detected. Readings below 300 does nothing.

http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/

Any ideas right in the christmas time? ;D

Does the program print the distance now? If so, just change that line to print your message. If that works, then add code to see if "distance" is in your range and then print your message.

Paul

Instead of

Serial.println(distance);                       // print the distance

maybe try

if (distance >300) {
   Serial.println("Motion Detected");

} else {

   // do nothing here..  or maybe you will think of something later
};

Or for something more fancy

if (distance >300) {
   Serial.println("Far away");

} else if (distance > 50) {
   Serial.println("Getting closer");
} else if (distance >10) {
   Serial.println("Real close now");
} else {
    Serial.println("BRACE FOR COLLISION!!");

};