I am looking to write a simple Arduino sketch- when a sharp IR GP2Y0A21Y detects an obstacle within 10 cm to 40 cm it turns on a relay switch for 20 seconds. Any help would be greatly appreciated, as I need this for a time-sensitive project. This is what my sketch looks like:
[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);
I changed the code slightly. What happens now is that the relay turns on immediately and does not turn off. It does not respond to the Sharp IR distance readings. Any suggestions?
Here is the 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 = 7;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode( relay, OUTPUT );
digitalWrite( relay, LOW );
}
void loop() {
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);
delay(4000);// leave on for 4 seconds
digitalWrite(relay, LOW); // Turn Relay OFF
}
}
Solved! Here is the code for anyone out there who needs to do something similar:
[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);
int relay = 7;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode( relay, OUTPUT );
digitalWrite( relay, HIGH );
}
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 ((dis > 10) && ( dis< 40)) {
digitalWrite(relay, LOW);
delay(15000);// leave on for 15 seconds
digitalWrite(relay, HIGH); // Turn Relay OFF