Detected people approaching if the distance is < 6 feet then LEDs blinking RED otherwise LEDs light green.

Dependencies:   mbed X_NUCLEO_53L1A1_mbed WS2812

Committer:
elab
Date:
Sun Nov 01 20:23:28 2020 +0000
Revision:
1:092ee96f479a
Parent:
0:ea8694e55809
Changed detection threshold distance from 1 m to 1.9 m (6 feet)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elab 0:ea8694e55809 1 #include <stdio.h>
elab 0:ea8694e55809 2
elab 0:ea8694e55809 3 #include "mbed.h"
elab 0:ea8694e55809 4 #include "VL53L1X_I2C.h"
elab 0:ea8694e55809 5 #include "VL53L1X_Class.h"
elab 0:ea8694e55809 6 #include "LED_WS2812.h"
elab 0:ea8694e55809 7
elab 0:ea8694e55809 8 #define VL53L1_I2C_SDA D14
elab 0:ea8694e55809 9 #define VL53L1_I2C_SCL D15
elab 0:ea8694e55809 10
elab 0:ea8694e55809 11 static VL53L1X *sensor = NULL;
elab 0:ea8694e55809 12 Serial pc(SERIAL_TX, SERIAL_RX);
elab 0:ea8694e55809 13
elab 0:ea8694e55809 14 /* flags that handle interrupt request for sensor and user blue button*/
elab 0:ea8694e55809 15 volatile bool int_sensor = false;
elab 0:ea8694e55809 16 volatile bool int_stop = false;
elab 0:ea8694e55809 17
elab 0:ea8694e55809 18
elab 0:ea8694e55809 19 /* ISR callback function of the sensor */
elab 0:ea8694e55809 20 void sensor_irq(void)
elab 0:ea8694e55809 21 {
elab 0:ea8694e55809 22 int_sensor = true;
elab 0:ea8694e55809 23 sensor->disable_interrupt_measure_detection_irq();
elab 0:ea8694e55809 24 }
elab 0:ea8694e55809 25
elab 0:ea8694e55809 26 /* ISR callback function of the user blue button to switch measuring sensor. */
elab 0:ea8694e55809 27 void measuring_stop_irq(void)
elab 0:ea8694e55809 28 {
elab 0:ea8694e55809 29 int_stop = true;
elab 0:ea8694e55809 30 }
elab 0:ea8694e55809 31
elab 0:ea8694e55809 32 /* Start the sensor ranging */
elab 0:ea8694e55809 33 int start_ranging()
elab 0:ea8694e55809 34 {
elab 0:ea8694e55809 35 int status = 0;
elab 0:ea8694e55809 36 /* start the measure on the sensor */
elab 0:ea8694e55809 37 if (NULL != sensor) {
elab 0:ea8694e55809 38 status = sensor->stop_measurement();
elab 0:ea8694e55809 39 if (status != 0) {
elab 0:ea8694e55809 40 return status;
elab 0:ea8694e55809 41 }
elab 0:ea8694e55809 42
elab 0:ea8694e55809 43 status = sensor->start_measurement(&sensor_irq);
elab 0:ea8694e55809 44 if (status != 0) {
elab 0:ea8694e55809 45 return status;
elab 0:ea8694e55809 46 }
elab 0:ea8694e55809 47 }
elab 0:ea8694e55809 48 return status;
elab 0:ea8694e55809 49 }
elab 0:ea8694e55809 50
elab 0:ea8694e55809 51 int range_measure(VL53L1X_DevI2C *device_i2c)
elab 0:ea8694e55809 52 {
elab 0:ea8694e55809 53 int status = 0;
elab 0:ea8694e55809 54 uint16_t distance = 0;
elab 0:ea8694e55809 55 int distance_close = 0;
elab 0:ea8694e55809 56
elab 0:ea8694e55809 57 LED_WS2812 LED(A0,12);
elab 0:ea8694e55809 58 LED.SetIntensity(5);
elab 0:ea8694e55809 59 LED.SetColor(BLACK);
elab 0:ea8694e55809 60
elab 0:ea8694e55809 61 /* Create a xshutdown pin */
elab 0:ea8694e55809 62 DigitalOut xshutdown(D10);
elab 0:ea8694e55809 63
elab 0:ea8694e55809 64 /* create instance of sensor class */
elab 0:ea8694e55809 65 sensor = new VL53L1X(device_i2c, &xshutdown, D11);
elab 0:ea8694e55809 66
elab 0:ea8694e55809 67 sensor->vl53l1_off();
elab 0:ea8694e55809 68 /* initialise sensor */
elab 0:ea8694e55809 69 sensor->init_sensor(0x52);
elab 0:ea8694e55809 70
elab 0:ea8694e55809 71 if (status) {
elab 0:ea8694e55809 72 delete sensor;
elab 0:ea8694e55809 73 sensor= NULL;
elab 0:ea8694e55809 74 printf("Sensor centre not present\n\r");
elab 0:ea8694e55809 75 }
elab 0:ea8694e55809 76 status = sensor->vl53l1x_set_timing_budget_in_ms(100); // good trade off between accuracy and power consumption
elab 0:ea8694e55809 77 status = sensor->vl53l1x_set_inter_measurement_in_ms(500); // perform one ranging every second
elab 1:092ee96f479a 78 status = sensor->vl53l1x_set_distance_threshold(1900, 1900, 0, 0); // interrupt if distance < 1.9 m (~6 feet)
elab 0:ea8694e55809 79 status = start_ranging();
elab 0:ea8694e55809 80 if (status != 0) {
elab 0:ea8694e55809 81 printf("Failed to start ranging!\r\n");
elab 0:ea8694e55809 82 return status;
elab 0:ea8694e55809 83 }
elab 0:ea8694e55809 84 if (NULL != sensor) {
elab 0:ea8694e55809 85 printf("Entering loop mode\r\n");
elab 0:ea8694e55809 86 /* Main ranging interrupt loop */
elab 0:ea8694e55809 87 while (true) {
elab 0:ea8694e55809 88 if (int_sensor) {
elab 0:ea8694e55809 89 distance_close = 1;
elab 0:ea8694e55809 90 int_sensor = false;
elab 0:ea8694e55809 91 status = sensor->handle_irq(&distance);
elab 0:ea8694e55809 92 // printf("distance: %d\r\n", distance);
elab 0:ea8694e55809 93 }
elab 0:ea8694e55809 94 if (distance_close)
elab 0:ea8694e55809 95 {
elab 0:ea8694e55809 96 LED.InsertColor(RED);
elab 0:ea8694e55809 97 LED.StartBlink(0.2);
elab 0:ea8694e55809 98 wait_ms(500);
elab 0:ea8694e55809 99 LED.StopBlink();
elab 0:ea8694e55809 100 distance_close = 0;
elab 0:ea8694e55809 101 }
elab 0:ea8694e55809 102 else
elab 0:ea8694e55809 103 {
elab 0:ea8694e55809 104 LED.SetColor(GREEN);
elab 0:ea8694e55809 105 wait_ms(500);
elab 0:ea8694e55809 106 }
elab 0:ea8694e55809 107 }
elab 0:ea8694e55809 108 }
elab 0:ea8694e55809 109
elab 0:ea8694e55809 110 return status;
elab 0:ea8694e55809 111
elab 0:ea8694e55809 112 }
elab 0:ea8694e55809 113
elab 0:ea8694e55809 114
elab 0:ea8694e55809 115 /*=================================== Main ==================================
elab 0:ea8694e55809 116 =============================================================================*/
elab 0:ea8694e55809 117 int main()
elab 0:ea8694e55809 118 {
elab 0:ea8694e55809 119 #if TARGET_STM // we are cross compiling for an STM32-Nucleo
elab 0:ea8694e55809 120 InterruptIn stop_button(USER_BUTTON);
elab 0:ea8694e55809 121 stop_button.rise(&measuring_stop_irq);
elab 0:ea8694e55809 122 #endif
elab 0:ea8694e55809 123 #if TARGET_Freescale // we are cross-compiling for NXP FRDM boards.
elab 0:ea8694e55809 124 InterruptIn stop_button(SW2);
elab 0:ea8694e55809 125 stop_button.rise(&measuring_stop_irq);
elab 0:ea8694e55809 126 #endif
elab 0:ea8694e55809 127 VL53L1X_DevI2C *device_i2c = new VL53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);
elab 0:ea8694e55809 128 range_measure(device_i2c); // start continuous measures
elab 0:ea8694e55809 129 }