Advertisement
Guest User

Untitled

a guest
Jul 31st, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. /* Ping))) Sensor
  2.  
  3. This sketch reads a PING))) ultrasonic rangefinder and returns the
  4. distance to the closest object in range. To do this, it sends a pulse
  5. to the sensor to initiate a reading, then listens for a pulse
  6. to return. The length of the returning pulse is proportional to
  7. the distance of the object from the sensor.
  8. */
  9.  
  10. #include <Wire.h>
  11. #include "Adafruit_LEDBackpack.h"
  12. #include "Adafruit_GFX.h"
  13. Adafruit_7segment matrix = Adafruit_7segment();
  14. int triggerPin = 5;
  15. int echoPin = 6;
  16. long duration = 0;
  17. float cm = 0.0;
  18.  
  19.  
  20. void setup() {
  21. Serial.begin(9600);
  22. matrix.begin(0x70);
  23. pinMode(triggerPin, OUTPUT);
  24. pinMode(echoPin, INPUT);
  25.  
  26. }
  27.  
  28. void loop(){
  29. //Inputs:
  30. digitalWrite(triggerPin,LOW);
  31. delayMicroseconds(5);
  32. digitalWrite(triggerPin,HIGH);
  33. delayMicroseconds(5);
  34. digitalWrite(triggerPin,LOW);
  35. delayMicroseconds(5);
  36. duration=pulseIn(echoPin, HIGH);
  37. //if(cm >= 20){
  38. //Serial.println("youre a little too close");
  39. //}
  40. //else{
  41. //Serial.println("I like nice people and programs. Do you?");
  42. //}
  43.  
  44.  
  45. // Calculations: convert the time into a distance
  46. cm = duration /29.0 /2.0;
  47.  
  48.  
  49.  
  50. //Outputs
  51. Serial.print(cm);
  52. Serial.print("cm");
  53. Serial.println();
  54.  
  55. matrix.print(cm);
  56. matrix.writeDisplay();
  57. delay(400);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement