CCCPaste

"Sensor Display" rev_03

/********* 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 Display"
	- Source Code NOT compiled for: Arduino Pro Mini 5V
	- Source Code created on: 2024-06-15 16:25:19

********* 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_VL6180X

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs();
void onButtonPressed();
void readSensor();

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

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

/***** DEFINITION OF I2C PINS *****/
const uint8_t tof_VL6180X_I2C_PIN_SDA_A4 = A4;
const uint8_t tof_VL6180X_I2C_PIN_SCL_A5 = A5;
const uint8_t tof_VL6180X_I2C_SLAVE_ADDRESS = 41;

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

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button(button_PushButton_PIN_D2);  // Initialize EasyButton with the button pin
Adafruit_VL6180X vl = Adafruit_VL6180X();  // Initialize VL6180X sensor

void setup(void)
{
    // put your setup code here, to run once:
    Serial.begin(115200);
    Serial.println();
    Serial.println(">>> EasyButton example with VL6180X <<<");

    pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
    pinMode(tof_VL6180X_GPIO_PIN_D4, INPUT_PULLUP);
    pinMode(tof_VL6180X_XSHUT_PIN_D3, OUTPUT);

    button.begin();  // Initialize the button
    button.onSequence(2, 2000, onButtonPressed);  // Set the callback function for button press sequence

    if (!vl.begin()) {
        Serial.println("Failed to find VL6180X sensor");
        while (1);
    }
    Serial.println("VL6180X sensor found!");
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    button.read();  // Read the button state
    updateOutputs();  // Refresh output data
}

void updateOutputs()
{
    digitalWrite(tof_VL6180X_XSHUT_PIN_D3, tof_VL6180X_XSHUT_PIN_D3_rawData);
}

void onButtonPressed()
{
    Serial.println("Button pressed twice");
    // Enable the sensor
    digitalWrite(tof_VL6180X_XSHUT_PIN_D3, HIGH);
    delay(10);  // Wait for the sensor to be ready

    // Read and display the sensor data
    readSensor();

    // Disable the sensor after 2 seconds
    delay(2000);
    digitalWrite(tof_VL6180X_XSHUT_PIN_D3, LOW);
}

void readSensor()
{
    float lux = vl.readLux(VL6180X_ALS_GAIN_5);
    Serial.print("Lux: ");
    Serial.println(lux);

    uint8_t range = vl.readRange();
    uint8_t status = vl.readRangeStatus();

    if (status == VL6180X_ERROR_NONE) {
        Serial.print("Range: ");
        Serial.println(range);
    } else {
        Serial.println("Error reading range");
    }
}

/* END CODE */