Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.8 kB
4
Indexable
Never
//#include "Adafruit_VL53L1X.h"


#include <Wire.h>
#include "Adafruit_VL53L1X.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

#define IRQ_PIN 2
#define XSHUT_PIN 3

#define PANTO 10
#define LED_PIN 8
#define LED_COUNT 1



Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_VL53L1X vl53 = Adafruit_VL53L1X(XSHUT_PIN, IRQ_PIN);

int jarak = 0;

void setup() {
  pinMode (PANTO, OUTPUT);
  Serial.begin(115200);
  while (!Serial) delay(10);

  //Serial.println(F("Adafruit VL53L1X sensor demo"));

  Wire.begin();
  if (! vl53.begin(0x29, &Wire)) {
    Serial.print(F("Error on init of VL sensor: "));
    Serial.println(vl53.vl_status);
    while (1)       delay(10);
  }
  Serial.println(F("VL53L1X sensor OK!"));

  Serial.print(F("Sensor ID: 0x"));
  Serial.println(vl53.sensorID(), HEX);

  if (! vl53.startRanging()) {
    Serial.print(F("Couldn't start ranging: "));
    Serial.println(vl53.vl_status);
    while (1)       delay(10);
  }
  Serial.println(F("Ranging started"));

  // Valid timing budgets: 15, 20, 33, 50, 100, 200 and 500ms!
  vl53.setTimingBudget(50);
  Serial.print(F("Timing budget (ms): "));
  Serial.println(vl53.getTimingBudget());

  /*
  vl.VL53L1X_SetDistanceThreshold(100, 300, 3, 1);
  vl.VL53L1X_SetInterruptPolarity(0);
  */
}

void loop() {
  int16_t distance;

  if (vl53.dataReady()) {
    // new measurement for the taking!
    distance = vl53.distance();
    if (distance == -1) {
      // something went wrong!
      Serial.print(F("Couldn't get distance: "));
      Serial.println(vl53.vl_status);
      return;
    }
    jarak = distance;
     if ( jarak > 70 && jarak <=100 ){
        Serial.println("NYALA");
        digitalWrite(PANTO, HIGH );
        colorWipe(strip.Color(255,   0,   0), 50); // Red
        delay(6000);
        
      }
      else {
        Serial.println("MATI");
        colorWipe(strip.Color(0,   0,   255), 50); // Red
        digitalWrite(PANTO, LOW );
        //delay(6000);
      }
      delay(50);
    
    //Serial.print(F("Distance: "));
    //Serial.print(distance);
    //Serial.println(" mm");

    // data is read out, time for another reading!
    vl53.clearInterrupt();
  }
}

void colorWipe(uint32_t color, int wait) {
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}
Leave a Comment