Hello,
I am very new to Arduino, but have done the projects in this package (Starter Kit)
and I also purchased this (Sharp IR sensor)
http://www.amazon.com/gp/product/B00IMOSEJA/ref=oh_details_o04_s00_i00?ie=UTF8&psc=1
I am trying to test the sensor I received and I am using the code below which I found here at the Arduino forums. When I have the sensor hooked up to AO and open the serial monitor, it returns the following values no matter where I place the sensor. Does anyone have any help they can offer? Thank you!
Results returned every time.
mean distance = 10 cm
time = 14 ms
Code
#include <SharpIR.h>
#define ir A0
#define model 1080
boolean done=false;
SharpIR sharp(0, 25, 93, 1080);
// 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);
}
void loop(){
delay(10000); // it gives you time to open the serial monitor after you upload the sketch
if (done==false){ // it only runs the loop once
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
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);
done=true;
}
}