Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_DotStar.h>
- // libraries for depth sensor and LED strip
- #include <HCSR04.h>
- #include <Adafruit_dotStar.h>
- #include <SPI.h>
- // For the LED strip:
- // Select which (digital) pins you have connected to clock and data:
- #define dataPin 5
- #define clockPin 4
- // And set the number of LEDs in strip
- #define NUMPIXELS 10
- // Set up the strip. DOTSTAR_BGR says what order the colors are in. Can vary between strips.
- Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, dataPin, clockPin, DOTSTAR_BGR);
- // Initialize sensor that uses digital pins 13 and 12.
- int triggerPin = 11;
- int echoPin = 10;
- UltraSonicDistanceSensor distanceSensor(triggerPin, echoPin);
- void setup () {
- Serial.begin(9600); // We initialize serial connection so that we could print values from sensor.
- // These two lines "set up" the strip for use after power on.
- strip.begin();
- strip.show(); // Initialize all pixels to 'off'
- strip.setBrightness(30); // Dim the LEDs a bit. 255 is max.
- }
- // create the color of the strip
- uint32_t red = strip.Color(255, 0, 0);
- uint32_t black = strip.Color(0, 0, 0);
- void setallpix (uint32_t numLED) {
- uint32_t i;
- for (i = 0; i < 10; i++) {
- strip.setPixelColor(i, black);
- }
- for (i = 0; i < numLED; i++) {
- strip.setPixelColor(i, red);
- }
- }
- void loop () {
- // Every 500 miliseconds, do a measurement using the sensor and print the distance in centimeters.
- double distance = distanceSensor.measureDistanceCm();
- Serial.println(distance);
- if (distance > 100) {
- setallpix(10);
- strip.show();
- }
- else if (distance <= 100 && distance > 90) {
- setallpix(9);
- strip.show();
- }
- else if (distance <= 90 && distance > 80) {
- setallpix(8);
- strip.show();
- }
- else if (distance <= 80 && distance > 70) {
- setallpix(7);
- strip.show();
- }
- else if (distance <= 70 && distance > 60) {
- setallpix(6);
- strip.show();
- }
- else if (distance <= 60 && distance > 50) {
- setallpix(5);
- strip.show();
- }
- else if (distance <= 50 && distance > 40) {
- setallpix(4);
- strip.show();
- }
- else if (distance <= 40 && distance > 30) {
- setallpix(3);
- strip.show();
- }
- else if (distance <= 30 && distance > 20) {
- setallpix(2);
- strip.show();
- }
- else if (distance <= 20 && distance > 10) {
- setallpix(1);
- strip.show();
- }
- else {
- setallpix(0);
- strip.show();
- }
- delay(50);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement