Advertisement
pleasedontcode

"Sensor Display" rev_03

Jun 15th, 2024
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Sensor Display"
  13.     - Source Code NOT compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2024-06-15 16:25:19
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* when button is pressed two times, so activate and */
  21.     /* read the sensor, display on serial for 2 seconds */
  22.     /* then disable the sensor again. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Wire.h>
  27. #include <EasyButton.h>  //https://github.com/evert-arias/EasyButton
  28. #include <Adafruit_VL6180X.h>  //https://github.com/adafruit/Adafruit_VL6180X
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void updateOutputs();
  34. void onButtonPressed();
  35. void readSensor();
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t button_PushButton_PIN_D2 = 2;
  39. const uint8_t tof_VL6180X_GPIO_PIN_D4 = 4;
  40.  
  41. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  42. const uint8_t tof_VL6180X_XSHUT_PIN_D3 = 3;
  43.  
  44. /***** DEFINITION OF I2C PINS *****/
  45. const uint8_t tof_VL6180X_I2C_PIN_SDA_A4 = A4;
  46. const uint8_t tof_VL6180X_I2C_PIN_SCL_A5 = A5;
  47. const uint8_t tof_VL6180X_I2C_SLAVE_ADDRESS = 41;
  48.  
  49. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  50. /***** used to store raw data *****/
  51. bool tof_VL6180X_XSHUT_PIN_D3_rawData = 0;
  52.  
  53. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  54. /***** used to store data after characteristic curve transformation *****/
  55. float tof_VL6180X_XSHUT_PIN_D3_phyData = 0.0;
  56.  
  57. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  58. EasyButton button(button_PushButton_PIN_D2);  // Initialize EasyButton with the button pin
  59. Adafruit_VL6180X vl = Adafruit_VL6180X();  // Initialize VL6180X sensor
  60.  
  61. void setup(void)
  62. {
  63.     // put your setup code here, to run once:
  64.     Serial.begin(115200);
  65.     Serial.println();
  66.     Serial.println(">>> EasyButton example with VL6180X <<<");
  67.  
  68.     pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
  69.     pinMode(tof_VL6180X_GPIO_PIN_D4, INPUT_PULLUP);
  70.     pinMode(tof_VL6180X_XSHUT_PIN_D3, OUTPUT);
  71.  
  72.     button.begin();  // Initialize the button
  73.     button.onSequence(2, 2000, onButtonPressed);  // Set the callback function for button press sequence
  74.  
  75.     if (!vl.begin()) {
  76.         Serial.println("Failed to find VL6180X sensor");
  77.         while (1);
  78.     }
  79.     Serial.println("VL6180X sensor found!");
  80. }
  81.  
  82. void loop(void)
  83. {
  84.     // put your main code here, to run repeatedly:
  85.     button.read();  // Read the button state
  86.     updateOutputs();  // Refresh output data
  87. }
  88.  
  89. void updateOutputs()
  90. {
  91.     digitalWrite(tof_VL6180X_XSHUT_PIN_D3, tof_VL6180X_XSHUT_PIN_D3_rawData);
  92. }
  93.  
  94. void onButtonPressed()
  95. {
  96.     Serial.println("Button pressed twice");
  97.     // Enable the sensor
  98.     digitalWrite(tof_VL6180X_XSHUT_PIN_D3, HIGH);
  99.     delay(10);  // Wait for the sensor to be ready
  100.  
  101.     // Read and display the sensor data
  102.     readSensor();
  103.  
  104.     // Disable the sensor after 2 seconds
  105.     delay(2000);
  106.     digitalWrite(tof_VL6180X_XSHUT_PIN_D3, LOW);
  107. }
  108.  
  109. void readSensor()
  110. {
  111.     float lux = vl.readLux(VL6180X_ALS_GAIN_5);
  112.     Serial.print("Lux: ");
  113.     Serial.println(lux);
  114.  
  115.     uint8_t range = vl.readRange();
  116.     uint8_t status = vl.readRangeStatus();
  117.  
  118.     if (status == VL6180X_ERROR_NONE) {
  119.         Serial.print("Range: ");
  120.         Serial.println(range);
  121.     } else {
  122.         Serial.println("Error reading range");
  123.     }
  124. }
  125.  
  126. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement