Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. #include <LedControl.h> // Library for LED control with MAX72XX
  2. #include <Bounce2.h> // Library for Bounce of switches
  3.  
  4. /*
  5. Pins of Arduino Nano for LedControl:
  6. Pin #7 is connected to DataIn (DIN)
  7. Pin #8 is connected to CLK (CLK)
  8. Pin #9 is connected to LOAD (CS)
  9. There is only one display with MAX72XX
  10. */
  11. Bounce SW2 = Bounce(); // Define Bounce to read StartStop switch
  12. Bounce SW1 = Bounce(); // Define Bounce to read StartStop switch
  13. LedControl lc = LedControl(7, 8, 9, 1); // LedControl(dataPin, clkPin, csPin, numDevices)
  14. int pinStartStop = 4; // Start-Stop Pin
  15. int pin2 = 5;
  16.  
  17.  
  18. bool setting = true;
  19. int curPos = 0;
  20. int switch_counter = 0;
  21. unsigned long current_seconds = 0;
  22.  
  23.  
  24. int hours_dec = 0;
  25. int hours_int = 0;
  26. int minutes_dec = 0;
  27. int minutes_int = 0;
  28. int seconds_dec = 0;
  29. int seconds_int = 0;
  30.  
  31. void setup() {
  32. Serial.begin(9600);
  33.  
  34. pinMode (pinStartStop, INPUT_PULLUP);
  35. pinMode (pin2, INPUT_PULLUP);
  36. lc.shutdown(0, false); // The MAX72XX is in power-saving mode on startup, we have to do a wakeup call
  37. lc.setIntensity(0, 2); // Set the brightness of display between 0 and 15
  38. lc.clearDisplay(0); // Clear the display
  39. //print_milis(0);
  40. Serial.begin(9600);
  41. pinMode (pinStartStop, INPUT_PULLUP);
  42. SW1.attach(pinStartStop); // Sets the pin (Internal Pull-Up) & matches the internal state
  43. SW1.interval(3); // Sets the debounce time in milliseconds
  44. pinMode(3, OUTPUT); // sets the digital pin 3 as output
  45. SW2.attach(pin2); // Sets the pin (Internal Pull-Up) & matches the internal state
  46. SW2.interval(2); // Sets the debounce time in milliseconds
  47. pinMode(2, OUTPUT); // sets the digital pin 2 as output
  48.  
  49. }
  50.  
  51. void setTime(int hours, int minutes, int seconds){
  52. lc.setDigit(0, 7, 0, false);
  53. lc.setDigit(0, 6, 0, false);
  54. lc.setDigit(0, 5, hours / 10, false);
  55. lc.setDigit(0, 4, hours % 10, true);
  56.  
  57.  
  58. lc.setDigit(0,3, minutes / 10, false);
  59. lc.setDigit(0,2, minutes % 10, true);
  60. lc.setDigit(0,1, seconds / 10, false);
  61. lc.setDigit(0,0,seconds % 10, false);
  62. }
  63.  
  64. void loop() {
  65.  
  66.  
  67. while(true){
  68.  
  69.  
  70.  
  71.  
  72. // SETTING UP TIME
  73.  
  74. while(setting){
  75.  
  76. SW1.update();
  77. SW2.update();
  78.  
  79.  
  80.  
  81. if(SW1.fell()){
  82. switch_counter++;
  83. if(curPos < 5)
  84. curPos++;
  85. else
  86. {
  87. curPos = 0;
  88. }
  89.  
  90. }
  91.  
  92. if(SW2.fell()){
  93. switch_counter = 0;
  94. switch(curPos){
  95.  
  96. case 0:
  97. {
  98. if (seconds_int < 9){
  99. seconds_int++;}
  100. else
  101. seconds_int = 0;
  102.  
  103. break;
  104. }
  105.  
  106. case 1:
  107. {
  108. if (seconds_dec < 5){
  109. seconds_dec++;}
  110. else
  111. seconds_dec = 0;
  112.  
  113. break;
  114. }
  115. case 2:
  116. {
  117. if (minutes_int < 9){
  118. minutes_int++;}
  119. else
  120. minutes_int = 0;
  121.  
  122. break;
  123. }
  124. case 3:
  125. {
  126. if (minutes_dec < 5){
  127. minutes_dec++;}
  128. else
  129. minutes_dec = 0;
  130.  
  131. break;
  132. }
  133. case 4:
  134. {
  135. if (hours_int < 9 && hours_dec < 2){
  136. hours_int++;}
  137. else if (hours_int < 3 && hours_dec == 2){
  138. hours_int++;}
  139. else
  140. hours_int = 0;
  141.  
  142. break;
  143. }
  144. case 5:
  145. {
  146. if (hours_dec < 2){
  147. hours_dec++;}
  148. else
  149. hours_dec = 0;
  150.  
  151. break;
  152. }
  153. }
  154. }
  155.  
  156.  
  157. setTime(hours_dec * 10 + hours_int, minutes_dec * 10 + minutes_int, seconds_dec * 10 + seconds_int);
  158. if(switch_counter > 6)
  159. {
  160. current_seconds = seconds_int + seconds_dec * 10 + minutes_int * 60 + minutes_dec * 600 + hours_int * 3600 + hours_dec * 36000;
  161. setting = false;
  162. switch_counter=0;
  163. }
  164.  
  165.  
  166. }
  167.  
  168. while(true){
  169. current_seconds++;
  170. if (current_seconds > 86399)
  171. current_seconds = 0;
  172.  
  173. unsigned long hours = current_seconds/3600;
  174. unsigned long minutes = (current_seconds - hours * 3600) / 60;
  175. unsigned long seconds = current_seconds - hours * 3600 - minutes * 60;
  176. Serial.println(current_seconds);
  177. Serial.println(hours);
  178. Serial.println(minutes);
  179. Serial.println(seconds);
  180. Serial.println("---------------");
  181. SW1.update();
  182. if(SW1.fell())
  183. {
  184. setting=1;
  185. break;
  186. }
  187.  
  188.  
  189. setTime(hours, minutes, seconds);
  190. delay(998);
  191. }
  192.  
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement