-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathInterruptProximity.ino
68 lines (49 loc) · 1.64 KB
/
InterruptProximity.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
APDS9960 - Proximity Sensor, Interrupt
This example reads proximity data from the on-board APDS9960 sensor of the
Nano 33 BLE Sense and prints the proximity value to the Serial Monitor. If
the measure is outside the interrupt thresholds it will trigger an interrupt.
The circuit:
- Arduino Nano 33 BLE Sense
This example code is in the public domain.
*/
#include <Arduino_APDS9960.h>
int interrupt_pin = 2; // pin connected to the Interrupt pin from the sensor
// Thresholds to trigger the interrupt
unsigned int lower_threshold = 0;
unsigned int higher_threshold = 150;
// Flag to know if the interrupt has been triggered
int interrupt_flag = 0;
void setup() {
Serial.begin(9600);
while (!Serial);
// Attach interrupt
attachInterrupt(interrupt_pin, assert_flag, FALLING);
if (!APDS.begin()) {
Serial.println("Error initializing APDS9960 sensor!");
}
//Enable the proximity Interrupt generation on the Interrupt pin
APDS.enableProximityInterrupt();
// Config the Thresholds
APDS.setProximityLowThreshold(lower_threshold);
APDS.setProximityHighThreshold(higher_threshold);
}
void loop() {
// Output the proximity
if (APDS.proximityAvailable()) {
Serial.println(APDS.readProximity());
}
// If the interrupt flag has been asserted, clear the interrupt of the sensor
if ( interrupt_flag == 1 ) {
Serial.println("Flagged!");
delay(1000);
interrupt_flag = 0;
APDS.clearProximityInterrupt();
Serial.println("Flag and interrupt clear");
delay(500);
}
delay(100);
}
void assert_flag() {
interrupt_flag = 1;
}