-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
105 lines (91 loc) · 2.71 KB
/
code.cpp
File metadata and controls
105 lines (91 loc) · 2.71 KB
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
#include <SevSeg.h>
SevSeg sevseg;
/* -- TIMER -- */
static int seconds = 0;
/* -- Buzz Game -- */
static int touchCount = 0;
const int touchPin = A0;
const int touchThreshold = 1000;
const int maxTouchCount = 20;
const unsigned long touchDelay = 500;
const unsigned long resetDelay = 2000;
/* -- 7-Segment -- */
const byte numDigits = 4;
const byte digitPins[] = {2, 5, 6, 8};
const byte segmentPins[] = {3, 7, 12, 10, 9, 4, 13, 11};
const bool resOnSeg = false;
void setup() {
// Start the 7-Segment instane
sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins, resOnSeg);
// Set screen brightness to 100, range is (-200 || +200)
sevseg.setBrightness(100);
}
void loop() {
// Read sensor value
int touchValue = analogRead(touchPin);
static bool wasTouched = false;
// Check if touch value exceeds threshold
if (touchValue >= touchThreshold) {
// Check if it wasn't touched previously
if (!wasTouched) {
// Increment touch count
touchCount++;
// Check if touch count exceeds maximum
if (touchCount >= maxTouchCount) {
touchCount = 0; // Reset touch count
failProc(); // call failProc function
} else {
// Display current touch count
sevseg.setNumber(touchCount, 0);
delay(touchDelay);
}
}
wasTouched = true; // Set wasTouched to true
} else {
wasTouched = false; // Set wasTouched to false
}
// If touch count is 0, display 0
if (touchCount == 0) {
sevseg.setNumber(0, 0);
}
// Start counting (from Arduino startup)
static unsigned long timer = millis();
// Increment timer every second
if (millis() - timer >= 1000) {
timer += 1000;
seconds++;
if (seconds == 1000) {
seconds = 0; // Reset seconds
}
// Extract last two digits of the timer
int lastTwoDigits = seconds % 100;
// Constrain the timer, hence display the value
sevseg.setNumber(lastTwoDigits, 2);
}
// Check if the timer exceeds 60 seconds without exceeding maxTouchCount
if (seconds >= 60 && touchCount < maxTouchCount) {
winProc(); // Call winProc function
}
// Refresh the display
sevseg.refreshDisplay();
}
auto failProc() -> void {
sevseg.blank(); // Clear the display
// Blink the fail message thrice
for (int i = 0; i < 3; ++i) {
sevseg.setChars("FAIL");
delay(300); // Display "FAIL" for 300 milli
sevseg.blank(); // Clear display
delay(300); // Delay for 300 milli
}
}
auto winProc() -> void {
sevseg.blank(); // Clear the display
// Blink the win message thrice
for (int i = 0; i < 3; ++i) {
sevseg.setChars("000");
delay(300); // Display "WIN" for 300 milli
sevseg.blank(); // Clear display
delay(300); // Delay for 300 milli
}
}