Skip to content

Commit 03ea85f

Browse files
committed
feat(hexalamp): add button trigger
1 parent 2534639 commit 03ea85f

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

hexalamp/hexalamp.ino

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#define DEBUG
1+
//#define DEBUG
22
#define ON_PIN 3
3+
#define BT_PIN 5
34
#define IR_PIN A6
45
#define PR_PIN A4
56
#define IR_TRH 900
@@ -12,42 +13,84 @@ void setup() {
1213
pinMode(ON_PIN, OUTPUT);
1314
pinMode(IR_PIN, INPUT);
1415
pinMode(PR_PIN, INPUT);
16+
pinMode(BT_PIN, INPUT_PULLUP);
1517

1618
#ifdef DEBUG
1719
Serial.begin(9600);
1820
#endif
1921
}
2022

23+
bool trig = false;
2124
bool on = false;
25+
bool bt = false;
2226
int ir, pr;
2327
unsigned long t = 0;
28+
char _src = '?';
29+
30+
bool irTrig(int ir, int pr, bool on) {
31+
if ((!on and ir < IR_TRH and pr < PR_TRH) or (on and ir < IR_TRH)) {
32+
if (on) {
33+
_src = '?';
34+
} else {
35+
_src = 'i';
36+
}
37+
38+
return true;
39+
}
40+
41+
return false;
42+
}
43+
44+
bool btPush(bool bt, bool on) {
45+
if (bt) {
46+
if (on) {
47+
_src = '?';
48+
} else {
49+
_src = 'b';
50+
}
51+
52+
return true;
53+
}
54+
55+
return false;
56+
}
2457

2558
void loop() {
2659
ir = analogRead(IR_PIN);
2760
pr = analogRead(PR_PIN);
61+
bt = digitalRead(BT_PIN) == LOW;
62+
63+
trig = (irTrig(ir, pr, on) or btPush(bt, on));
2864

2965
#ifdef DEBUG
3066
Serial.print("IR: ");
3167
Serial.print(ir);
3268
Serial.print(" / PR: ");
33-
Serial.println(pr);
69+
Serial.print(pr);
70+
Serial.print(" / BT: ");
71+
Serial.print(bt);
72+
Serial.print(" / SRC: ");
73+
Serial.println(_src);
3474
#endif
3575

36-
if ((!on and ir < IR_TRH and pr < PR_TRH) or (on and ir < IR_TRH)) {
76+
if (trig) {
3777
on = !on;
3878

3979
digitalWrite(ON_PIN, on);
40-
delay(5*graceMillis);
80+
delay(5 * graceMillis);
4181

4282
if (on) {
4383
t = millis();
4484
}
4585
}
4686

47-
if (on) {
48-
if (millis() - t > lightMillis) {
49-
digitalWrite(ON_PIN, !on);
50-
t = millis();
87+
if (_src == 'i') {
88+
if (on) {
89+
if (millis() - t > lightMillis) {
90+
digitalWrite(ON_PIN, !on);
91+
t = millis();
92+
_src = '?';
93+
}
5194
}
5295
}
5396

0 commit comments

Comments
 (0)