-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
218 lines (198 loc) · 6.61 KB
/
code.cpp
File metadata and controls
218 lines (198 loc) · 6.61 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
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
// constants won't change. Used here to set a pin number:
const int red_light_pin = 12;
const int yellow_light_pin = 11;
const int green_light_pin = 10;
const int start_button_pin = 6;
const int pause_button_pin = 5;
const int stop_button_pin = 4;
const int buzzer_pin = 9;
const long iniBlinkPeriod = 60000;
const long interval = 500; // interval at which to blink (milliseconds)
const long beepPeriod = 5000; // the time in ms that beeper will continue
const int buzzerFreq = 1000; // the frequenzy of the buzzer
// Variables will change:
int ledState = LOW; // ledState used to set the LED
int start_buttonState = LOW;
int start_buttonStatePrev = LOW;
int pause_buttonState = LOW;
int pause_buttonStatePrev = LOW;
int stop_buttonState = LOW;
int stop_buttonStatePrev = LOW;
boolean outputTone = false;
long blinkPeriod = iniBlinkPeriod; // the time in ms each led should blink
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long start_pressMillis = 0; // when the start button was last pressed - used for pause stop
unsigned long currentMillis = 0; // will store the current time for each time the loop begins
void setup() {
// set the digital pin as output:
pinMode(red_light_pin, OUTPUT);
pinMode(yellow_light_pin, OUTPUT);
pinMode(green_light_pin, OUTPUT);
pinMode(start_button_pin, INPUT);
pinMode(pause_button_pin, INPUT);
pinMode(stop_button_pin, INPUT);
digitalWrite(start_button_pin, HIGH);
digitalWrite(pause_button_pin, HIGH);
digitalWrite(stop_button_pin, HIGH);
}
void loop() {
currentMillis = millis();
// read the state of the pushbutton value:
readPinStates(true, true, true);
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
setTimeMode(); // enter mode to set the countdown time
startButton();
stopButton();
pauseButton();
setButtonState();
delay(50); // delay to make sure press is registred properly
countdown();
}
void readPinStates(boolean readStart, boolean readPause, boolean readStop) {
if (readStart) {
start_buttonState = digitalRead(start_button_pin);
}
if (readPause) {
pause_buttonState = digitalRead(pause_button_pin);
}
if (readStop) {
stop_buttonState = digitalRead(stop_button_pin);
}
}
void setTimeMode() {
if (pause_buttonState != pause_buttonStatePrev && stop_buttonState != stop_buttonStatePrev) {
boolean setMode = false;
if (stop_buttonState == LOW && pause_buttonState == LOW) {
setMode = true;
ledSoundAll(1, 1, 1, 1); // change to use millis potentially
delay(1000);
ledSoundAll(0, 0, 0, 0);
}
while (setMode == true) {
readPinStates(true, true, true);
if (start_buttonState == LOW) {
setMode = false;
} else {
if (blinkPeriod < iniBlinkPeriod) {
ledSoundAll(1,0,0,0);
if (pause_buttonState == LOW) {
blinkPeriod = iniBlinkPeriod;
}
delay(200);
} else if (blinkPeriod == iniBlinkPeriod) {
ledSoundAll(1,1,0,0);
if (pause_buttonState == LOW) {
blinkPeriod*=1.5;
} else if (stop_buttonState == LOW) {
blinkPeriod/=2;
}
delay(200);
} else {
ledSoundAll(1,1,1,0);
if (stop_buttonState == LOW) {
blinkPeriod=iniBlinkPeriod;
}
delay(200);
}
}
}
}
}
void startButton() {
if (start_buttonState != start_buttonStatePrev) {
if (start_buttonState == LOW) {
// button being pressed down
start_pressMillis = millis();
ledSoundAll(0, 0, 0, 0);
} else {
}
}
}
void stopButton() {
if (stop_buttonState != stop_buttonStatePrev) {
if (stop_buttonState == LOW) {
// button being pressed down
start_pressMillis = 0;
ledSoundAll(0, 0, 0, 0);
} else {
}
}
}
void pauseButton() {
if (pause_buttonState != pause_buttonStatePrev &&
start_pressMillis > 0 &&
currentMillis - start_pressMillis <= blinkPeriod) {
unsigned long pause_startMillis = millis();
unsigned long pause_stopMillis = 0;
if (pause_buttonState == LOW) {
unsigned int pauseExit = 0;
delay(50);
while (pauseExit == 0) {
readPinStates(true, false, false);
if (start_buttonState == LOW) {
pauseExit = 1;
pause_stopMillis = millis();
unsigned long pause_Duration = pause_stopMillis - pause_startMillis;
start_pressMillis += pause_Duration;
}
delay(50);
}
} else {
}
}
}
void setButtonState() {
start_buttonStatePrev = start_buttonState;
pause_buttonStatePrev = pause_buttonState;
stop_buttonStatePrev = stop_buttonState;
}
void ledSoundAll(int green_state, int yellow_state, int red_state, int buzzerState) {
digitalWrite(red_light_pin, red_state);
digitalWrite(yellow_light_pin, yellow_state);
digitalWrite(green_light_pin, green_state);
if (buzzerState == 1) {
tone(buzzer_pin, buzzerFreq);
} else {
noTone(buzzer_pin);
}
}
void countdown() {
if (currentMillis - previousMillis >= interval &&
start_pressMillis > 0) {
if (currentMillis - start_pressMillis <= blinkPeriod) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
if (currentMillis - start_pressMillis <= blinkPeriod / 3) {
digitalWrite(green_light_pin, ledState);
} else if (currentMillis - start_pressMillis <= blinkPeriod / 3 * 2) {
digitalWrite(yellow_light_pin, ledState);
digitalWrite(green_light_pin, HIGH);
} else if (currentMillis - start_pressMillis <= blinkPeriod) {
digitalWrite(red_light_pin, ledState);
digitalWrite(yellow_light_pin, HIGH);
}
} else if (currentMillis - start_pressMillis <= blinkPeriod + beepPeriod &&
currentMillis - start_pressMillis >= blinkPeriod) {
previousMillis = currentMillis;
if (outputTone == false) {
ledSoundAll(1, 1, 1, 1);
outputTone = true;
} else {
ledSoundAll(0, 0, 0, 0);
outputTone = false;
}
} else {
ledSoundAll(0, 0, 0, 0);
start_pressMillis = 0;
}
}
}