/*SHARP GP2Y0A21YK0F IR sensor with Arduino and SharpIR library example code. More info: https://www.makerguides.com */ //Include the library #include #include SoftwareSerial Serial_IR(0, 1); // rx, tx //Define model and input pin #define IRPin A2 #define model 20150 /*Model : GP2Y0A02YK0F --> 20150 20-150 --- GP2Y0A21YK0F --> 1080 10-80 GP2Y0A710K0F --> 100500 GP2YA41SK0F --> 430 */ //Setup the sensor object SharpIR IR_Sensor_1(IRPin, model); void setup() { //Begin serial communication at a baud rate of 9600 Serial_IR.begin(9600); } void loop() { delay(1000); int distance_cm = IR_Sensor_1.distance(); // this returns the distance to the object you're measuring Serial_IR.print("Mean distance: "); // returns it to the serial monitor Serial_IR.print(distance_cm); Serial_IR.println(" cm"); }