Advertisement
Guest User

Herätyskello

a guest
Sep 30th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. /*Kommentit:
  2. Koodia voisi vähentää ja funktioita, mutta toistaiseksi pysyy tällaisena, voi siinä muutamat funtiot sitten yhdistää myöhemmin.
  3. */
  4. #include <LiquidCrystal.h>
  5. volatile boolean kytkin;
  6. long previousTime = 0;
  7. long previousTime2 = 0;
  8. long previousTime3 = 0;
  9. long previousTime4 = 0;
  10. long previousTime5 = 0;
  11. long previousTime6 = 0;
  12. long interval = 1000;
  13. long interval2 = 350;
  14. int sekunnit = 0;
  15. int tunnit = 12;
  16. int minuutit = 35;
  17. int halytystunnit = 0;
  18. int halytysminuutit = 0;
  19. const int ledPin = 6;
  20. const int buttonPin2 = 4;
  21. const int buttonPin1 = 9;
  22. const int buttonPin3 = 10;
  23. const int buttonPin4 = 2;
  24. int buttonState1 = 0;
  25. int buttonState3 = 0;
  26. int buttonState2 = 0;
  27. int buttonState4 = 0;
  28. LiquidCrystal lcd(13, 11, 5, 8, 3, 7);
  29.  
  30. void setup() {
  31. // put your setup code here, to run once:
  32. lcd.begin(16, 2);
  33. pinMode(ledPin, OUTPUT);
  34. pinMode(buttonPin1, INPUT);
  35. pinMode(buttonPin2, INPUT);
  36. pinMode(buttonPin3, INPUT);
  37. pinMode(buttonPin4, INPUT);
  38. attachInterrupt(0, modi, FALLING);
  39. sei();
  40. Serial.begin(9600);
  41. }
  42.  
  43. void loop() {
  44. // put your main code here, to run repeatedly:
  45. buttonState2 = digitalRead(buttonPin2);
  46. buttonState1 = digitalRead(buttonPin1);
  47. buttonState3 = digitalRead(buttonPin3);
  48. buttonState4 = digitalRead(buttonPin4);
  49. paivita_aika();
  50. aseta_halytys();
  51. halytys();
  52. lopeta_halytys();
  53. tarkista_aika();
  54. nayta_halytys();
  55. aseta_aika();
  56. nayta_aika();
  57. }
  58.  
  59. void paivita_aika() {
  60.  
  61. unsigned long currentTime = millis();
  62. if (currentTime - previousTime > interval) {
  63. previousTime = currentTime;
  64. sekunnit++;
  65. }
  66. }
  67.  
  68. void nayta_aika() {
  69. unsigned long currentTime = millis();
  70.  
  71. if (currentTime - previousTime5 > interval2) {
  72. previousTime5 = currentTime;
  73. lcd.setCursor(0, 0);
  74. lcd.print("Time: ");
  75. if (tunnit < 10) {
  76. lcd.print(0);
  77. }
  78. lcd.print(tunnit);
  79. lcd.print(":");
  80. if (minuutit < 10) {
  81. lcd.print(0);
  82. }
  83. lcd.print(minuutit);
  84. }
  85. }
  86.  
  87. void tarkista_aika() {
  88. if (tunnit == 24) {
  89. tunnit = 0;
  90. }
  91. if (minuutit == 60) {
  92. minuutit = 0;
  93. tunnit++;
  94. }
  95. if (sekunnit == 60) {
  96. sekunnit = 0;
  97. minuutit++;
  98. }
  99. }
  100.  
  101. void aseta_halytys() {
  102.  
  103. unsigned long currentTime = millis();
  104.  
  105. if (currentTime - previousTime2 > interval2) {
  106. previousTime2 = currentTime;
  107.  
  108. if (buttonState2 == LOW && buttonState3 == LOW && kytkin == true) {
  109. halytysminuutit++;
  110. }
  111. if (buttonState2 == LOW && buttonState1 == LOW && kytkin == true) {
  112. halytysminuutit--;
  113. }
  114. if (buttonState2 == HIGH && buttonState3 == LOW && kytkin == true) {
  115. halytystunnit++;
  116. }
  117. if (buttonState2 == HIGH && buttonState1 == LOW && kytkin == true) {
  118. halytystunnit--;
  119. }
  120. if (halytysminuutit == 60) {
  121. halytysminuutit = 0;
  122. }
  123. if (halytysminuutit == -1) {
  124. halytysminuutit = 59;
  125. }
  126. if (halytystunnit == -1) {
  127. halytystunnit = 23;
  128. }
  129. if (halytystunnit == 24) {
  130. halytystunnit = 0;
  131. }
  132. }
  133. }
  134.  
  135. void nayta_halytys() {
  136. unsigned long currentTime = millis();
  137.  
  138. if (currentTime - previousTime6 > interval2) {
  139. previousTime6 = currentTime;
  140. lcd.setCursor(0, 1);
  141. lcd.print("Alarm: ");
  142. if (halytystunnit < 10) {
  143. lcd.print(0);
  144. }
  145. lcd.print(halytystunnit);
  146. lcd.print(":");
  147. if (halytysminuutit < 10) {
  148. lcd.print(0);
  149. }
  150. lcd.print(halytysminuutit);
  151. if (kytkin == true) {
  152. lcd.print("Amod");
  153.  
  154. } else {
  155. lcd.print("Tmod");
  156. }
  157. }
  158. }
  159.  
  160. void halytys() {
  161.  
  162. if (halytystunnit == tunnit && halytysminuutit == minuutit && sekunnit - 2 <= 0) {
  163. digitalWrite(ledPin, HIGH); // joku koodi joka aktivoi hälytyksen
  164. }
  165. }
  166.  
  167. void lopeta_halytys() {
  168.  
  169. if ( buttonState2 == LOW ) {
  170. digitalWrite(ledPin, LOW);
  171. }
  172. }
  173.  
  174.  
  175. void aseta_aika() {
  176.  
  177. unsigned long currentTime = millis();
  178. if (currentTime - previousTime3 > interval2) {
  179. previousTime3 = currentTime;
  180.  
  181. if (buttonState3 == LOW && buttonState2 == LOW && kytkin == false) {
  182. minuutit++;
  183. }
  184. if (buttonState1 == LOW && buttonState2 == LOW && kytkin == false) {
  185. minuutit--;
  186. }
  187. if (buttonState3 == LOW && buttonState2 == HIGH && kytkin == false) {
  188. tunnit++;
  189. }
  190. if (buttonState1 == LOW && buttonState2 == HIGH && kytkin == false) {
  191. tunnit--;
  192. }
  193. if (minuutit == 60) {
  194. minuutit = 0;
  195. }
  196. if (minuutit == -1) {
  197. minuutit = 59;
  198. }
  199. if (tunnit == -1) {
  200. tunnit = 23;
  201. }
  202. if (tunnit == 24) {
  203. tunnit = 0;
  204. }
  205. }
  206. }
  207.  
  208. void modi() {
  209. cli();
  210. unsigned long currentTime = millis();
  211. if (currentTime - previousTime4 > interval2) {
  212. previousTime4 = currentTime;
  213.  
  214. kytkin = !kytkin;
  215. Serial.println(kytkin);
  216. }
  217. sei();
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement