-
Notifications
You must be signed in to change notification settings - Fork 1
/
DimmableKitchenLightGesture.ino
executable file
·136 lines (116 loc) · 3.55 KB
/
DimmableKitchenLightGesture.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
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
#include <SparkFun_APDS9960.h>
// Pins
#define GREEN_OUTPUT 10
#define BLUE_OUTPUT 11
#define RED_OUTPUT 5
#define WW_OUTPUT 6
// Constants
#define MAX_DISTANCE 20
#define MIN_LEVEL 35
#define GREEN_BASE_LEVEL 50
#define BLUE_BASE_LEVEL 6
#define RED_BASE_LEVEL 255
#define WW_BASE_LEVEL 255
#define LOCK_TIME 5
#define LOCK_THRESHOLD 3
// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
unsigned long last_lock_time = 0;
byte last_lock_level = 0;
void setup() {
// Initialize Serial port
Serial.begin(9600);
Serial.println();
Serial.println(F("------------------------------------"));
Serial.println(F("SparkFun APDS-9960 - ProximitySensor"));
Serial.println(F("------------------------------------"));
// 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!"));
}
// Adjust the Proximity sensor gain
if ( !apds.setProximityGain(PGAIN_2X) ) {
Serial.println(F("Something went wrong trying to set PGAIN"));
}
// Start running the APDS-9960 proximity sensor (no interrupts)
if ( apds.enableProximitySensor(false) ) {
Serial.println(F("Proximity sensor is now running"));
} else {
Serial.println(F("Something went wrong during sensor init!"));
}
pinMode(GREEN_OUTPUT, OUTPUT);
pinMode(BLUE_OUTPUT, OUTPUT);
pinMode(RED_OUTPUT, OUTPUT);
pinMode(WW_OUTPUT, OUTPUT);
set_light_level(255); // Start with very bright but not max since max is not correct temperature
}
void loop() {
uint8_t proximity_data = 0;
if ( !apds.readProximity(proximity_data) ) {
Serial.println("Error reading proximity value");
} else {
if (proximity_data > MAX_DISTANCE) {
Serial.print("Proximity: ");
Serial.println(proximity_data);
set_light_level(proximity_data);
}
}
}
void set_light_level (byte level) {
if (level < MIN_LEVEL) {
turn_off();
last_lock_level = level;
last_lock_time = millis();
} else {
if (abs(last_lock_level - level) < LOCK_THRESHOLD) {
if (millis() - last_lock_time > LOCK_TIME) {
// Level has been held steady for a while. Blink to acknowledge and sleep to allow time to remove hand from sensor.
// turn_off();
// delay(150);
adjust(level); // Set level back to selected level
// last_lock_level = 0;
// last_lock_time = 0;
// delay(LOCK_TIME * 3);
} else {
adjust(level);
}
} else {
// Level was significantly changed.
adjust(level);
last_lock_level = level;
last_lock_time = millis();
}
}
}
void turn_off() {
analogWrite(GREEN_OUTPUT, 0);
analogWrite(BLUE_OUTPUT, 0);
analogWrite(RED_OUTPUT, 0);
analogWrite(WW_OUTPUT, 0);
}
void adjust(byte level) {
Serial.print("level:");
Serial.print(level);
Serial.print(" adj:");
if (level > 135) {
level = 255;
} else {
level = 25;
}
float adjustment = level / 255.0;
Serial.print(adjustment);
analogWrite(GREEN_OUTPUT, max(1, GREEN_BASE_LEVEL * adjustment));
Serial.print(" green:");
Serial.print(GREEN_BASE_LEVEL * adjustment);
analogWrite(BLUE_OUTPUT, max(1, BLUE_BASE_LEVEL * adjustment));
Serial.print(" blue:");
Serial.print(BLUE_BASE_LEVEL * adjustment);
analogWrite(RED_OUTPUT, max(1, RED_BASE_LEVEL * adjustment));
Serial.print(" red:");
Serial.print(RED_BASE_LEVEL * adjustment);
analogWrite(WW_OUTPUT, max(1, WW_BASE_LEVEL * adjustment));
Serial.print(" ww:");
Serial.println(WW_BASE_LEVEL * adjustment);
}