Advertisement
pleasedontcode

"Sensor Activation" rev_02

Jun 15th, 2024
366
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 Activation"
  13.     - Source Code NOT compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2024-06-15 16:18:08
  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_VL53L0X.h>  //https://github.com/adafruit/Adafruit_VL53L0X
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void onButtonPressed();
  34. void VL53LOXISR();
  35. void updateOutputs();
  36. void activateSensor();
  37. void deactivateSensor();
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t button_PushButton_PIN_D2 = 2;
  41. const uint8_t sensor_VL53L0X_GPIO_PIN_D4 = 4;
  42.  
  43. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  44. const uint8_t sensor_VL53L0X_XSHUT_PIN_D3 = 3;
  45.  
  46. /***** DEFINITION OF I2C PINS *****/
  47. const uint8_t sensor_VL53L0X_I2C_PIN_SDA_A4 = A4;
  48. const uint8_t sensor_VL53L0X_I2C_PIN_SCL_A5 = A5;
  49. const uint8_t sensor_VL53L0X_I2C_SLAVE_ADDRESS = 41;
  50.  
  51. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  52. /***** used to store raw data *****/
  53. bool sensor_VL53L0X_XSHUT_PIN_D3_rawData = 0;
  54.  
  55. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  56. /***** used to store data after characteristic curve transformation *****/
  57. float sensor_VL53L0X_XSHUT_PIN_D3_phyData = 0.0;
  58.  
  59. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  60. EasyButton button(button_PushButton_PIN_D2); // Initialize EasyButton instance
  61. Adafruit_VL53L0X lox = Adafruit_VL53L0X(); // Initialize Adafruit_VL53L0X instance
  62.  
  63. /****** GLOBAL VARIABLES *****/
  64. volatile byte VL53LOX_State = LOW;
  65. bool sensorActive = false;
  66.  
  67. void setup(void)
  68. {
  69.   // put your setup code here, to run once:
  70.  
  71.   pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
  72.   pinMode(sensor_VL53L0X_GPIO_PIN_D4, INPUT_PULLUP);
  73.   pinMode(sensor_VL53L0X_XSHUT_PIN_D3, OUTPUT);
  74.  
  75.   // Initialize the button
  76.   button.begin();
  77.   // Attach the callback function to be called when the button is pressed twice
  78.   button.onSequence(2, 2000, onButtonPressed);
  79.  
  80.   // Initialize Serial for debugging purposes
  81.   Serial.begin(115200);
  82.   while (!Serial) { delay(1); }
  83.  
  84.   // Attach interrupt for VL53L0X
  85.   attachInterrupt(digitalPinToInterrupt(sensor_VL53L0X_GPIO_PIN_D4), VL53LOXISR, CHANGE);
  86. }
  87.  
  88. void loop(void)
  89. {
  90.   // put your main code here, to run repeatedly:
  91.  
  92.   // Continuously read the status of the button
  93.   button.read();
  94.  
  95.   updateOutputs(); // Refresh output data
  96.  
  97.   if (sensorActive && VL53LOX_State == LOW) {
  98.     VL53L0X_RangingMeasurementData_t measure;
  99.     lox.getRangingMeasurement(&measure, false);
  100.  
  101.     if (measure.RangeStatus != 4) {
  102.       Serial.print("Distance (mm): ");
  103.       Serial.println(measure.RangeMilliMeter);
  104.     } else {
  105.       Serial.println("Out of range");
  106.     }
  107.     lox.clearInterruptMask(false);
  108.   } else {
  109.     delay(10);
  110.   }
  111. }
  112.  
  113. void updateOutputs()
  114. {
  115.   digitalWrite(sensor_VL53L0X_XSHUT_PIN_D3, sensor_VL53L0X_XSHUT_PIN_D3_rawData);
  116. }
  117.  
  118. void onButtonPressed()
  119. {
  120.   Serial.println("Button pressed twice");
  121.   activateSensor();
  122.   delay(2000); // Keep the sensor active for 2 seconds
  123.   deactivateSensor();
  124. }
  125.  
  126. void VL53LOXISR() {
  127.   VL53LOX_State = digitalRead(sensor_VL53L0X_GPIO_PIN_D4);
  128.   digitalWrite(LED_BUILTIN, VL53LOX_State);
  129. }
  130.  
  131. void activateSensor() {
  132.   // Initialize VL53L0X sensor
  133.   while (!lox.begin()) {
  134.     Serial.println(F("Failed to boot VL53L0X"));
  135.     digitalWrite(sensor_VL53L0X_XSHUT_PIN_D3, LOW);
  136.     delay(100);
  137.     digitalWrite(sensor_VL53L0X_XSHUT_PIN_D3, HIGH);
  138.     delay(100);
  139.   }
  140.  
  141.   lox.setGpioConfig(VL53L0X_DEVICEMODE_CONTINUOUS_RANGING,
  142.                     VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW,
  143.                     VL53L0X_INTERRUPTPOLARITY_LOW);
  144.  
  145.   FixPoint1616_t LowThreashHold = (50 * 65536.0);
  146.   FixPoint1616_t HighThreashHold = (100 * 65536.0);
  147.   lox.setInterruptThresholds(LowThreashHold, HighThreashHold, true);
  148.  
  149.   lox.setDeviceMode(VL53L0X_DEVICEMODE_CONTINUOUS_RANGING, false);
  150.   lox.startMeasurement();
  151.  
  152.   sensorActive = true;
  153. }
  154.  
  155. void deactivateSensor() {
  156.   sensorActive = false;
  157.   digitalWrite(sensor_VL53L0X_XSHUT_PIN_D3, LOW); // Disable the sensor
  158. }
  159.  
  160. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement