-
Notifications
You must be signed in to change notification settings - Fork 0
/
Homework3.ino
354 lines (332 loc) · 9.2 KB
/
Homework3.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const int pinSW = 2;
const int pinX = A0;
const int pinY = A1;
// declare all the segments pins and states
const int pinA = 4;
const int pinB = 5;
const int pinC = 6;
const int pinD = 7;
const int pinE = 8;
const int pinF = 9;
const int pinG = 10;
const int pinDP = 11;
const int segSize = 8;
byte ledAState = LOW;
byte ledBState = LOW;
byte ledCState = LOW;
byte ledDState = LOW;
byte ledEState = LOW;
byte ledFState = LOW;
byte ledGState = LOW;
byte ledDPState = LOW;
int segments[segSize] = {
pinA, pinB, pinC, pinD, pinE, pinF, pinG, pinDP
};
// declare variables for blinking state
int currentBlinkPin = 11;
byte blinkLedState = LOW;
byte doNotBlink = LOW;
// declare variables for joystick potentiometers
int xValue = 0;
int yValue = 0;
int minThreshold = 400;
int maxThreshold = 600;
byte joyBackToMiddleYstate2 = LOW;
byte joyBackToMiddleYstate1 = LOW;
byte joyBackToMiddleX = LOW;
// declare variables for joystick switch
byte swState = LOW;
byte lastSwState = HIGH;
byte switchState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned int debounceDelay = 50;
unsigned int blinkInterval = 400;
unsigned long lastBlinkTime = 0;
unsigned long pressedTime;
unsigned long releasedTime;
unsigned long noOfPresses = 0;
const int longPressDuration = 1000;
const int shortPressDuration = 50;
int state = 1;
void setup() {
for (int i = 0; i < segSize; i++) {
pinMode(segments[i], OUTPUT);
}
pinMode(pinSW, INPUT_PULLUP);
Serial.begin(9600);
}
// function that displays the state of all the segments
void displayLeds() {
if (currentBlinkPin != pinA)
digitalWrite(pinA, ledAState);
if (currentBlinkPin != pinB)
digitalWrite(pinB, ledBState);
if (currentBlinkPin != pinC)
digitalWrite(pinC, ledCState);
if (currentBlinkPin != pinD)
digitalWrite(pinD, ledDState);
if (currentBlinkPin != pinE)
digitalWrite(pinE, ledEState);
if (currentBlinkPin != pinF)
digitalWrite(pinF, ledFState);
if (currentBlinkPin != pinG)
digitalWrite(pinG, ledGState);
if (currentBlinkPin != pinDP)
digitalWrite(pinDP, ledDPState);
digitalWrite(currentBlinkPin, blinkLedState);
}
// function that resets the state of all the segments
// and moves the blinking to the decimal point
void resetLeds() {
ledAState = LOW;
ledBState = LOW;
ledCState = LOW;
ledDState = LOW;
ledEState = LOW;
ledFState = LOW;
ledGState = LOW;
ledDPState = LOW;
currentBlinkPin = pinDP;
}
// function that moves the blinking segment according
// to the movement of the joystick
void moveBlinkingState1() {
if (yValue < minThreshold && joyBackToMiddleYstate1 == LOW && state == 1) {
if (currentBlinkPin == pinA) {
currentBlinkPin = pinF;
Serial.println("left from A to F");
}
else if (currentBlinkPin == pinB) {
currentBlinkPin = pinF;
Serial.println("left from B to F");
}
else if (currentBlinkPin == pinC) {
currentBlinkPin = pinE;
Serial.println("left from C to E");
}
else if (currentBlinkPin == pinDP) {
currentBlinkPin = pinC;
Serial.println("left from DP to C");
}
else if (currentBlinkPin == pinD) {
currentBlinkPin = pinE;
Serial.println("left from D to E");
}
joyBackToMiddleYstate1 = HIGH;
}
else if (yValue > maxThreshold && joyBackToMiddleYstate1 == LOW && state == 1) {
if (currentBlinkPin == pinA || currentBlinkPin == pinF) {
currentBlinkPin = pinB;
Serial.println("right from A to B");
}
else if (currentBlinkPin == pinF) {
currentBlinkPin = pinB;
Serial.println("right from F to B");
}
else if (currentBlinkPin == pinC) {
currentBlinkPin = pinDP;
Serial.println("right from C to DP");
}
else if (currentBlinkPin == pinE) {
currentBlinkPin = pinC;
Serial.println("right from E to C");
}
else if (currentBlinkPin == pinD) {
currentBlinkPin = pinC;
Serial.println("right from D to C");
}
joyBackToMiddleYstate1 = HIGH;
}
else if (joyBackToMiddleYstate1 == HIGH && yValue < maxThreshold && yValue > minThreshold && state == 1) {
joyBackToMiddleYstate1 = LOW;
}
if (xValue < minThreshold && joyBackToMiddleX == LOW && state == 1) {
if (currentBlinkPin == pinG) {
currentBlinkPin = pinD;
Serial.println("down from G to D");
}
else if (currentBlinkPin == pinA) {
currentBlinkPin = pinG;
Serial.println("down from A to G");
}
else if (currentBlinkPin == pinB) {
currentBlinkPin = pinG;
Serial.println("down from B to G");
}
else if (currentBlinkPin == pinF) {
currentBlinkPin = pinG;
Serial.println("down from F to G");
}
else if (currentBlinkPin == pinC) {
currentBlinkPin = pinD;
Serial.println("down from C to D");
}
else if (currentBlinkPin == pinE) {
currentBlinkPin = pinD;
Serial.println("down from E to D");
}
joyBackToMiddleX = HIGH;
}
else if (xValue > maxThreshold && joyBackToMiddleX == LOW && state == 1) {
if (currentBlinkPin == pinG) {
currentBlinkPin = pinA;
Serial.println("up from G to A");
}
else if (currentBlinkPin == pinD) {
currentBlinkPin = pinG;
Serial.println("up from D to G");
}
else if (currentBlinkPin == pinE) {
currentBlinkPin = pinG;
Serial.println("up from E to G");
}
else if (currentBlinkPin == pinC) {
currentBlinkPin = pinG;
Serial.println("up from C to G");
}
else if (currentBlinkPin == pinB) {
currentBlinkPin = pinA;
Serial.println("up from B to A");
}
else if (currentBlinkPin == pinF) {
currentBlinkPin = pinA;
Serial.println("up from F to A");
}
joyBackToMiddleX = HIGH;
}
else if (joyBackToMiddleX == HIGH && xValue < maxThreshold && xValue > minThreshold && state == 1) {
joyBackToMiddleX = LOW;
}
}
// function that turns on or off the segment
// by using the joystick movement on the y axis (left - off, right - on)
void turnOnOffState2() {
if (yValue < minThreshold && joyBackToMiddleYstate2 == LOW && state == 2) {
blinkLedState = LOW;
if (currentBlinkPin == pinA) {
ledAState = LOW;
Serial.println("off A");
}
else if (currentBlinkPin == pinB) {
ledBState = LOW;
Serial.println("off B");
}
else if (currentBlinkPin == pinC) {
ledCState = LOW;
Serial.println("off C");
}
else if (currentBlinkPin == pinD) {
ledDState = LOW;
Serial.println("off D");
}
else if (currentBlinkPin == pinE) {
ledEState = LOW;
Serial.println("off E");
}
else if (currentBlinkPin == pinF) {
ledFState = LOW;
Serial.println("off F");
}
else if (currentBlinkPin == pinG) {
ledGState = LOW;
Serial.println("off G");
}
else if (currentBlinkPin == pinDP) {
ledDPState = LOW;
Serial.println("off DP");
}
joyBackToMiddleYstate2 = HIGH;
}
else if (yValue > maxThreshold && joyBackToMiddleYstate2 == LOW && state == 2) {
blinkLedState = HIGH;
if (currentBlinkPin == pinA) {
ledAState = HIGH;
Serial.println("on A");
}
else if (currentBlinkPin == pinB) {
ledBState = HIGH;
Serial.println("on B");
}
else if (currentBlinkPin == pinC) {
ledCState = HIGH;
Serial.println("on C");
}
else if (currentBlinkPin == pinD) {
ledDState = HIGH;
Serial.println("on D");
}
else if (currentBlinkPin == pinE) {
ledEState = HIGH;
Serial.println("on E");
}
else if (currentBlinkPin == pinF) {
ledFState = HIGH;
Serial.println("on F");
}
else if (currentBlinkPin == pinG) {
ledGState = HIGH;
Serial.println("on G");
}
else if (currentBlinkPin == pinDP) {
ledDPState = HIGH;
Serial.println("on DP");
}
joyBackToMiddleYstate2 = HIGH;
}
else if (joyBackToMiddleYstate2 == HIGH && yValue < maxThreshold && yValue > minThreshold && state == 2) {
joyBackToMiddleYstate2 = LOW;
}
}
// function that changes the state based on
// how the button is pressed
// (long press - reset, short press - enter state 1)
void buttonChangeState() {
if (swState != lastSwState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (swState != switchState) {
switchState = swState;
if (switchState == LOW) {
pressedTime = millis();
}
else {
releasedTime = millis();
}
long pressDuration = releasedTime - pressedTime;
if (pressDuration > longPressDuration and state == 1) {
Serial.println("long press");
resetLeds();
}
else if (pressDuration > shortPressDuration) {
Serial.println("short press");
noOfPresses ++;
if (noOfPresses % 2 != 0) {
doNotBlink = HIGH;
state = 2;
}
else {
doNotBlink = LOW;
state = 1;
}
}
}
}
lastSwState = swState;
}
void blink() {
if (millis() - lastBlinkTime > blinkInterval && doNotBlink == LOW) {
blinkLedState = !blinkLedState;
lastBlinkTime = millis();
}
}
void loop() {
displayLeds();
xValue = analogRead(pinX);
yValue = analogRead(pinY);
blink();
moveBlinkingState1();
turnOnOffState2();
swState = digitalRead(pinSW);
buttonChangeState();
}