Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.55 KB | None | 0 0
  1. #include <Adafruit_DotStar.h>
  2. // libraries for depth sensor and LED strip
  3. #include <HCSR04.h>
  4. #include <Adafruit_dotStar.h>
  5. #include <SPI.h>
  6.  
  7. // For the LED strip:
  8. // Select which (digital) pins you have connected to clock and data:
  9. #define dataPin    5
  10. #define clockPin   4
  11. // And set the number of LEDs in strip
  12. #define NUMPIXELS 10
  13. // Set up the strip. DOTSTAR_BGR says what order the colors are in. Can vary between strips.
  14. Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, dataPin, clockPin, DOTSTAR_BGR);
  15.  
  16. // Initialize sensor that uses digital pins 13 and 12.
  17. int triggerPin = 11;
  18. int echoPin = 10;
  19. UltraSonicDistanceSensor distanceSensor(triggerPin, echoPin);
  20.  
  21. void setup () {
  22.     Serial.begin(9600);  // We initialize serial connection so that we could print values from sensor.
  23.     // These two lines "set up" the strip for use after power on.
  24.     strip.begin();
  25.     strip.show();              // Initialize all pixels to 'off'
  26.     strip.setBrightness(30);  // Dim the LEDs a bit. 255 is max.
  27. }
  28. // create the color of the strip
  29. uint32_t red = strip.Color(255, 0, 0);
  30. uint32_t black = strip.Color(0, 0, 0);
  31.  
  32. void setallpix (uint32_t numLED) {
  33.   uint32_t i;
  34.   for (i = 0; i < 10; i++) {
  35.     strip.setPixelColor(i, black);
  36.   }
  37.    for (i = 0; i < numLED; i++) {
  38.     strip.setPixelColor(i, red);
  39.   }
  40. }
  41.  
  42. void loop () {
  43.     // Every 500 miliseconds, do a measurement using the sensor and print the distance in centimeters.
  44.     double distance = distanceSensor.measureDistanceCm();
  45.     Serial.println(distance);
  46.     if (distance > 100) {
  47.       setallpix(10);
  48.       strip.show();
  49.     }
  50.     else if (distance <= 100 && distance > 90) {
  51.       setallpix(9);
  52.       strip.show();
  53.     }
  54.      else if (distance <= 90 && distance > 80) {
  55.       setallpix(8);
  56.       strip.show();
  57.     }
  58.     else if (distance <= 80 && distance > 70) {
  59.       setallpix(7);
  60.       strip.show();
  61.     }
  62.     else if (distance <= 70 && distance > 60) {
  63.       setallpix(6);
  64.       strip.show();
  65.     }
  66.     else if (distance <= 60 && distance > 50) {
  67.       setallpix(5);
  68.       strip.show();
  69.     }
  70.     else if (distance <= 50 && distance > 40) {
  71.       setallpix(4);
  72.       strip.show();
  73.     }
  74.     else if (distance <= 40 && distance > 30) {
  75.       setallpix(3);
  76.       strip.show();
  77.     }
  78.     else if (distance <= 30 && distance > 20) {
  79.       setallpix(2);
  80.       strip.show();
  81.     }
  82.     else if (distance <= 20 && distance > 10) {
  83.       setallpix(1);
  84.       strip.show();
  85.     }
  86.     else {
  87.       setallpix(0);
  88.       strip.show();
  89.     }
  90.    
  91.    
  92.     delay(50);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement