|
| 1 | +// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +#include "Adafruit_VCNL4200.h" |
| 6 | + |
| 7 | +Adafruit_VCNL4200 vcnl4200; |
| 8 | + |
| 9 | +void setup() { |
| 10 | + Serial.begin(115200); |
| 11 | + while (!Serial) { |
| 12 | + delay(10); // wait for native USB |
| 13 | + } |
| 14 | + |
| 15 | + Serial.println("Adafruit VCNL4200 ALS simple test"); |
| 16 | + |
| 17 | + if (!vcnl4200.begin()) { |
| 18 | + Serial.println("Could not find a valid VCNL4200 sensor, check wiring!"); |
| 19 | + while (1) { |
| 20 | + delay(10); |
| 21 | + } |
| 22 | + } |
| 23 | + Serial.println("VCNL4200 found!"); |
| 24 | + |
| 25 | + vcnl4200.setALSshutdown(false); |
| 26 | + vcnl4200.setALSIntegrationTime(VCNL4200_ALS_IT_100MS); |
| 27 | + vcnl4200.setALSPersistence(VCNL4200_ALS_PERS_2); |
| 28 | + vcnl4200.setALSthresholdLow(100); |
| 29 | + vcnl4200.setALSthresholdHigh(20000); |
| 30 | + vcnl4200.setInterrupt(true, false); // activate int on ALS chan |
| 31 | + |
| 32 | + vcnl4200.setProxShutdown(false); |
| 33 | + vcnl4200.setProxHD(false); |
| 34 | + vcnl4200.setProxLEDCurrent(VCNL4200_LED_I_200MA); |
| 35 | + vcnl4200.setProxIntegrationTime(VCNL4200_PS_IT_8T); |
| 36 | +} |
| 37 | + |
| 38 | +void loop() { |
| 39 | + uint16_t proxData = vcnl4200.readProxData(); |
| 40 | + Serial.print("Prox Data: "); |
| 41 | + Serial.println(proxData); |
| 42 | + // Read the ambient light sensor (ALS) data |
| 43 | + uint16_t alsData = vcnl4200.readALSdata(); |
| 44 | + Serial.print("ALS Data: "); |
| 45 | + Serial.print(alsData); |
| 46 | + uint16_t whiteData = vcnl4200.readWhiteData(); |
| 47 | + Serial.print(", White Data: "); |
| 48 | + Serial.println(whiteData); |
| 49 | + |
| 50 | + uint8_t flags = vcnl4200.getInterruptFlags(); |
| 51 | + if (flags != 0 && flags != 0xFF) { |
| 52 | + Serial.print("Interrupt flags: 0x"); |
| 53 | + Serial.println(flags, HEX); |
| 54 | + |
| 55 | + if (flags & VCNL4200_INTFLAG_ALS_HIGH) { |
| 56 | + Serial.println("ALS high threshold interrupt triggered."); |
| 57 | + } |
| 58 | + if (flags & VCNL4200_INTFLAG_ALS_LOW) { |
| 59 | + Serial.println("ALS low threshold interrupt triggered."); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + delay(100); |
| 64 | +} |
0 commit comments