CCCPaste

"Sensor Activation" rev_02

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Sensor Activation"
	- Source Code NOT compiled for: Arduino Pro Mini 5V
	- Source Code created on: 2024-06-15 16:18:08

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* when button is pressed two times, so activate and */
	/* read the sensor, display on serial for 2 seconds */
	/* then disable the sensor again. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include   //https://github.com/evert-arias/EasyButton
#include   //https://github.com/adafruit/Adafruit_VL53L0X

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void onButtonPressed();
void VL53LOXISR();
void updateOutputs();
void activateSensor();
void deactivateSensor();

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t button_PushButton_PIN_D2 = 2;
const uint8_t sensor_VL53L0X_GPIO_PIN_D4 = 4;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t sensor_VL53L0X_XSHUT_PIN_D3 = 3;

/***** DEFINITION OF I2C PINS *****/
const uint8_t sensor_VL53L0X_I2C_PIN_SDA_A4 = A4;
const uint8_t sensor_VL53L0X_I2C_PIN_SCL_A5 = A5;
const uint8_t sensor_VL53L0X_I2C_SLAVE_ADDRESS = 41;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool sensor_VL53L0X_XSHUT_PIN_D3_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float sensor_VL53L0X_XSHUT_PIN_D3_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button(button_PushButton_PIN_D2); // Initialize EasyButton instance
Adafruit_VL53L0X lox = Adafruit_VL53L0X(); // Initialize Adafruit_VL53L0X instance

/****** GLOBAL VARIABLES *****/
volatile byte VL53LOX_State = LOW;
bool sensorActive = false;

void setup(void)
{
  // put your setup code here, to run once:

  pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
  pinMode(sensor_VL53L0X_GPIO_PIN_D4, INPUT_PULLUP);
  pinMode(sensor_VL53L0X_XSHUT_PIN_D3, OUTPUT);

  // Initialize the button
  button.begin();
  // Attach the callback function to be called when the button is pressed twice
  button.onSequence(2, 2000, onButtonPressed);

  // Initialize Serial for debugging purposes
  Serial.begin(115200);
  while (!Serial) { delay(1); }

  // Attach interrupt for VL53L0X
  attachInterrupt(digitalPinToInterrupt(sensor_VL53L0X_GPIO_PIN_D4), VL53LOXISR, CHANGE);
}

void loop(void)
{
  // put your main code here, to run repeatedly:

  // Continuously read the status of the button
  button.read();

  updateOutputs(); // Refresh output data

  if (sensorActive && VL53LOX_State == LOW) {
    VL53L0X_RangingMeasurementData_t measure;
    lox.getRangingMeasurement(&measure, false);

    if (measure.RangeStatus != 4) {
      Serial.print("Distance (mm): ");
      Serial.println(measure.RangeMilliMeter);
    } else {
      Serial.println("Out of range");
    }
    lox.clearInterruptMask(false);
  } else {
    delay(10);
  }
}

void updateOutputs()
{
  digitalWrite(sensor_VL53L0X_XSHUT_PIN_D3, sensor_VL53L0X_XSHUT_PIN_D3_rawData);
}

void onButtonPressed()
{
  Serial.println("Button pressed twice");
  activateSensor();
  delay(2000); // Keep the sensor active for 2 seconds
  deactivateSensor();
}

void VL53LOXISR() {
  VL53LOX_State = digitalRead(sensor_VL53L0X_GPIO_PIN_D4);
  digitalWrite(LED_BUILTIN, VL53LOX_State);
}

void activateSensor() {
  // Initialize VL53L0X sensor
  while (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    digitalWrite(sensor_VL53L0X_XSHUT_PIN_D3, LOW);
    delay(100);
    digitalWrite(sensor_VL53L0X_XSHUT_PIN_D3, HIGH);
    delay(100);
  }

  lox.setGpioConfig(VL53L0X_DEVICEMODE_CONTINUOUS_RANGING,
                    VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW,
                    VL53L0X_INTERRUPTPOLARITY_LOW);

  FixPoint1616_t LowThreashHold = (50 * 65536.0);
  FixPoint1616_t HighThreashHold = (100 * 65536.0);
  lox.setInterruptThresholds(LowThreashHold, HighThreashHold, true);

  lox.setDeviceMode(VL53L0X_DEVICEMODE_CONTINUOUS_RANGING, false);
  lox.startMeasurement();

  sensorActive = true;
}

void deactivateSensor() {
  sensorActive = false;
  digitalWrite(sensor_VL53L0X_XSHUT_PIN_D3, LOW); // Disable the sensor
}

/* END CODE */