Advertisement
Tywais

VL53L0X_GC9A01A_ESP32C6

Apr 17th, 2025
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | Source Code | 0 0
  1. // GOOD FOR ESP32CS DEV MODULE
  2.  
  3. #include <Wire.h>
  4. #include <Adafruit_GC9A01A.h>
  5. #include <Adafruit_GFX.h>
  6. #include <VL53L0X.h>
  7.  
  8. // Define the Pins for the Display
  9. #define TFT_CS        5    // Chip select pin
  10. #define TFT_RST       1    // Reset pin
  11. #define TFT_DC        0    // Data/Command pin
  12. int distance = 0;
  13. int radiusmap = 0;
  14. int centerX;
  15. int centerY;
  16.  
  17. VL53L0X sensor;
  18.  
  19. // Create an instance of the display
  20. Adafruit_GC9A01A tft = Adafruit_GC9A01A(TFT_CS, TFT_DC, TFT_RST);
  21.  
  22. void setup() {
  23.   Serial.begin(115200);
  24.   Wire.setPins( 14,  15);
  25.   Wire.begin();
  26.  
  27.   sensor.setTimeout(500);
  28.   if (!sensor.init())
  29.   {
  30.     Serial.println("Failed to detect and initialize sensor!");
  31.     while (1) {}
  32.   }
  33.  
  34.   sensor.setTimeout(500);
  35.   if (!sensor.init())
  36.   {
  37.     Serial.println("Failed to detect and initialize sensor!");
  38.     while (1) {}
  39.   }
  40.   sensor.startContinuous();
  41.  
  42.   tft.begin();                   // Initialize the display
  43.   tft.setRotation(0);            // Set the rotation of the display
  44.   tft.fillScreen(GC9A01A_BLACK); // Fill the screen with black color
  45.  
  46.   // Draw a circle
  47.   centerX = tft.width() / 2;  // Center x of the display
  48.   centerY = tft.height() / 2; // Center y of the display
  49.   int diameter = 240;
  50.   int radius = diameter/2;
  51.  
  52.   tft.drawCircle(centerX, centerY, radius-10, GC9A01A_RED);
  53.  
  54.   tft.setTextColor(GC9A01A_GREEN,GC9A01A_BLACK);
  55.   tft.setTextSize(2);
  56. }
  57.  
  58. void loop() {
  59.   distance = sensor.readRangeContinuousMillimeters();
  60.   if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
  61.  
  62.   Serial.println();
  63.  
  64.   if (distance > 500) {
  65.     distance = 500;
  66.   }
  67.   Serial.println(distance);
  68.   radiusmap = map(distance,0,500,0,120);  //convert distance to radius
  69.   tft.fillScreen(GC9A01A_BLACK);          // Fill the screen with black color
  70.   tft.drawCircle(centerX, centerY, radiusmap, GC9A01A_RED);
  71.  
  72.   tft.setTextColor(GC9A01A_GREEN,GC9A01A_BLACK);
  73.   tft.setTextSize(2);
  74.  
  75.   tft.setCursor(105, 110); // Set cursor position
  76.   tft.print(distance);
  77.   delay(250);
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement