Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2.  
  3. const int trigPin = 9;
  4. const int echoPin = 10;
  5. const int buzzer = 11;
  6.  
  7. #define dinPin 4
  8. #define numLEDs 5
  9.  
  10. Adafruit_NeoPixel strip = Adafruit_NeoPixel(numLEDs, dinPin, NEO_GRB + NEO_KHZ800);
  11.  
  12. long duration;
  13. int distance;
  14.  
  15. int blue;
  16. int green;
  17. int off;
  18.  
  19. boolean lightLED = false;
  20.  
  21.  
  22. void setup() {
  23. strip.begin();
  24. strip.setBrightness(80);
  25. strip.show();
  26.  
  27. pinMode(trigPin, OUTPUT);
  28. pinMode(echoPin, INPUT);
  29. pinMode(buzzer, OUTPUT);
  30. Serial.begin(9600);
  31. }
  32.  
  33.  
  34. void loop() {
  35. digitalWrite(trigPin, LOW);
  36. delayMicroseconds(2);
  37.  
  38. digitalWrite(trigPin, HIGH);
  39. delayMicroseconds(10);
  40. digitalWrite(trigPin, LOW);
  41.  
  42. duration = pulseIn(echoPin, HIGH);
  43.  
  44. distance = duration * 0.034 / 2;
  45.  
  46. uint32_t blue = strip.Color(0, 100, 255);
  47. uint32_t green = strip.Color(0, 255, 20);
  48. uint32_t off = strip.Color(0, 0, 0);
  49.  
  50. ///////////////////////////////////////////////////////////
  51. // LED 1 + Buzzer
  52. ///////////////////////////////////////////////////////////
  53.  
  54. if (distance <= 5) {
  55.  
  56. if (! lightLED) {
  57. strip.setPixelColor(0, blue);
  58. strip.show();
  59. delay(0);
  60. }
  61.  
  62. else {
  63. lightLED = false;
  64. }
  65.  
  66. digitalWrite(buzzer, HIGH);
  67. tone(buzzer, 400, 100);
  68. delay(75);
  69. tone(buzzer, 600, 100);
  70. delay(75);
  71. tone(buzzer, 800, 100);
  72. delay(75);
  73. noTone(buzzer);
  74.  
  75. }
  76.  
  77. else {
  78. digitalWrite(buzzer, LOW);
  79. }
  80.  
  81. ///////////////////////////////////////////////////////////
  82. // LED 2
  83. ///////////////////////////////////////////////////////////
  84.  
  85. if (lightLED && distance <= 5) {
  86.  
  87. lightLED = true;
  88.  
  89. if (! lightLED) {
  90. strip.setPixelColor(1, blue);
  91. strip.show();
  92. delay(0);
  93. }
  94. }
  95.  
  96. ///////////////////////////////////////////////////////////
  97. // DETECT USER PRESENCE
  98. ///////////////////////////////////////////////////////////
  99.  
  100. if (distance >= 6 && distance <= 20) {
  101. strip.setPixelColor(3, green);
  102. strip.setPixelColor(4, green);
  103. strip.show();
  104. delay(0);
  105. }
  106.  
  107. else {
  108. strip.setPixelColor(3, off);
  109. strip.setPixelColor(4, off);
  110. strip.show();
  111. delay(0);
  112. }
  113.  
  114. // Voir la distance dans le moniteur
  115. Serial.print("Distance: ");
  116. Serial.println(distance);
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement