-
Notifications
You must be signed in to change notification settings - Fork 0
/
Putris_Immoderata_tree.ino
129 lines (115 loc) · 4.42 KB
/
Putris_Immoderata_tree.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
/* Putris Immoderata - Dual Key Switch
* Idea Fab Labs - Chico
* Tree
*/
#include <WS2812FX.h>
#define LED_COUNT 31 // tree
#define LED_PIN 6 // NeoPixel Pin
#define SW1_PIN 8 // 'Key Inserted' Button
#define SW2_PIN 9 // Key Switch One (putris)
#define SW3_PIN 10 // Key Switch Two (vivus)
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_RGB + NEO_KHZ800);
int sw1State = HIGH; // current state of sw1
int sw1lastState = HIGH; // previous state of sw1
int sw2State = HIGH; // current state of sw2
int sw2lastState = HIGH; // previous state of sw2
int sw3State = HIGH; // current state of sw3
int sw3lastState = HIGH; // previous state of sw3
unsigned long tmrBounce = 0;
const unsigned long BOUNCE_INTERVAL = 10;
bool Running = false;
unsigned long tmrTimeout = 0;
const unsigned long TIMEOUT_INTERVAL = 10000;
void setup() {
Serial.begin(115200);
Serial.println("");
Serial.println("Start ...");
pinMode(SW1_PIN, INPUT_PULLUP);
pinMode(SW2_PIN, INPUT_PULLUP);
pinMode(SW3_PIN, INPUT_PULLUP);
ws2812fx.init();
ws2812fx.setBrightness(255);
ws2812fx.setSegment(0, 0, 0, FX_MODE_FADE, RED, 5000, NO_OPTIONS); // segment 0 is leds 0 - 0
ws2812fx.setIdleSegment(1, 0, 0, FX_MODE_STATIC, WHITE, 5000, NO_OPTIONS); // segment 0 is leds 0 - 0
ws2812fx.setSegment(2, 1, 31, FX_MODE_FADE, WHITE, 5000, REVERSE); // segment 1 is leds 1 - 31
ws2812fx.setIdleSegment(3, 1, 31, FX_MODE_BREATH, COLORS(DIM(GREEN), BLUE), 1000, NO_OPTIONS); // segment 1 is leds 1 - 31
ws2812fx.setIdleSegment(4, 1, 31, FX_MODE_COMET, COLORS(RED, ORANGE, YELLOW), 1200, NO_OPTIONS); // segment 1 is leds 1 - 31
ws2812fx.start();
}
void loop() {
ws2812fx.service();
process_switches();
}
void process_switches() {
unsigned long currentMillis = millis();
if(currentMillis - tmrBounce >= BOUNCE_INTERVAL) {
sw1State = digitalRead(SW1_PIN);
sw2State = digitalRead(SW2_PIN);
sw3State = digitalRead(SW3_PIN);
// compare the switchs State to its previous state
if(sw1State != sw1lastState) {
if(sw1State == LOW) {
// if the current state is LOW then the button went from off to on:
Serial.println("sw1 on");
ws2812fx.swapActiveSegment(0, 1);
} else {
// if the current state is HIGH then the button went from on to off:
Serial.println("sw1 off");
ws2812fx.swapActiveSegment(1, 0);
}
// save the current state as the last state, for next time through the loop
sw1lastState = sw1State;
}
if(sw2State != sw2lastState) { // Putris
if(sw2State == LOW) {
// if the current state is LOW then the switch went from off to on:
Serial.println("sw2 on");
if(Running) {
if(ws2812fx.isActiveSegment(3)) {
ws2812fx.swapActiveSegment(3, 4);
}
} else {
ws2812fx.swapActiveSegment(2, 4);
}
} else {
// if the current state is HIGH then the switch went from on to off:
Serial.println("sw2 off");
tmrTimeout = currentMillis;
Running = true;
}
// save the current state as the last state, for next time through the loop
sw2lastState = sw2State;
}
if(sw3State != sw3lastState) { // Vivus
if(sw3State == LOW) {
// if the current state is LOW then the switch went from off to on:
Serial.println("sw3 on");
if(Running) {
if(ws2812fx.isActiveSegment(4)) {
ws2812fx.swapActiveSegment(4, 3);
}
} else {
ws2812fx.swapActiveSegment(2, 3);
}
} else {
// if the current state is HIGH then the switch went from on to off:
Serial.println("sw3 off");
tmrTimeout = currentMillis;
Running = true;
}
// save the current state as the last state, for next time through the loop
sw3lastState = sw3State;
}
tmrBounce = currentMillis;
}
if(Running) {
if(currentMillis - tmrTimeout >= TIMEOUT_INTERVAL) {
if(ws2812fx.isActiveSegment(3)) { // if seg[0] is active, switch to seg[2]
ws2812fx.swapActiveSegment(3, 2);
} else { // else, switch to seg[0]
ws2812fx.swapActiveSegment(4, 2);
}
Running = false;
}
}
}