getDistance.ino 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * getDistance
  3. *
  4. * Example of using SharpIR library to calculate the distance beetween the sensor and an obstacle
  5. *
  6. * Created by Giuseppe Masino, 15 June 2016
  7. *
  8. * -----------------------------------------------------------------------------------
  9. *
  10. * Things that you need:
  11. * - Arduino
  12. * - A Sharp IR Sensor
  13. *
  14. *
  15. * The circuit:
  16. * - Arduino 5V -> Sensor's pin 1 (Vcc)
  17. * - Arduino GND -> Sensor's pin 2 (GND)
  18. * - Arduino pin A0 -> Sensor's pin 3 (Output)
  19. *
  20. *
  21. * See the Sharp sensor datasheet for the pin reference, the pin configuration is the same for all models.
  22. * There is the datasheet for the model GP2Y0A41SK0F:
  23. *
  24. * http://www.robotstore.it/open2b/var/product-files/78.pdf
  25. *
  26. */
  27. //import the library in the sketch
  28. #include <SharpIR.h>
  29. //Create a new instance of the library
  30. //Call the sensor "sensor"
  31. //The model of the sensor is "GP2YA41SK0F"
  32. //The sensor output pin is attached to the pin A0
  33. SharpIR sensor( SharpIR::GP2Y0A41SK0F, A0 );
  34. void setup()
  35. {
  36. Serial.begin( 9600 ); //Enable the serial comunication
  37. }
  38. void loop()
  39. {
  40. int distance = sensor.getDistance(); //Calculate the distance in centimeters and store the value in a variable
  41. Serial.println( distance ); //Print the value to the serial monitor
  42. }