Capteur ST

Dependencies:   VL6180x mbed Servo

Fork of VL6180X_Explorer by Ian Kilburn

Committer:
highroads
Date:
Sun May 10 07:49:08 2015 +0000
Revision:
0:47f8714fe967
Child:
1:126b6cd0f4f5
Example for VL6180x library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
highroads 0:47f8714fe967 1 /******************************************************************************
highroads 0:47f8714fe967 2 * Developed from
highroads 0:47f8714fe967 3 * VL6180X_demo.ino
highroads 0:47f8714fe967 4 * Example Sketch for VL6180x time of flight range finder.
highroads 0:47f8714fe967 5 * Casey Kuhns @ SparkFun Electronics
highroads 0:47f8714fe967 6 * 10/29/2014
highroads 0:47f8714fe967 7 * https://github.com/sparkfun/ToF_Range_Finder-VL6180_Library
highroads 0:47f8714fe967 8 *
highroads 0:47f8714fe967 9 * The VL6180x by ST micro is a time of flight range finder that
highroads 0:47f8714fe967 10 * uses pulsed IR light to determine distances from object at close
highroads 0:47f8714fe967 11 * range. The average range of a sensor is between 0-200mm
highroads 0:47f8714fe967 12 *
highroads 0:47f8714fe967 13 * Resources:
highroads 0:47f8714fe967 14 * This library uses the Arduino Wire.h to complete I2C transactions.
highroads 0:47f8714fe967 15 *
highroads 0:47f8714fe967 16 * Development environment specifics:
highroads 0:47f8714fe967 17 * IDE: Arduino 1.0.5
highroads 0:47f8714fe967 18 * Hardware Platform: Arduino Pro 3.3V/8MHz
highroads 0:47f8714fe967 19 * VL6180x Breakout Version: 1.0
highroads 0:47f8714fe967 20 *
highroads 0:47f8714fe967 21 *
highroads 0:47f8714fe967 22 * This code is beerware. If you see me (or any other SparkFun employee) at the
highroads 0:47f8714fe967 23 * local pub, and you've found our code helpful, please buy us a round!
highroads 0:47f8714fe967 24 *
highroads 0:47f8714fe967 25 * Distributed as-is; no warranty is given.
highroads 0:47f8714fe967 26 ******************************************************************************/
highroads 0:47f8714fe967 27 #include "mbed.h"
highroads 0:47f8714fe967 28 #include <VL6180x.h>
highroads 0:47f8714fe967 29
highroads 0:47f8714fe967 30 /*const float GAIN_1 = 1.01; // Actual ALS Gain of 1.01
highroads 0:47f8714fe967 31 const float GAIN_1_25 = 1.28; // Actual ALS Gain of 1.28
highroads 0:47f8714fe967 32 const float GAIN_1_67 = 1.72; // Actual ALS Gain of 1.72
highroads 0:47f8714fe967 33 const float GAIN_2_5 = 2.6; // Actual ALS Gain of 2.60
highroads 0:47f8714fe967 34 const float GAIN_5 = 5.21; // Actual ALS Gain of 5.21
highroads 0:47f8714fe967 35 const float GAIN_10 = 10.32; // Actual ALS Gain of 10.32
highroads 0:47f8714fe967 36 const float GAIN_20 = 20; // Actual ALS Gain of 20
highroads 0:47f8714fe967 37 const float GAIN_40 = 40; // Actual ALS Gain of 40
highroads 0:47f8714fe967 38 */
highroads 0:47f8714fe967 39
highroads 0:47f8714fe967 40 #define VL6180X_ADDRESS 0x29
highroads 0:47f8714fe967 41
highroads 0:47f8714fe967 42 VL6180xIdentification identification;
highroads 0:47f8714fe967 43 // mbed uses 8bit addresses shift address by 1 bit left
highroads 0:47f8714fe967 44 VL6180x sensor(D14, D15, VL6180X_ADDRESS<<1);
highroads 0:47f8714fe967 45
highroads 0:47f8714fe967 46 void printIdentification(struct VL6180xIdentification *temp){
highroads 0:47f8714fe967 47 printf("Model ID = ");
highroads 0:47f8714fe967 48 printf("%d\n",temp->idModel);
highroads 0:47f8714fe967 49
highroads 0:47f8714fe967 50 printf("Model Rev = ");
highroads 0:47f8714fe967 51 printf("%d",temp->idModelRevMajor);
highroads 0:47f8714fe967 52 printf(".");
highroads 0:47f8714fe967 53 printf("%d\n",temp->idModelRevMinor);
highroads 0:47f8714fe967 54
highroads 0:47f8714fe967 55 printf("Module Rev = ");
highroads 0:47f8714fe967 56 printf("%d",temp->idModuleRevMajor);
highroads 0:47f8714fe967 57 printf(".");
highroads 0:47f8714fe967 58 printf("%d\n",temp->idModuleRevMinor);
highroads 0:47f8714fe967 59
highroads 0:47f8714fe967 60 printf("Manufacture Date = ");
highroads 0:47f8714fe967 61 printf("%d",((temp->idDate >> 3) & 0x001F));
highroads 0:47f8714fe967 62 printf("/");
highroads 0:47f8714fe967 63 printf("%d",((temp->idDate >> 8) & 0x000F));
highroads 0:47f8714fe967 64 printf("/1");
highroads 0:47f8714fe967 65 printf("%d\n",((temp->idDate >> 12) & 0x000F));
highroads 0:47f8714fe967 66 printf(" Phase: ");
highroads 0:47f8714fe967 67 printf("%d\n",(temp->idDate & 0x0007));
highroads 0:47f8714fe967 68
highroads 0:47f8714fe967 69 printf("Manufacture Time (s)= ");
highroads 0:47f8714fe967 70 printf("%d\n",(temp->idTime * 2));
highroads 0:47f8714fe967 71 printf("\n\n");
highroads 0:47f8714fe967 72 }
highroads 0:47f8714fe967 73 int main() {
highroads 0:47f8714fe967 74 // Serial.begin(115200); //Start Serial at 115200bps
highroads 0:47f8714fe967 75
highroads 0:47f8714fe967 76 wait_ms(100); // delay .1s
highroads 0:47f8714fe967 77
highroads 0:47f8714fe967 78 sensor.getIdentification(&identification); // Retrieve manufacture info from device memory
highroads 0:47f8714fe967 79 printIdentification(&identification); // Helper function to print all the Module information
highroads 0:47f8714fe967 80
highroads 0:47f8714fe967 81 if(sensor.VL6180xInit() != 0){
highroads 0:47f8714fe967 82 printf("FAILED TO INITALIZE\n"); //Initialize device and check for errors
highroads 0:47f8714fe967 83 };
highroads 0:47f8714fe967 84
highroads 0:47f8714fe967 85 sensor.VL6180xDefautSettings(); //Load default settings to get started.
highroads 0:47f8714fe967 86
highroads 0:47f8714fe967 87 wait_ms(1000); // delay 1s
highroads 0:47f8714fe967 88
highroads 0:47f8714fe967 89
highroads 0:47f8714fe967 90
highroads 0:47f8714fe967 91 while(1) {
highroads 0:47f8714fe967 92 //Get Ambient Light level and report in LUX
highroads 0:47f8714fe967 93 printf("Ambient Light Level (Lux) = ");
highroads 0:47f8714fe967 94
highroads 0:47f8714fe967 95 //Input GAIN for light levels,
highroads 0:47f8714fe967 96 // GAIN_20 // Actual ALS Gain of 20
highroads 0:47f8714fe967 97 // GAIN_10 // Actual ALS Gain of 10.32
highroads 0:47f8714fe967 98 // GAIN_5 // Actual ALS Gain of 5.21
highroads 0:47f8714fe967 99 // GAIN_2_5 // Actual ALS Gain of 2.60
highroads 0:47f8714fe967 100 // GAIN_1_67 // Actual ALS Gain of 1.72
highroads 0:47f8714fe967 101 // GAIN_1_25 // Actual ALS Gain of 1.28
highroads 0:47f8714fe967 102 // GAIN_1 // Actual ALS Gain of 1.01
highroads 0:47f8714fe967 103 // GAIN_40 // Actual ALS Gain of 40
highroads 0:47f8714fe967 104
highroads 0:47f8714fe967 105 printf("%f\n",sensor.getAmbientLight(GAIN_1) );
highroads 0:47f8714fe967 106
highroads 0:47f8714fe967 107 //Get Distance and report in mm
highroads 0:47f8714fe967 108 printf("Distance measured (mm) = ");
highroads 0:47f8714fe967 109 printf("%d\n", sensor.getDistance() );
highroads 0:47f8714fe967 110
highroads 0:47f8714fe967 111 wait_ms(500);
highroads 0:47f8714fe967 112 }
highroads 0:47f8714fe967 113 }
highroads 0:47f8714fe967 114
highroads 0:47f8714fe967 115
highroads 0:47f8714fe967 116
highroads 0:47f8714fe967 117
highroads 0:47f8714fe967 118