coltonspastebin

Timer and Stopwatch

Feb 16th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. #include <Time.h>
  3. #include <Wire.h>
  4. #include "LedControl.h"
  5. int buttonPin1 = 4;
  6. int buttonPin2 = 3;
  7. int buttonPin3 = 2;
  8. int buzzerPin = 13;
  9.  
  10. int din = 16;
  11. int cs = 15;
  12. int clk = 14;
  13.  
  14. unsigned long delaytime1=50;
  15. unsigned long delaytime2=50;
  16.  
  17. int tSeconds = 10, tMinutes = 0, hours = 0;
  18. int centiseconds = 0, sSeconds = 0, sMinutes = 0;
  19.  
  20. int button1Counter = 0;//allows the buttons to have multiple operations
  21. int button1State = 0;
  22. int lastButton1State = 0;
  23. int button2Counter = 0;
  24. int button2State = 0;
  25. int lastButton2State = 0;
  26.  
  27. LiquidCrystal lcd (7,8,9,10,11,12);
  28. LedControl lc= LedControl(16,15,14,1);
  29.  
  30.  
  31. void columns() {
  32. for(int col=0;col<8;col++) {
  33. delay(delaytime2);
  34. lc.setColumn(0,col,B10100000);
  35. delay(delaytime2);
  36. lc.setColumn(0,col,(byte)0);
  37. for(int i=0;i<col;i++) {
  38. delay(delaytime2);
  39. lc.setColumn(0,col,B10100000);
  40. delay(delaytime2);
  41. lc.setColumn(0,col,(byte)0);
  42. }
  43. }
  44. }
  45. void setup()
  46. {
  47. lcd.begin (16,2);
  48. pinMode (buttonPin1, INPUT_PULLUP);
  49. pinMode (buttonPin2, INPUT_PULLUP);
  50. pinMode (buttonPin3, INPUT_PULLUP);
  51. pinMode (buzzerPin, OUTPUT);
  52. lc.shutdown(0, false);
  53. lc.setIntensity(0,8);
  54. lc.clearDisplay(0);
  55. Serial.begin (9600);
  56. }
  57.  
  58. void loop()
  59. {
  60. button1State = digitalRead (buttonPin1);//this section gives the "mode button the ability to have three seperate operations
  61. Serial.println(button1State);
  62. if (button1State != lastButton1State)
  63. {
  64. if (button1State = HIGH)
  65. {
  66. button1Counter++;
  67. button2Counter = 1;
  68. delay (200);
  69. }
  70. }
  71. lastButton1State = button1State;
  72. if (button1Counter > 3)
  73. {
  74. button1Counter = 1;
  75. }
  76. button2State = digitalRead (buttonPin2);//"start/stop" button the ability to have two seperate functions
  77. if (button2State != lastButton2State)
  78. {
  79. if (button2State = HIGH)
  80. {
  81. button2Counter++;
  82. delay (200);
  83. }
  84. }
  85. lastButton2State = button2State;
  86. if (button2Counter > 2)
  87. {
  88. button2Counter = 1;
  89. }
  90. switch (button1Counter)
  91. {
  92. case 1:
  93. lcd.clear();
  94. lcd.setCursor (2,1);
  95. lcd.print(":");
  96. lcd.setCursor(5, 1);
  97. lcd.print(":");
  98. timerFunction();
  99. break;
  100. case 2: //if "Mode" is pressed again, the clock switches once more to stopwatchFunction()
  101. lcd.clear();
  102. lcd.setCursor(2, 1);
  103. lcd.print(":");
  104. lcd.setCursor(5, 1);
  105. lcd.print(".");
  106. stopwatchFunction();
  107. break;
  108. }
  109. }
  110.  
  111. void timerFunction()
  112. {
  113. if(button2Counter == 2) //if "Start/Stop" is pressed, the timer counts down
  114. {
  115. static unsigned long lastTick = 0;
  116. if (tSeconds > 0)
  117. {
  118. if (millis() - lastTick >= 1000)
  119. {
  120. lastTick = millis();
  121. tSeconds--;
  122. lcdOutput();
  123. }
  124. }
  125. if (tMinutes > 0)
  126. {
  127. if (tSeconds <= 0)
  128. {
  129. tMinutes--;
  130. tSeconds = 60;
  131. }
  132. }
  133. if (hours > 0)
  134. {
  135. if (tMinutes <= 0)
  136. {
  137. hours--;
  138. tMinutes = 60;
  139. }
  140. }
  141. }
  142. else //if "Start/Stop" is unpressed or pressed a second time, display the current time, but don't count down
  143. {
  144. lcdOutput();
  145. }
  146. if(hours == 00 && tMinutes == 00 && tSeconds == 00) //when timer ends, the alarm goes off
  147. {
  148. button2Counter = 1;
  149. columns();
  150. while(digitalRead(buttonPin3) == HIGH)//the alarm will only go off until "Restart" is pressed
  151.  
  152. {
  153. lcd.setCursor(0, 1);
  154. lcd.print("00:00:00");
  155. digitalWrite(buzzerPin, HIGH);
  156. delay(100);
  157. digitalWrite(buzzerPin, LOW);
  158. delay(100);
  159. }
  160. if(digitalRead(buttonPin3) == LOW) //when "Restart" is pressed, the timer resets
  161. {
  162.  
  163. hours = 0; //this part also
  164. tMinutes = 0;
  165. tSeconds = 10;
  166. lcdOutput();
  167. }
  168. }
  169. if(digitalRead(buttonPin3) == LOW && button2Counter == 1) //resets the timer when "Restart" button is pressed, as long as the timer is NOT running
  170. {
  171. hours = 0; //this part also must be changed when the timer is altered, to match the initial time
  172. tMinutes = 0;
  173. tSeconds = 10;
  174. delay(10);
  175. }
  176. }
  177.  
  178. void stopwatchFunction()
  179. {
  180. lcd.setCursor(6, 1);
  181. printDigits(centiseconds);
  182. lcd.setCursor(3, 1);
  183. printDigits(sSeconds);
  184. lcd.setCursor(0, 1);
  185. printDigits(sMinutes);
  186. if(button2Counter == 2) //if the "Start/Stop" button is pressed, the time begins running
  187. {
  188. delay(5);
  189. centiseconds++;
  190. }
  191. if(digitalRead(buttonPin3) == LOW && button2Counter == 1) //if the "Restart" button is pressed, as long as the timer is paused, the stopwatch resets to 00:00.00
  192. {
  193. centiseconds = 0;
  194. sSeconds = 0;
  195. sMinutes = 0;
  196. }
  197. if(centiseconds == 100)
  198. {
  199. centiseconds = 0;
  200. sSeconds++;
  201. }
  202. if(sSeconds == 60)
  203. {
  204. sSeconds = 0;
  205. sMinutes++;
  206. }
  207. }
  208.  
  209. void lcdOutput()
  210. {
  211. lcd.setCursor(0, 1);
  212. printDigits(hours);
  213. lcd.setCursor(3, 1);
  214. printDigits(tMinutes);
  215. lcd.setCursor(6, 1);
  216. printDigits(tSeconds);
  217. delay(100);
  218. }
  219.  
  220. void printDigits(int digits)
  221. {
  222. if(digits < 10)
  223. {
  224. lcd.print("0");
  225. lcd.print(digits);
  226. }
  227. else
  228. {
  229. lcd.print(digits);
  230. }
  231. }
Add Comment
Please, Sign In to add comment