Advertisement
pleasedontcode

"Sensor Output" rev_04

Jun 15th, 2024
382
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 Output"
  13.     - Source Code compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2024-06-15 16:29:00
  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.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Wire.h>
  28. #include <EasyButton.h>  //https://github.com/evert-arias/EasyButton
  29. #include <Adafruit_VL6180X.h>  //https://github.com/adafruit/Adafruit_VL6180X
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs();
  35. void onButtonPressed();
  36. void readSensor();
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t button_PushButton_PIN_D2 = 2;
  40. const uint8_t tof_VL6180X_GPIO_PIN_D4 = 4;
  41.  
  42. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  43. const uint8_t tof_VL6180X_XSHUT_PIN_D3 = 3;
  44.  
  45. /***** DEFINITION OF I2C PINS *****/
  46. const uint8_t tof_VL6180X_I2C_PIN_SDA_A4 = A4;
  47. const uint8_t tof_VL6180X_I2C_PIN_SCL_A5 = A5;
  48. const uint8_t tof_VL6180X_I2C_SLAVE_ADDRESS = 41;
  49.  
  50. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  51. /***** used to store raw data *****/
  52. bool tof_VL6180X_XSHUT_PIN_D3_rawData = 0;
  53.  
  54. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  55. /***** used to store data after characteristic curve transformation *****/
  56. float tof_VL6180X_XSHUT_PIN_D3_phyData = 0.0;
  57.  
  58. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  59. EasyButton button(button_PushButton_PIN_D2);  // Initialize EasyButton with the button pin
  60. Adafruit_VL6180X vl = Adafruit_VL6180X();  // Initialize VL6180X sensor
  61.  
  62. void setup(void)
  63. {
  64.     // put your setup code here, to run once:
  65.     Serial.begin(115200);
  66.     Serial.println();
  67.     Serial.println(">>> EasyButton example with VL6180X <<<");
  68.  
  69.     pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
  70.     pinMode(tof_VL6180X_GPIO_PIN_D4, INPUT_PULLUP);
  71.     pinMode(tof_VL6180X_XSHUT_PIN_D3, OUTPUT);
  72.  
  73.     button.begin();  // Initialize the button
  74.     button.onSequence(2, 2000, onButtonPressed);  // Set the callback function for button press sequence
  75.  
  76.     if (!vl.begin()) {
  77.         Serial.println("Failed to find VL6180X sensor");
  78.         while (1);
  79.     }
  80.     Serial.println("VL6180X sensor found!");
  81. }
  82.  
  83. void loop(void)
  84. {
  85.     // put your main code here, to run repeatedly:
  86.     button.read();  // Read the button state
  87.     updateOutputs();  // Refresh output data
  88. }
  89.  
  90. void updateOutputs()
  91. {
  92.     digitalWrite(tof_VL6180X_XSHUT_PIN_D3, tof_VL6180X_XSHUT_PIN_D3_rawData);
  93. }
  94.  
  95. void onButtonPressed()
  96. {
  97.     Serial.println("Button pressed twice");
  98.     // Enable the sensor
  99.     digitalWrite(tof_VL6180X_XSHUT_PIN_D3, HIGH);
  100.     delay(10);  // Wait for the sensor to be ready
  101.  
  102.     // Read and display the sensor data
  103.     readSensor();
  104.  
  105.     // Disable the sensor after 2 seconds
  106.     delay(2000);
  107.     digitalWrite(tof_VL6180X_XSHUT_PIN_D3, LOW);
  108. }
  109.  
  110. void readSensor()
  111. {
  112.     float lux = vl.readLux(VL6180X_ALS_GAIN_5);
  113.     Serial.print("Lux: ");
  114.     Serial.println(lux);
  115.  
  116.     uint8_t range = vl.readRange();
  117.     uint8_t status = vl.readRangeStatus();
  118.  
  119.     if (status == VL6180X_ERROR_NONE) {
  120.         Serial.print("Range: ");
  121.         Serial.println(range);
  122.     } else {
  123.         Serial.println("Error reading range");
  124.     }
  125. }
  126.  
  127. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement