Advertisement
Guest User

Untitled

a guest
Feb 11th, 2017
135
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <NewPing.h>
  2.  
  3. #define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor.
  4. #define ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor.
  5. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
  6. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Creating the NewPing Object.
  7.  
  8. void setup() {
  9. Serial.begin(115200); // Begin serial com. at 115200 baud rate.
  10. pinMode(13, OUTPUT);
  11. pinMode(12, OUTPUT);
  12. pinMode(11, OUTPUT);
  13. pinMode(10, OUTPUT);
  14. pinMode(9, OUTPUT);
  15. pinMode(8, OUTPUT);
  16. }
  17.  
  18. void loop() {
  19. delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  20. unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  21. int cm = uS / US_ROUNDTRIP_CM;
  22. Serial.print("Ping: ");
  23. Serial.print(cm); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  24. Serial.println("cm"); //If you would like ping in inches, remove "US_ROUNDTRIP_CM" and the backslash, don't forget to rename "cm" to "inches"
  25. int typ;
  26. typ = map(cm, 1, 70, 1, 7);
  27. switch(typ)
  28. {
  29. case 1:
  30. digitalWrite(13, HIGH);
  31. break;
  32.  
  33.  
  34.  
  35. case 2:
  36. digitalWrite(13, HIGH);
  37. digitalWrite(12, HIGH);
  38.  
  39. break;
  40. case 3:
  41. digitalWrite(13, HIGH);
  42. digitalWrite(12, HIGH);
  43. digitalWrite(11, HIGH);
  44.  
  45. break;
  46. case 4:
  47. digitalWrite(13, HIGH);
  48. digitalWrite(12, HIGH);
  49. digitalWrite(11, HIGH);
  50. digitalWrite(10, HIGH);
  51.  
  52. break;
  53. case 5:
  54. digitalWrite(13, HIGH);
  55. digitalWrite(12, HIGH);
  56. digitalWrite(11, HIGH);
  57. digitalWrite(10, HIGH);
  58. digitalWrite(9, HIGH);
  59.  
  60. break;
  61. case 6:
  62. digitalWrite(13, HIGH);
  63. digitalWrite(12, HIGH);
  64. digitalWrite(11, HIGH);
  65. digitalWrite(10, HIGH);
  66. digitalWrite(9, HIGH);
  67. digitalWrite(8, HIGH);
  68. break;
  69. case 7:
  70. digitalWrite(13, LOW);
  71. digitalWrite(12, LOW);
  72. digitalWrite(11, LOW);
  73. digitalWrite(10, LOW);
  74. digitalWrite(9, LOW);
  75. digitalWrite(8, LOW);
  76. break;
  77.  
  78. default:
  79. digitalWrite(13, LOW);
  80. digitalWrite(12, LOW);
  81. digitalWrite(11, LOW);
  82. digitalWrite(10, LOW);
  83. digitalWrite(9, LOW);
  84. digitalWrite(8, LOW);
  85. break;
  86.  
  87.  
  88. }
  89. Serial.print(typ);
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement