-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathAmbientLightInterrupt.ino
More file actions
169 lines (136 loc) · 4.93 KB
/
AmbientLightInterrupt.ino
File metadata and controls
169 lines (136 loc) · 4.93 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/****************************************************************
AmbientLightInterrupt.ino
APDS-9960 RGB and Gesture Sensor
Shawn Hymel @ SparkFun Electronics
October 24, 2014
https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor
Tests the ambient light interrupt abilities of the APDS-9960.
Configures the APDS-9960 over I2C and waits for an external
interrupt based on high or low light conditions. Try covering
the sensor with your hand or bringing the sensor close to a
bright light source. You might need to adjust the LIGHT_INT_HIGH
and LIGHT_INT_LOW values to get the interrupt to work correctly.
Hardware Connections:
IMPORTANT: The APDS-9960 can only accept 3.3V!
Wemos Pin APDS-9960 Board Function
3.3V VCC Power
GND GND Ground
D3 SDA I2C Data
D1 SCL I2C Clock
D6 INT Interrupt
D7 - LED
Resources:
Include Wire.h and SparkFun_APDS-9960.h
Development environment specifics:
Written in Arduino 1.0.5
Tested with SparkFun Arduino Pro Mini 3.3V
This code is beerware; if you see me (or any other SparkFun
employee) at the local, and you've found our code helpful, please
buy us a round!
Distributed as-is; no warranty is given.
Modified for ESP8266 by Jon Ulmer Nov 2016
****************************************************************/
#include <Wire.h>
#include <SparkFun_APDS9960.h>
// Pins on wemos D1 mini
#define APDS9960_INT D6 //AKA GPIO12 -- Interupt pin
#define APDS9960_SDA D3 //AKA GPIO0
#define APDS9960_SCL D1 //AKA GPIO5
#define LED_PIN D7 //AKA GPIO13 -- LED for showing interrupt
// Constants
#define LIGHT_INT_HIGH 1000 // High light level for interrupt
#define LIGHT_INT_LOW 10 // Low light level for interrupt
// Global variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
uint16_t ambient_light = 0;
uint16_t red_light = 0;
uint16_t green_light = 0;
uint16_t blue_light = 0;
volatile bool isr_flag = 0;
uint16_t threshold = 0;
void setup() {
//Start I2C with pins defined above
Wire.begin(APDS9960_SDA,APDS9960_SCL);
// Set LED as output
pinMode(LED_PIN, OUTPUT);
pinMode(APDS9960_INT, INPUT);
// Initialize Serial port
Serial.begin(115200);
Serial.println();
Serial.println(F("-------------------------------------"));
Serial.println(F("SparkFun APDS-9960 - Light Interrupts"));
Serial.println(F("-------------------------------------"));
// Initialize interrupt service routine
attachInterrupt(APDS9960_INT, interruptRoutine, FALLING);
// Initialize APDS-9960 (configure I2C and initial values)
if ( apds.init() ) {
Serial.println(F("APDS-9960 initialization complete"));
} else {
Serial.println(F("Something went wrong during APDS-9960 init!"));
}
// Set high and low interrupt thresholds
if ( !apds.setLightIntLowThreshold(LIGHT_INT_LOW) ) {
Serial.println(F("Error writing low threshold"));
}
if ( !apds.setLightIntHighThreshold(LIGHT_INT_HIGH) ) {
Serial.println(F("Error writing high threshold"));
}
// Start running the APDS-9960 light sensor (no interrupts)
if ( apds.enableLightSensor(false) ) {
Serial.println(F("Light sensor is now running"));
} else {
Serial.println(F("Something went wrong during light sensor init!"));
}
// Read high and low interrupt thresholds
if ( !apds.getLightIntLowThreshold(threshold) ) {
Serial.println(F("Error reading low threshold"));
} else {
Serial.print(F("Low Threshold: "));
Serial.println(threshold);
}
if ( !apds.getLightIntHighThreshold(threshold) ) {
Serial.println(F("Error reading high threshold"));
} else {
Serial.print(F("High Threshold: "));
Serial.println(threshold);
}
// Enable interrupts
if ( !apds.setAmbientLightIntEnable(1) ) {
Serial.println(F("Error enabling interrupts"));
}
// Wait for initialization and calibration to finish
delay(500);
}
void loop() {
// If interrupt occurs, print out the light levels
if ( isr_flag == 1 ) {
// Read the light levels (ambient, red, green, blue) and print
if ( !apds.readAmbientLight(ambient_light) ||
!apds.readRedLight(red_light) ||
!apds.readGreenLight(green_light) ||
!apds.readBlueLight(blue_light) ) {
Serial.println("Error reading light values");
} else {
Serial.print("Interrupt! Ambient: ");
Serial.print(ambient_light);
Serial.print(" R: ");
Serial.print(red_light);
Serial.print(" G: ");
Serial.print(green_light);
Serial.print(" B: ");
Serial.println(blue_light);
}
// Turn on LED for a half a second
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
// Reset flag and clear APDS-9960 interrupt (IMPORTANT!)
isr_flag = 0;
if ( !apds.clearAmbientLightInt() ) {
Serial.println("Error clearing interrupt");
}
}
}
void interruptRoutine() {
isr_flag = 1;
}