1 | #include <Wire.h>
|
2 | #include <SparkFun_APDS9960.h>
|
3 |
|
4 | // Pins on wemos D1 mini
|
5 | #define APDS9960_INT D6 //AKA GPIO12 -- Interupt pin
|
6 | #define APDS9960_SDA D3 //AKA GPIO0
|
7 | #define APDS9960_SCL D1 //AKA GPIO5
|
8 | #define LED_PIN D7 //AKA GPIO13 -- LED for showing interrupt
|
9 |
|
10 | // Constants
|
11 | #define PROX_INT_HIGH 255 // Proximity level for interrupt
|
12 | #define PROX_INT_LOW 0 // No far interrupt
|
13 |
|
14 | // Global variables
|
15 | SparkFun_APDS9960 apds = SparkFun_APDS9960();
|
16 | uint8_t proximity_data = 0;
|
17 | volatile bool isr_flag = 0;
|
18 |
|
19 | void setup() {
|
20 |
|
21 | //Start I2C with pins defined above
|
22 | Wire.begin(APDS9960_SDA,APDS9960_SCL);
|
23 |
|
24 | // Set LED as output
|
25 | pinMode(LED_PIN, OUTPUT);
|
26 | pinMode(APDS9960_INT, INPUT);
|
27 |
|
28 | // Initialize Serial port
|
29 | Serial.begin(115200);
|
30 | Serial.println();
|
31 | Serial.println(F("---------------------------------------"));
|
32 | Serial.println(F("SparkFun APDS-9960 - ProximityInterrupt"));
|
33 | Serial.println(F("---------------------------------------"));
|
34 |
|
35 | // Initialize interrupt service routine
|
36 | attachInterrupt(APDS9960_INT, interruptRoutine, FALLING);
|
37 |
|
38 | // Initialize APDS-9960 (configure I2C and initial values)
|
39 | if ( apds.init() ) {
|
40 | Serial.println(F("APDS-9960 initialization complete"));
|
41 | } else {
|
42 | Serial.println(F("Something went wrong during APDS-9960 init!"));
|
43 | }
|
44 |
|
45 | // Adjust the Proximity sensor gain
|
46 | if ( !apds.setProximityGain(PGAIN_8X) ) {
|
47 | Serial.println(F("Something went wrong trying to set PGAIN"));
|
48 | }
|
49 |
|
50 | // Set proximity interrupt thresholds
|
51 | if ( !apds.setProximityIntLowThreshold(PROX_INT_LOW) ) {
|
52 | Serial.println(F("Error writing low threshold"));
|
53 | }
|
54 | if ( !apds.setProximityIntHighThreshold(PROX_INT_HIGH) ) {
|
55 | Serial.println(F("Error writing high threshold"));
|
56 | }
|
57 |
|
58 | // Start running the APDS-9960 proximity sensor (interrupts)
|
59 | if ( apds.enableProximitySensor(true) ) {
|
60 | Serial.println(F("Proximity sensor is now running"));
|
61 | } else {
|
62 | Serial.println(F("Something went wrong during sensor init!"));
|
63 | }
|
64 | }
|
65 |
|
66 | void loop() {
|
67 |
|
68 | // If interrupt occurs, print out the proximity level
|
69 | if ( isr_flag == 1 ) {
|
70 |
|
71 | // Read proximity level and print it out
|
72 | if ( !apds.readProximity(proximity_data) ) {
|
73 | Serial.println("Error reading proximity value");
|
74 | } else {
|
75 | Serial.print("Proximity detected! Level: ");
|
76 | Serial.println(proximity_data);
|
77 | }
|
78 |
|
79 | // Turn on LED for a half a second
|
80 | digitalWrite(LED_PIN, HIGH);
|
81 | delay(500);
|
82 | digitalWrite(LED_PIN, LOW);
|
83 |
|
84 | // Reset flag and clear APDS-9960 interrupt (IMPORTANT!)
|
85 | isr_flag = 0;
|
86 | if ( !apds.clearProximityInt() ) {
|
87 | Serial.println("Error clearing interrupt");
|
88 | }
|
89 |
|
90 | }
|
91 | }
|
92 |
|
93 | ICACHE_RAM_ATTR void interruptRoutine() {
|
94 | isr_flag = 1;
|
95 | //Serial.println("Interrupt");
|
96 | }
|