advise on using a sharp IR Sensor (GP2Y0A41SK0F) to trigger a 5vdc relay

Hi Folks,

I’m looking for some advise on coding and on setting up a circuit using a sharp IR sensor to trigger a 5vdc realy, which in turn will turn on a bulb. Any help would be greatly appreciated.

Thanks

Any help would be greatly appreciated.

What do you need help with? Googling "sharp IR sensors library for Arduino? Writing to a digital pin?

Essentially the code is what I’m looking for. I’ve done a little research and I came across this code.

#include <SharpIR.h>

#define ir A0
#define model 1080
// ir: the pin where your sensor is attached
// model: an int that determines your sensor: 1080 for GP2Y0A21Y
// 20150 for GP2Y0A02Y
// (working distance range according to the datasheets)

SharpIR SharpIR(ir, model);
unsigned int distance, cm;
int relay = A1;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
delay(200);

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

int dis=SharpIR.distance(); // this returns the distance to the object you’re measuring

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

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 ((cm>10) && (cm<40)) {digitalWrite(relay, HIGH);
digitalWrite(relay, LOW);

delay(50);

digitalWrite(relay, LOW); // Turn Relay OFF
delay(20000);// leave on for 20 seconds

delay(50);
}
}