This program shows how to use the VCNL4010 (proximity and ambient light sensor) on the QW dev kit for detecting an open closet door. When the closet is open, a message will be displayed in the console window and a Sigfox message will be send.
Fork of QW-Closet-detection by
QW Closet Detection
This program shows how to use the VCNL4010 (proximity and ambient light sensor) on the QW dev kit for detecting an open closet door. When the closet is open, a message will be displayed in the console window and a Sigfox message will be send.
Code explanation
The program starts with the initialisation/declaration of the leds, pushbuttons and sensor. Also the necessary function prototypes and serial communications are declared. After the initialisation phase, the sensor ID is read. After that, the program is waiting for a push on one of the buttons to calibrate the "device". Once calibrated, the program enters an infinite loop. Each loop the ambient light value is read. When a certain threshold is passed, a led is turned on to indicate an open closet, a message is shown in the console window and a Sigfox message is transmitted. When the Sigfox message is transmitted, the loop continuous.
Sigfox message payload
First there is the "05", this is the Quicksand ID of the example program. This is used by Quicksand to keep track of our example programs. The second value that is transmitted is the ambient light value the moment when an open closet was detected.
More information and other example code can be found on the component page by clicking the link below: https://developer.mbed.org/components/QW-SIGFOX-Development-Kit/
main.cpp@0:6c17d1a79f75, 2015-11-05 (annotated)
- Committer:
- quicksand
- Date:
- Thu Nov 05 09:40:10 2015 +0000
- Revision:
- 0:6c17d1a79f75
- Child:
- 1:95286e4370e7
This program demonstrates the ambient light sensing functionality of the VCNL4010 on the QW Shields.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
quicksand | 0:6c17d1a79f75 | 1 | #include "mbed.h" |
quicksand | 0:6c17d1a79f75 | 2 | #include "VCNL4010.h" |
quicksand | 0:6c17d1a79f75 | 3 | |
quicksand | 0:6c17d1a79f75 | 4 | DigitalOut LED_0 (PB_6); |
quicksand | 0:6c17d1a79f75 | 5 | DigitalOut LED_1 (PA_7); |
quicksand | 0:6c17d1a79f75 | 6 | DigitalOut LED_2 (PA_6); |
quicksand | 0:6c17d1a79f75 | 7 | DigitalOut LED_3 (PA_5); |
quicksand | 0:6c17d1a79f75 | 8 | |
quicksand | 0:6c17d1a79f75 | 9 | //Virtual serial port over USB |
quicksand | 0:6c17d1a79f75 | 10 | Serial pc(USBTX, USBRX); |
quicksand | 0:6c17d1a79f75 | 11 | Serial modem(PA_9, PA_10); |
quicksand | 0:6c17d1a79f75 | 12 | |
quicksand | 0:6c17d1a79f75 | 13 | // configure VCNL4010 |
quicksand | 0:6c17d1a79f75 | 14 | VCNL40x0 VCNL40x0_Device (PB_9, PB_8, VCNL40x0_ADDRESS); // Define SDA, SCL pin and I2C address |
quicksand | 0:6c17d1a79f75 | 15 | |
quicksand | 0:6c17d1a79f75 | 16 | |
quicksand | 0:6c17d1a79f75 | 17 | int main() { |
quicksand | 0:6c17d1a79f75 | 18 | |
quicksand | 0:6c17d1a79f75 | 19 | LED_0 = 1; |
quicksand | 0:6c17d1a79f75 | 20 | LED_1 = 1; |
quicksand | 0:6c17d1a79f75 | 21 | LED_2 = 1; |
quicksand | 0:6c17d1a79f75 | 22 | LED_3 = 1; |
quicksand | 0:6c17d1a79f75 | 23 | |
quicksand | 0:6c17d1a79f75 | 24 | unsigned char ID=0; |
quicksand | 0:6c17d1a79f75 | 25 | unsigned int AmbiValue=0; |
quicksand | 0:6c17d1a79f75 | 26 | |
quicksand | 0:6c17d1a79f75 | 27 | // print information on screen |
quicksand | 0:6c17d1a79f75 | 28 | pc.printf("\n\n VCNL4010 Proximity/Ambient Light Sensor"); |
quicksand | 0:6c17d1a79f75 | 29 | pc.printf("\n Read Ambillight on demand in endless loop"); |
quicksand | 0:6c17d1a79f75 | 30 | |
quicksand | 0:6c17d1a79f75 | 31 | VCNL40x0_Device.ReadID (&ID); // Read VCNL40x0 product ID revision register |
quicksand | 0:6c17d1a79f75 | 32 | pc.printf("\n\n Product ID Revision Register: %d", ID); |
quicksand | 0:6c17d1a79f75 | 33 | |
quicksand | 0:6c17d1a79f75 | 34 | wait_ms(3000); // wait 3s (only for display) |
quicksand | 0:6c17d1a79f75 | 35 | |
quicksand | 0:6c17d1a79f75 | 36 | while(1) { |
quicksand | 0:6c17d1a79f75 | 37 | VCNL40x0_Device.ReadAmbiOnDemand (&AmbiValue); // read ambi value on demand |
quicksand | 0:6c17d1a79f75 | 38 | pc.printf("Ambient light: %5.0i cts \tIlluminance: %7.2f lx\r", AmbiValue, AmbiValue/4.0); |
quicksand | 0:6c17d1a79f75 | 39 | if(AmbiValue < 5000) LED_3 = 0; |
quicksand | 0:6c17d1a79f75 | 40 | else LED_3 = 1; |
quicksand | 0:6c17d1a79f75 | 41 | if(AmbiValue < 4000) LED_2 = 0; |
quicksand | 0:6c17d1a79f75 | 42 | else LED_2 = 1; |
quicksand | 0:6c17d1a79f75 | 43 | if(AmbiValue < 3000) LED_1 = 0; |
quicksand | 0:6c17d1a79f75 | 44 | else LED_1 = 1; |
quicksand | 0:6c17d1a79f75 | 45 | if(AmbiValue < 2000) LED_0 = 0; |
quicksand | 0:6c17d1a79f75 | 46 | else LED_0 = 1; |
quicksand | 0:6c17d1a79f75 | 47 | } |
quicksand | 0:6c17d1a79f75 | 48 | } |