Components and supplies
5 mm LED: Green
Arduino Nano R3
Buzzer
Tactile Switch, SPST-NO
Resistor 220 ohm
Shift Register- Parallel to Serial
Resistor 100 ohm
SparkFun 7-Segment Serial Display - Red
LED, Orange
Tools and machines
Solder Wire, Lead Free
Soldering iron (generic)
Apps and platforms
Arduino IDE
Project description
Code
Pomodoro code
arduino
1int D1 = 2; 2int D2 = 3; 3int D3 = 4; 4int D4 = 5; 5 6int dataPin = 11; 7int latchPin = 8; 8int clockPin = 12; 9 10int buzzerPin = 6; 11 12int buttonPin = 7; 13 14int stateLedPin = 9; 15 16// ----- ^^ PINS ^^ ----- 17 18int delayTime = 4; 19int buzzerFrequency = 523; 20 21int nums[10] = { 22 1, 79, 18, 6, 76, 36, 32, 15, 0, 4 23}; 24int blanckNum = 127; 25int pLetter = 24; 26 27void updateShiftRegisterState(byte input){ 28 digitalWrite(latchPin, LOW); 29 shiftOut(dataPin, clockPin, LSBFIRST, input); 30 digitalWrite(latchPin, HIGH); 31} 32 33void draw(int state, bool enableDecimal){ 34 int leds = state*2; 35 if(!enableDecimal) { 36 leds = leds + 1; 37 } 38 39 updateShiftRegisterState(leds); 40} 41 42void resetTo(int letter, int dState){ 43 digitalWrite(D1, dState); 44 digitalWrite(D2, dState); 45 digitalWrite(D3, dState); 46 digitalWrite(D4, dState); 47 48 draw(letter, false); 49} 50 51void setDigit(int d1, int d2, int d3, int d4){ 52 digitalWrite(D1, d1); 53 digitalWrite(D2, d2); 54 digitalWrite(D3, d3); 55 digitalWrite(D4, d4); 56} 57 58void setD1() { 59 setDigit(HIGH, LOW, LOW, LOW); 60} 61 62void setD2() { 63 setDigit(LOW, HIGH, LOW, LOW); 64} 65 66void setD3() { 67 setDigit(LOW, LOW, HIGH, LOW); 68} 69 70void setD4() { 71 setDigit(LOW, LOW, LOW, HIGH); 72} 73 74void drawNumber(int num){ 75 if(num == -1){ 76 resetTo(blanckNum, LOW); 77 return; 78 } 79 else if(num == -2){ 80 resetTo(pLetter, HIGH); 81 return; 82 } 83 84 for(int i=0; i<4; i++){ 85 if(num==0 and i>0){ 86 break; 87 } 88 89 int dig = num % 10; 90 num = num/10; 91 92 if(i==0){ 93 setD4(); 94 } 95 else if(i==1){ 96 setD3(); 97 } 98 else if(i==2){ 99 setD2(); 100 } 101 else if(i==3){ 102 setD1(); 103 } 104 bool enableDecimal = false; 105 if(i==2){ 106 enableDecimal = true; 107 } 108 draw(nums[dig], enableDecimal); 109 delay(delayTime); 110 resetTo(blanckNum, LOW); 111 } 112} 113 114int secToTime(int secs){ 115 int mins = secs/60; 116 int remSecs = secs%60; 117 118 return mins*100 + remSecs; 119} 120 121int pomodoroSecs[8] = { 122 1500, 300, 1500, 300, 1500, 300, 1500, 900 123}; 124//int pomodoroSecs[8] = { 125// 10, 5, 10, 5, 10, 5, 10, 6 126//}; 127 128bool isWorkState[8] = { 129 true, false, true, false, true, false, true, false 130}; 131 132int curPomodoro = 0; 133 134int curSecs = -1; 135 136int STOP_SIGNAL = -2; 137int WELCOME_STATE = -1; 138int TIMER_STATE = 0; 139int TIMER_END = 1; 140int TIMER_PAUSED = 2; 141int SKIP_SIGNAL = 3; 142 143int deviceState = WELCOME_STATE; 144 145void resetTimer(){ 146 curSecs = 0; 147 curPomodoro = (curPomodoro+1)%8; 148} 149 150unsigned long nextMillis = 0; 151 152int setNextMillis(int gap){ 153 nextMillis = millis()+gap; 154 nextMillis = nextMillis - nextMillis%100; 155} 156 157int lastButtonState = LOW; 158int curButtonState; 159unsigned long debounceDelay = 50; 160unsigned long lastDebounceTime = 0; 161unsigned long buttonDownMillis = 0; 162 163unsigned long longPressTime = 1000; 164unsigned long stopPressTime = 3000; 165 166unsigned long isButtonPressed(){ 167 unsigned long pressTime = 0; 168 169 int buttonState = digitalRead(buttonPin); 170 171 if(lastButtonState != buttonState) 172 lastDebounceTime = millis(); 173 174 if((millis() - lastDebounceTime) > debounceDelay) 175 if(buttonState != curButtonState){ 176 curButtonState = buttonState; 177 178 if(curButtonState == HIGH) 179 buttonDownMillis = millis(); 180 if(curButtonState == LOW) 181 pressTime = millis() - buttonDownMillis; 182 } 183 184 lastButtonState = buttonState; 185 return pressTime; 186} 187 188bool disOn = true; 189 190void resetDevice(){ 191 curSecs = -1; 192 curPomodoro = 0; 193} 194 195int timerPaused(){ 196 unsigned long buttonMillis = isButtonPressed(); 197 if(buttonMillis>stopPressTime){ 198 drawNumber(-1); 199 noTone(buzzerPin); 200 resetDevice(); 201 return STOP_SIGNAL; 202 } 203 else if(buttonMillis>longPressTime) 204 return SKIP_SIGNAL; 205 else if(buttonMillis) 206 return TIMER_STATE; 207 208 if(millis() > nextMillis){ 209 setNextMillis(500); 210 disOn = !disOn; 211 } 212 213 if(disOn) 214 drawNumber(secToTime(curSecs)); 215 else 216 drawNumber(-1); 217 218 return TIMER_PAUSED; 219 220} 221 222int timerRunner(){ 223 224 unsigned long buttonMillis = isButtonPressed(); 225 if(buttonMillis>stopPressTime){ 226 drawNumber(-1); 227 noTone(buzzerPin); 228 resetDevice(); 229 return STOP_SIGNAL; 230 } 231 else if(buttonMillis>longPressTime) 232 return SKIP_SIGNAL; 233 else if(buttonMillis) { 234 tone(buzzerPin, buzzerFrequency); 235 noTone(buzzerPin); 236 return TIMER_PAUSED; 237 } 238 239 if(curSecs <= pomodoroSecs[curPomodoro]){ 240 if(millis() > nextMillis){ 241 curSecs++; 242 setNextMillis(1000); 243 } 244 245 digitalWrite(stateLedPin, isWorkState[curPomodoro]); 246 247 drawNumber(secToTime(curSecs)); 248 return TIMER_STATE; 249 } 250 else{ 251 resetTimer(); 252 return TIMER_END; 253 } 254} 255 256int timerEnderProgramm(){ 257 unsigned long buttonMillis = isButtonPressed(); 258 if(buttonMillis>stopPressTime){ 259 drawNumber(-1); 260 noTone(buzzerPin); 261 resetDevice(); 262 return STOP_SIGNAL; 263 } 264 else if(buttonMillis > longPressTime) 265 return SKIP_SIGNAL; 266 else if(buttonMillis){ 267 noTone(buzzerPin); 268 return TIMER_STATE; 269 } 270 271 digitalWrite(stateLedPin, isWorkState[curPomodoro]); 272 273 if(millis() > nextMillis){ 274 setNextMillis(500); 275 disOn = !disOn; 276 } 277 278 if(disOn){ 279 drawNumber(secToTime(pomodoroSecs[curPomodoro])); 280 tone(buzzerPin, buzzerFrequency); 281 } 282 else{ 283 drawNumber(-1); 284 noTone(buzzerPin); 285 } 286 return TIMER_END; 287} 288 289int skipCurrentTimer(){ 290 resetTimer(); 291 return TIMER_END; 292} 293 294 295unsigned long welcomeTomeLength = 2000; 296unsigned long startMillis = 0; 297int welcomeProgramm(){ 298 if(millis()-startMillis<=welcomeTomeLength){ 299 tone(buzzerPin, buzzerFrequency); 300 } 301 else 302 noTone(buzzerPin); 303 304 unsigned long buttonMillis = isButtonPressed(); 305 if(buttonMillis>stopPressTime){ 306 drawNumber(-1); 307 noTone(buzzerPin); 308 resetDevice(); 309 return STOP_SIGNAL; 310 } 311 else if(buttonMillis){ 312 pinMode(stateLedPin, OUTPUT); 313 noTone(buzzerPin); 314 return TIMER_STATE; 315 } 316 317 drawNumber(-2); 318 return WELCOME_STATE; 319} 320 321int doNothing(){ 322 pinMode(stateLedPin, INPUT); 323 if(isButtonPressed()){ 324 startMillis = millis(); 325 return WELCOME_STATE; 326 } 327 328 return STOP_SIGNAL; 329} 330 331void setup() { 332 pinMode(D1, OUTPUT); 333 pinMode(D2, OUTPUT); 334 pinMode(D3, OUTPUT); 335 pinMode(D4, OUTPUT); 336 337 pinMode(dataPin, OUTPUT); 338 pinMode(latchPin, OUTPUT); 339 pinMode(clockPin, OUTPUT); 340 341 pinMode(buzzerPin, OUTPUT); 342 343 pinMode(buttonPin, INPUT); 344 345 pinMode(stateLedPin, INPUT); 346} 347 348void loop() { 349 if(deviceState == STOP_SIGNAL) 350 deviceState = doNothing(); 351 else if(deviceState == WELCOME_STATE) 352 deviceState = welcomeProgramm(); 353 else if(deviceState == TIMER_STATE) 354 deviceState = timerRunner(); 355 else if(deviceState == TIMER_PAUSED) 356 deviceState = timerPaused(); 357 else if(deviceState == TIMER_END) 358 deviceState = timerEnderProgramm(); 359 else if(deviceState == SKIP_SIGNAL) 360 deviceState = skipCurrentTimer(); 361 362}
Pomodoro code
arduino
1int D1 = 2; 2int D2 = 3; 3int D3 = 4; 4int D4 = 5; 5 6int dataPin 7 = 11; 8int latchPin = 8; 9int clockPin = 12; 10 11int buzzerPin = 6; 12 13int 14 buttonPin = 7; 15 16int stateLedPin = 9; 17 18// ----- ^^ PINS ^^ ----- 19 20int 21 delayTime = 4; 22int buzzerFrequency = 523; 23 24int nums[10] = { 25 1, 79, 26 18, 6, 76, 36, 32, 15, 0, 4 27}; 28int blanckNum = 127; 29int pLetter = 24; 30 31void 32 updateShiftRegisterState(byte input){ 33 digitalWrite(latchPin, LOW); 34 shiftOut(dataPin, 35 clockPin, LSBFIRST, input); 36 digitalWrite(latchPin, HIGH); 37} 38 39void 40 draw(int state, bool enableDecimal){ 41 int leds = state*2; 42 if(!enableDecimal) 43 { 44 leds = leds + 1; 45 } 46 47 updateShiftRegisterState(leds); 48} 49 50void 51 resetTo(int letter, int dState){ 52 digitalWrite(D1, dState); 53 digitalWrite(D2, 54 dState); 55 digitalWrite(D3, dState); 56 digitalWrite(D4, dState); 57 58 59 draw(letter, false); 60} 61 62void setDigit(int d1, int d2, int d3, int d4){ 63 64 digitalWrite(D1, d1); 65 digitalWrite(D2, d2); 66 digitalWrite(D3, d3); 67 68 digitalWrite(D4, d4); 69} 70 71void setD1() { 72 setDigit(HIGH, LOW, LOW, 73 LOW); 74} 75 76void setD2() { 77 setDigit(LOW, HIGH, LOW, LOW); 78} 79 80void 81 setD3() { 82 setDigit(LOW, LOW, HIGH, LOW); 83} 84 85void setD4() { 86 setDigit(LOW, 87 LOW, LOW, HIGH); 88} 89 90void drawNumber(int num){ 91 if(num == -1){ 92 resetTo(blanckNum, 93 LOW); 94 return; 95 } 96 else if(num == -2){ 97 resetTo(pLetter, HIGH); 98 99 return; 100 } 101 102 for(int i=0; i<4; i++){ 103 if(num==0 and i>0){ 104 105 break; 106 } 107 108 int dig = num % 10; 109 num = num/10; 110 111 112 if(i==0){ 113 setD4(); 114 } 115 else if(i==1){ 116 setD3(); 117 118 } 119 else if(i==2){ 120 setD2(); 121 } 122 else if(i==3){ 123 124 setD1(); 125 } 126 bool enableDecimal = false; 127 if(i==2){ 128 129 enableDecimal = true; 130 } 131 draw(nums[dig], enableDecimal); 132 133 delay(delayTime); 134 resetTo(blanckNum, LOW); 135 } 136} 137 138int secToTime(int 139 secs){ 140 int mins = secs/60; 141 int remSecs = secs%60; 142 143 return mins*100 144 + remSecs; 145} 146 147int pomodoroSecs[8] = { 148 1500, 300, 1500, 300, 1500, 149 300, 1500, 900 150}; 151//int pomodoroSecs[8] = { 152// 10, 5, 10, 5, 10, 5, 10, 153 6 154//}; 155 156bool isWorkState[8] = { 157 true, false, true, false, true, false, 158 true, false 159}; 160 161int curPomodoro = 0; 162 163int curSecs = -1; 164 165int 166 STOP_SIGNAL = -2; 167int WELCOME_STATE = -1; 168int TIMER_STATE = 0; 169int TIMER_END 170 = 1; 171int TIMER_PAUSED = 2; 172int SKIP_SIGNAL = 3; 173 174int deviceState = WELCOME_STATE; 175 176void 177 resetTimer(){ 178 curSecs = 0; 179 curPomodoro = (curPomodoro+1)%8; 180} 181 182unsigned 183 long nextMillis = 0; 184 185int setNextMillis(int gap){ 186 nextMillis = millis()+gap; 187 188 nextMillis = nextMillis - nextMillis%100; 189} 190 191int lastButtonState = 192 LOW; 193int curButtonState; 194unsigned long debounceDelay = 50; 195unsigned long 196 lastDebounceTime = 0; 197unsigned long buttonDownMillis = 0; 198 199unsigned long 200 longPressTime = 1000; 201unsigned long stopPressTime = 3000; 202 203unsigned long 204 isButtonPressed(){ 205 unsigned long pressTime = 0; 206 207 int buttonState 208 = digitalRead(buttonPin); 209 210 if(lastButtonState != buttonState) 211 lastDebounceTime 212 = millis(); 213 214 if((millis() - lastDebounceTime) > debounceDelay) 215 if(buttonState 216 != curButtonState){ 217 curButtonState = buttonState; 218 219 if(curButtonState 220 == HIGH) 221 buttonDownMillis = millis(); 222 if(curButtonState == 223 LOW) 224 pressTime = millis() - buttonDownMillis; 225 } 226 227 lastButtonState 228 = buttonState; 229 return pressTime; 230} 231 232bool disOn = true; 233 234void 235 resetDevice(){ 236 curSecs = -1; 237 curPomodoro = 0; 238} 239 240int timerPaused(){ 241 242 unsigned long buttonMillis = isButtonPressed(); 243 if(buttonMillis>stopPressTime){ 244 245 drawNumber(-1); 246 noTone(buzzerPin); 247 resetDevice(); 248 return 249 STOP_SIGNAL; 250 } 251 else if(buttonMillis>longPressTime) 252 return SKIP_SIGNAL; 253 254 else if(buttonMillis) 255 return TIMER_STATE; 256 257 if(millis() > nextMillis){ 258 259 setNextMillis(500); 260 disOn = !disOn; 261 } 262 263 if(disOn) 264 drawNumber(secToTime(curSecs)); 265 266 else 267 drawNumber(-1); 268 269 return TIMER_PAUSED; 270 271} 272 273int 274 timerRunner(){ 275 276 unsigned long buttonMillis = isButtonPressed(); 277 if(buttonMillis>stopPressTime){ 278 279 drawNumber(-1); 280 noTone(buzzerPin); 281 resetDevice(); 282 return 283 STOP_SIGNAL; 284 } 285 else if(buttonMillis>longPressTime) 286 return SKIP_SIGNAL; 287 288 else if(buttonMillis) { 289 tone(buzzerPin, buzzerFrequency); 290 noTone(buzzerPin); 291 292 return TIMER_PAUSED; 293 } 294 295 if(curSecs <= pomodoroSecs[curPomodoro]){ 296 297 if(millis() > nextMillis){ 298 curSecs++; 299 setNextMillis(1000); 300 301 } 302 303 digitalWrite(stateLedPin, isWorkState[curPomodoro]); 304 305 306 drawNumber(secToTime(curSecs)); 307 return TIMER_STATE; 308 } 309 else{ 310 311 resetTimer(); 312 return TIMER_END; 313 } 314} 315 316int timerEnderProgramm(){ 317 318 unsigned long buttonMillis = isButtonPressed(); 319 if(buttonMillis>stopPressTime){ 320 321 drawNumber(-1); 322 noTone(buzzerPin); 323 resetDevice(); 324 return 325 STOP_SIGNAL; 326 } 327 else if(buttonMillis > longPressTime) 328 return SKIP_SIGNAL; 329 330 else if(buttonMillis){ 331 noTone(buzzerPin); 332 return TIMER_STATE; 333 334 } 335 336 digitalWrite(stateLedPin, isWorkState[curPomodoro]); 337 338 if(millis() 339 > nextMillis){ 340 setNextMillis(500); 341 disOn = !disOn; 342 } 343 344 345 if(disOn){ 346 drawNumber(secToTime(pomodoroSecs[curPomodoro])); 347 tone(buzzerPin, 348 buzzerFrequency); 349 } 350 else{ 351 drawNumber(-1); 352 noTone(buzzerPin); 353 354 } 355 return TIMER_END; 356} 357 358int skipCurrentTimer(){ 359 resetTimer(); 360 361 return TIMER_END; 362} 363 364 365unsigned long welcomeTomeLength = 2000; 366unsigned 367 long startMillis = 0; 368int welcomeProgramm(){ 369 if(millis()-startMillis<=welcomeTomeLength){ 370 371 tone(buzzerPin, buzzerFrequency); 372 } 373 else 374 noTone(buzzerPin); 375 376 377 unsigned long buttonMillis = isButtonPressed(); 378 if(buttonMillis>stopPressTime){ 379 380 drawNumber(-1); 381 noTone(buzzerPin); 382 resetDevice(); 383 return 384 STOP_SIGNAL; 385 } 386 else if(buttonMillis){ 387 pinMode(stateLedPin, OUTPUT); 388 389 noTone(buzzerPin); 390 return TIMER_STATE; 391 } 392 393 drawNumber(-2); 394 395 return WELCOME_STATE; 396} 397 398int doNothing(){ 399 pinMode(stateLedPin, INPUT); 400 401 if(isButtonPressed()){ 402 startMillis = millis(); 403 return WELCOME_STATE; 404 405 } 406 407 return STOP_SIGNAL; 408} 409 410void setup() { 411 pinMode(D1, OUTPUT); 412 413 pinMode(D2, OUTPUT); 414 pinMode(D3, OUTPUT); 415 pinMode(D4, OUTPUT); 416 417 418 pinMode(dataPin, OUTPUT); 419 pinMode(latchPin, OUTPUT); 420 pinMode(clockPin, 421 OUTPUT); 422 423 pinMode(buzzerPin, OUTPUT); 424 425 pinMode(buttonPin, INPUT); 426 427 428 pinMode(stateLedPin, INPUT); 429} 430 431void loop() { 432 if(deviceState == 433 STOP_SIGNAL) 434 deviceState = doNothing(); 435 else if(deviceState == WELCOME_STATE) 436 437 deviceState = welcomeProgramm(); 438 else if(deviceState == TIMER_STATE) 439 440 deviceState = timerRunner(); 441 else if(deviceState == TIMER_PAUSED) 442 deviceState 443 = timerPaused(); 444 else if(deviceState == TIMER_END) 445 deviceState = timerEnderProgramm(); 446 447 else if(deviceState == SKIP_SIGNAL) 448 deviceState = skipCurrentTimer(); 449 450 451}
Downloadable files
Schematics
Schematics
Comments
Only logged in users can leave comments