How to Program a Time-of-Flight Sensor with an OLED Display (VL53L1X)

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ม.ค. 2025

ความคิดเห็น • 5

  • @CodeXXProgramming
    @CodeXXProgramming  9 วันที่ผ่านมา

    8-pin OLED Display Code:
    #include
    #include
    #include
    #include
    #include "Adafruit_VL53L1X.h"
    #define SCREEN_WIDTH 128
    #define SCREEN_HEIGHT 64
    #define OLED_MOSI 9
    #define OLED_CLK 10
    #define OLED_DC 11
    #define OLED_CS 12
    #define OLED_RESET 13
    #define XSHUT_PIN 3
    #define IRQ_PIN 2
    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
    Adafruit_VL53L1X vl53 = Adafruit_VL53L1X(XSHUT_PIN, IRQ_PIN);
    void setup() {
    if (!display.begin(SSD1306_SWITCHCAPVCC)) {
    while (1);
    }
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 0);
    display.println("Display OK");
    display.display();
    delay(1000);
    Wire.begin();
    if (!vl53.begin(0x29, &Wire)) {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.println("VL53L1X Error");
    display.display();
    while (1);
    }
    vl53.startRanging();
    display.clearDisplay();
    display.setCursor(0, 0);
    display.println("VL53L1X Ready");
    display.display();
    delay(1000);
    }
    void loop() {
    int16_t distance;
    if (vl53.dataReady()) {
    distance = vl53.distance();
    if (distance == -1) {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.setTextSize(1);
    display.println("Error Reading");
    display.display();
    } else {
    int distance_cm = distance / 10;
    display.clearDisplay();
    display.setTextSize(2);
    display.setCursor(0, 0);
    display.println("Distance:");
    display.setTextSize(3);
    display.setCursor(10, 30);
    display.print(distance_cm);
    display.println(" cm");
    display.display();
    }
    vl53.clearInterrupt();
    }
    delay(100);
    }

  • @nicolascomandos8934
    @nicolascomandos8934 5 วันที่ผ่านมา

    I don't know why but that seems something a horror game could use

  • @acero0777
    @acero0777 6 วันที่ผ่านมา

    marco ma sei crazy 500 views

  • @CodeXXProgramming
    @CodeXXProgramming  9 วันที่ผ่านมา

    4-pin OLED Display Code:
    #include
    #include
    #include
    #include "Adafruit_VL53L1X.h"
    #define SCREEN_WIDTH 128
    #define SCREEN_HEIGHT 64
    #define OLED_I2C_ADDRESS 0x3C
    #define XSHUT_PIN 3
    #define IRQ_PIN 2
    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
    Adafruit_VL53L1X vl53 = Adafruit_VL53L1X(XSHUT_PIN, IRQ_PIN);
    void setup() {
    if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_I2C_ADDRESS)) {
    while (1);
    }
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 0);
    display.println("Display OK");
    display.display();
    delay(1000);
    Wire.begin();
    if (!vl53.begin(0x29, &Wire)) {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.println("VL53L1X Error");
    display.display();
    while (1);
    }
    vl53.startRanging();
    display.clearDisplay();
    display.setCursor(0, 0);
    display.println("VL53L1X Ready");
    display.display();
    delay(1000);
    }
    void loop() {
    int16_t distance;
    if (vl53.dataReady()) {
    distance = vl53.distance();
    if (distance == -1) {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.setTextSize(1);
    display.println("Error Reading");
    display.display();
    } else {
    int distance_cm = distance / 10;
    display.clearDisplay();
    display.setTextSize(2);
    display.setCursor(0, 0);
    display.println("Distance:");
    display.setTextSize(3);
    display.setCursor(10, 30);
    display.print(distance_cm);
    display.println(" cm");
    display.display();
    }
    vl53.clearInterrupt();
    }
    delay(100);
    }