Advertisement
markruff

procratination-timer-arduino

Apr 12th, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. /*
  2.  * I have two essays to write. Each 5000 words with a tonne
  3.  * of literature review required. The deadline is fast
  4.  * approaching.
  5.  *
  6.  * I can't start writing until I have some form of hardware
  7.  * timer to let me know how long I've been working for. If
  8.  * this program were on twitter: #procrastination
  9.  *
  10.  * LEDs count up 10 minute increments (in binary) reseting
  11.  * at 60 minutes with an alarm tone.
  12.  *
  13.  * Mark Ruff, April 2016
  14.  */
  15.  
  16. // GLOBALS
  17.  
  18.  
  19. // LED pins
  20. int pin1 = 8;
  21. int pin2 = 7;
  22. int pin3 = 4;
  23. int indicatorLed = 13; // so I know it's working
  24. int resetSwitch = 2;
  25. int speaker = 11;
  26.  
  27. // timing
  28. unsigned long int timer = 0;
  29. unsigned long int prevMillis = 0;
  30. unsigned long int ledTime = 600000; // time to increment the LEDS = 10 mins
  31. unsigned long int alarmTime = 3600000; // time to alarm and reset LEDS/counter
  32.  
  33. // notes for alarm
  34. int alarmNoteCount = 3;
  35. int alarmNotes[] = { 196, 196, 261 };
  36. int alarmNoteLen[] { 500, 250, 2000 };
  37.  
  38. void setup() {
  39.   // LED pins and speaker = output, reset switch = input
  40.   pinMode(pin1,OUTPUT);
  41.   pinMode(pin2,OUTPUT);
  42.   pinMode(pin3,OUTPUT);
  43.   pinMode(speaker,OUTPUT);
  44.   pinMode(resetSwitch,INPUT);
  45.   pinMode(indicatorLed,OUTPUT);
  46. }
  47.  
  48. void loop() {
  49.   // increment the timer
  50.   timer += millis() - prevMillis;
  51.   prevMillis = millis();
  52.  
  53.   if (digitalRead(resetSwitch) == HIGH) {
  54.     // reset stuff
  55.     digitalWrite(pin1, LOW);
  56.     digitalWrite(pin2, LOW);
  57.     digitalWrite(pin3, LOW);
  58.     timer = 0;
  59.    
  60.     // need delay to get finger off the button
  61.     delay(500);
  62.   }
  63.  
  64.   if ( timer > alarmTime ) {
  65.     // flash LEDs
  66.     for ( int i = 0 ; i < 10 ; i++ ) {
  67.       digitalWrite(pin1, HIGH);
  68.       digitalWrite(pin2, HIGH);
  69.       digitalWrite(pin3, HIGH);
  70.       delay(200);
  71.       digitalWrite(pin1, LOW);
  72.       digitalWrite(pin2, LOW);
  73.       digitalWrite(pin3, LOW);
  74.       delay(100);
  75.     }
  76.     // play alarm
  77.     for (int i = 0 ; i < alarmNoteCount ; i++ ) {
  78.         tone(speaker, alarmNotes[i], alarmNoteLen[i]);
  79.         delay(alarmNoteLen[i]);
  80.     }
  81.     // reset timer
  82.     timer = 0;
  83.   }
  84.  
  85.   // light up the LEDs for the time
  86.   int timerConversion = timer / ledTime;
  87.   if (timerConversion  == 1 || timerConversion  == 3 || timerConversion  == 5) {
  88.     digitalWrite(pin1, HIGH);
  89.   }
  90.   else {
  91.     digitalWrite(pin1, LOW);
  92.   }
  93.   if (timerConversion  == 2 || timerConversion  == 3 || timerConversion  == 6) {
  94.     digitalWrite(pin2, HIGH);
  95.   }
  96.   else {
  97.     digitalWrite(pin2, LOW);
  98.   }
  99.   if (timerConversion  > 3) {
  100.     digitalWrite(pin3, HIGH);
  101.   }
  102.   else {
  103.     digitalWrite(pin3, LOW);
  104.   }
  105.  
  106.   // give the poor little Arduino a rest
  107.   delay(200);
  108.  
  109.   // let me know the thing is actually working (flash on/off every second)
  110.   if ( (timer / 1000)%2 == 0 ) {
  111.     digitalWrite(indicatorLed, HIGH);
  112.   }
  113.   else {
  114.     digitalWrite(indicatorLed, LOW);
  115.   }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement