-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduinorotaryencoderleds.ino
225 lines (175 loc) · 3.89 KB
/
arduinorotaryencoderleds.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <FastLED.h>
#define NUM_LEDS 15
#define DATA_PIN 6
#define ROTARY_ENCODER_DT_PIN 2
#define ROTARY_ENCODER_CLT_PIN 3
#define ROTARY_ENCODER_BUTTON_PIN 7
CRGB leds[NUM_LEDS];
bool savedRotaryEncoderDtValue;
long unsigned rotaryEncoderSecureTimer;
bool isButtonPressed = false;
int positionIndex = 0;
int range = 1;
bool isRangeMode = false;
bool firstLoop = true;
enum positionStatus
{
UNCHANGED,
CHANGED,
ERROR
};
long unsigned showErrorTimer = 0;
bool errorStatus = false;
void setup()
{
pinMode(ROTARY_ENCODER_BUTTON_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
savedRotaryEncoderDtValue = digitalRead(ROTARY_ENCODER_DT_PIN);
rotaryEncoderSecureTimer = millis();
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}
void loop()
{
bool modeChanged = manageSwitchMode();
enum positionStatus status = managePositionChange();
bool showError = manageErrorVisibility(status);
if (modeChanged || status != UNCHANGED || firstLoop || errorStatus != showError)
{
serialPrintInfo(positionIndex, range);
renderLeds(positionIndex, range, isRangeMode, showError);
firstLoop = false;
errorStatus = showError;
}
}
bool manageErrorVisibility(enum positionStatus status)
{
if (status == ERROR)
{
showErrorTimer = millis();
}
if (showErrorTimer != 0 && abs(millis() - showErrorTimer) < 100)
{
return true;
}
else if (showErrorTimer != 0)
{
showErrorTimer = 0;
}
return false;
}
bool manageSwitchMode()
{
bool buttonPressed = getButtonPressed();
if (buttonPressed)
{
isRangeMode = !isRangeMode;
return true;
}
return false;
}
bool getButtonPressed()
{
int buttonState = digitalRead(ROTARY_ENCODER_BUTTON_PIN);
if (buttonState == HIGH)
{
isButtonPressed = true;
}
else if (isButtonPressed)
{
isButtonPressed = false;
return true;
}
return false;
}
enum positionStatus managePositionChange()
{
bool success = false;
int movement = getRotaryEncoderPosition();
if (movement == 0)
{
return UNCHANGED;
}
if (isRangeMode)
{
success = setGlobalRange(movement, positionIndex, NUM_LEDS);
}
else
{
success = setGlobalPositionIndex(movement, range, NUM_LEDS);
}
return success ? CHANGED : ERROR;
}
bool setGlobalPositionIndex(int movement, int currentRange, int numLed)
{
int newValue = positionIndex + movement;
if (newValue < 0 || newValue + currentRange > numLed)
{
Serial.println("Error bad position");
return false;
}
positionIndex = newValue;
return true;
}
bool setGlobalRange(int movement, int currentPositionIndex, int numLed)
{
int newValue = range + movement;
if (newValue < 1 || newValue + currentPositionIndex > numLed)
{
Serial.println("Error bad range");
return false;
}
range = newValue;
return true;
}
int getRotaryEncoderPosition()
{
bool dtStatus = digitalRead(ROTARY_ENCODER_DT_PIN);
int movement = 0;
if (dtStatus != savedRotaryEncoderDtValue)
{
if (abs(millis() - rotaryEncoderSecureTimer) > 50)
{
if (digitalRead(ROTARY_ENCODER_CLT_PIN) != savedRotaryEncoderDtValue)
{
movement = -1;
}
else
{
movement = 1;
}
rotaryEncoderSecureTimer = millis();
}
savedRotaryEncoderDtValue = dtStatus;
}
return movement;
}
void serialPrintInfo(int currentPositionIndex, int currentRange)
{
Serial.print("Position :");
Serial.print(currentPositionIndex);
Serial.print(" Range :");
Serial.println(currentRange);
}
void renderLeds(int startIndex, int range, bool isRangeMode, bool error)
{
for (int j = startIndex; j < startIndex + range; j++)
{
leds[j] = getLedColor(isRangeMode, error);
}
FastLED.show();
FastLED.clear();
}
CRGB getLedColor(bool isRangeMode, bool error)
{
CRGB color = CRGB::Green;
if (error)
{
color = CRGB::Red;
}
else if (isRangeMode)
{
color = CRGB::White;
}
return color;
}