Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <SparkFun_VL53L1X.h>
- #include <Wire.h>
- /* I2C-1 standard pins: PB7(sda) PB6(scl) */
- /*
- // Default pin used for generic 'Serial' instance
- // Mandatory for Firmata
- #ifndef PIN_SERIAL_RX
- #define PIN_SERIAL_RX PA3
- #endif
- #ifndef PIN_SERIAL_TX
- #define PIN_SERIAL_TX PA2
- #endif
- */
- #define OFFSET_CALIBRATION_DISTANCE 140 // mm
- #define XTALK_CALIBRATION_DISTANCE 500 // mm
- int xtalkCalibrationDistance = XTALK_CALIBRATION_DISTANCE;
- #define SHUTDOWN_PIN PB7
- #define INTERRUPT_PIN PB6
- SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN);
- #define Button_Pin PA0
- unsigned long oneButtonPeriod = 100; // the debounce time; increase if the output flickers
- int buttonState; // the current reading from the input pin
- int lastButtonState = LOW; // the previous reading from the input pin
- // VL53L1X* ranger = distanceSensor._device;
- void readDistance();
- void calibrateXTalk();
- void calibrateOffset();
- void setup()
- {
- pinMode(LED_BUILTIN,OUTPUT);
- digitalWrite(LED_BUILTIN, LOW);
- pinMode(Button_Pin, INPUT);
- Serial.setTx(PB10);
- Serial.setRx(PB11);
- Serial.begin(115200);
- Wire.setSDA(PB9);
- Wire.setSCL(PB8);
- Wire.setClock(10000U);
- Wire.begin();
- Serial.println("VL53L1X Qwiic Test");
- if (distanceSensor.begin() != 0) // Begin returns 0 on a good init
- {
- Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
- while (1)
- ;
- }
- Serial.println("Sensor online!");
- distanceSensor.setDistanceModeShort();
- distanceSensor.setTimingBudgetInMs(100);
- distanceSensor.setOffset((int16_t)2034);
- // ranger.VL53L1X_SensorInit();
- // ranger.VL53L1X_StartRanging();
- delay(100);
- digitalWrite(LED_BUILTIN, HIGH);
- }
- void loop()
- {
- static uint32_t prevmillis = 0;
- static int pressedButton = 0;
- if (millis() - prevmillis >= oneButtonPeriod)
- {
- bool buttonState = digitalRead(Button_Pin);
- pressedButton += buttonState;
- if (pressedButton >= 1 && buttonState == LOW /* Unpressed */)
- {
- Serial.printf("Debounced button\n");
- if (pressedButton > 10 /* Long press */)
- {
- calibrateXTalk();
- }
- else
- {
- calibrateOffset();
- }
- pressedButton = 0;
- }
- prevmillis = millis();
- }
- // readDistance();
- // delay(1000);
- static uint32_t prevmillis_Distance;
- if (millis() - prevmillis_Distance >= 500)
- {
- readDistance();
- prevmillis_Distance = millis();
- if (Serial.available())
- {
- auto i = Serial.parseInt();
- if (i)
- {
- Serial.printf("The xTalk distance is set to %d\n", i);
- xtalkCalibrationDistance = i;
- }
- }
- }
- }
- void calibrateXTalk()
- {
- auto currentXtalk = distanceSensor.getXTalk();
- distanceSensor.calibrateXTalk(xtalkCalibrationDistance);
- auto calibratedXtalk = distanceSensor.getXTalk();
- Serial.printf("\tcurrentXtalk=%u\tcalibratedXtalk=%u\n", currentXtalk, calibratedXtalk);
- }
- void calibrateOffset()
- {
- auto currentOffset = distanceSensor.getOffset();
- distanceSensor.calibrateOffset(OFFSET_CALIBRATION_DISTANCE);
- auto calibratedOffset = distanceSensor.getOffset();
- Serial.printf("\tcurrentOffset=%u\tcalibratedOffset=%u\n", currentOffset, calibratedOffset);
- }
- void readDistance()
- {
- distanceSensor.startOneshotRanging(); // Write configuration bytes to initiate measurement
- while (!distanceSensor.checkForDataReady())
- {
- delay(1);
- }
- int distance = distanceSensor.getDistance(); // Get the result of the measurement from the sensor
- distanceSensor.clearInterrupt();
- distanceSensor.stopRanging();
- Serial.printf("Distance(mm): %d\n", distance);
- // float distanceInches = distance * 0.0393701;
- // float distanceFeet = distanceInches / 12.0;
- // Serial.print("\tDistance(ft): ");
- // Serial.print(distanceFeet, 2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement