Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include "VCNL40x0.h"
- #define VERSION "Version: 1.2 01/2012"
- #define unsigned int i=0;
- #define unsigned char ID=0;
- #define unsigned char Command=0;
- #define unsigned char Current=0;
- #define unsigned int ProxiValue=0;
- #define unsigned int SummeProxiValue=0;
- #define unsigned int AverageProxiValue=0;
- #define unsigned int AmbiValue=0;
- #define unsigned char InterruptStatus=0;
- #define unsigned char InterruptControl=0;
- VCNL40x0 VCNL40x0_Device();
- void setup() {
- Serial.begin(9600);
- // print information on screen
- Serial.println("VCNL4010/4020/3020 Proximity/Ambient Light Sensor");
- Serial.println(VERSION);
- Serial.println("Demonstration #4:");
- Serial.println("Proximity Measurement and Ambient light Measurement in selftimed mode");
- Serial.println("Proximity with 31 measurements/s, Ambient light with 2 measurement/s");
- Serial.println("Interrupt waiting on proximity value > upper threshold limit");
- VCNL40x0_Device.ReadID (&ID); // Read VCNL40x0 product ID revision register
- Serial.println("Product ID Revision Register: %d", ID);
- VCNL40x0_Device.SetCurrent (20); // Set current to 200mA
- VCNL40x0_Device.ReadCurrent (&Current); // Read back IR LED current
- Serial.println("\n IR LED Current: %d", Current);
- // stop all activities (necessary for changing proximity rate, see datasheet)
- VCNL40x0_Device.SetCommandRegister (COMMAND_ALL_DISABLE);
- // set proximity rate to 31/s
- VCNL40x0_Device.SetProximityRate (PROX_MEASUREMENT_RATE_31);
- // enable prox and ambi in selftimed mode
- VCNL40x0_Device.SetCommandRegister (COMMAND_PROX_ENABLE |
- COMMAND_AMBI_ENABLE |
- COMMAND_SELFTIMED_MODE_ENABLE);
- // set interrupt control for threshold
- VCNL40x0_Device.SetInterruptControl (INTERRUPT_THRES_SEL_PROX |
- INTERRUPT_THRES_ENABLE |
- INTERRUPT_COUNT_EXCEED_1);
- // set ambient light measurement parameter
- VCNL40x0_Device.SetAmbiConfiguration (AMBI_PARA_AVERAGE_32 |
- AMBI_PARA_AUTO_OFFSET_ENABLE |
- AMBI_PARA_MEAS_RATE_2);
- // measure average of prox value
- SummeProxiValue = 0;
- for (i=0; i<30; i++) {
- do { // wait on prox data ready bit
- VCNL40x0_Device.ReadCommandRegister (&Command); // read command register
- } while (!(Command & COMMAND_MASK_PROX_DATA_READY)); // prox data ready ?
- VCNL40x0_Device.ReadProxiValue (&ProxiValue); // read prox value
- SummeProxiValue += ProxiValue; // Summary of all measured prox values
- }
- AverageProxiValue = SummeProxiValue/30; // calculate average
- VCNL40x0_Device.SetHighThreshold (AverageProxiValue+100); // set upper threshold for interrupt
- Serial.println("Upper Threshold Value: %i cts\n\n", AverageProxiValue+100);
- delay(2000); // wait 2s (only for display)
- }
- void loop() {
- // wait on data ready bit
- do {
- VCNL40x0_Device.ReadCommandRegister (&Command); // read command register
- } while (!(Command & (COMMAND_MASK_PROX_DATA_READY | COMMAND_MASK_AMBI_DATA_READY))); // data ready ?
- // read interrupt status register
- VCNL40x0_Device.ReadInterruptStatus (&InterruptStatus);
- // check interrupt status for High Threshold
- if (InterruptStatus & INTERRUPT_MASK_STATUS_THRES_HI) {
- //mled2 = 1; // LED on, Interrupt
- VCNL40x0_Device.SetInterruptStatus (InterruptStatus); // clear Interrupt Status
- //mled2 = 0; // LED off, Interrupt
- }
- // prox value ready for using
- if (Command & COMMAND_MASK_PROX_DATA_READY) {
- //mled0 = 1; // LED on, Prox Data Ready
- VCNL40x0_Device.ReadProxiValue (&ProxiValue); // read prox value
- // print prox value and interrupt status on screen
- Serial.println("\nProxi: %5.0i cts \tInterruptStatus: %i", ProxiValue, InterruptStatus);
- //mled0 = 0; // LED off, Prox data Ready
- }
- // ambi value ready for using
- if (Command & COMMAND_MASK_AMBI_DATA_READY) {
- //mled1 = 1; // LED on, Ambi Data Ready
- VCNL40x0_Device.ReadAmbiValue (&AmbiValue); // read ambi value
- // print ambi value and interrupt status on screen
- Serial.println("\n Ambi: %i", AmbiValue);
- //mled1 = 0; // LED off, Ambi Data Ready
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement