Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. #include <TM1638.h>
  2. #include <Timer.h>
  3.  
  4. bool enabled = false; // counting is enabled or not
  5. byte counter = 0; // the counter (for main loop, counter max = 1000 msec / delay)
  6. byte hours = 0, minutes = 0, seconds = 0; // trivial
  7. bool btnpressed = false; // using to on/off and brightness switch don't repeat if pressed
  8. byte animation = 0; // animations' counter
  9. byte brightness = 0; // brightness of the display (0..7)
  10. bool DoIt = false;
  11. int8_t clockEvent;
  12.  
  13. // define a regular module
  14. TM1638 module(3,4,5); // data, clock, strobe
  15. Timer t;
  16.  
  17. // initial setup when arduino starts
  18. void setup() {
  19. randomSeed(analogRead(0));
  20. // initialize UV tube relay (turn off)
  21. pinMode(13, OUTPUT);
  22. TubeEnabled(false);
  23. // initialize the timer to call clockHeartBeat function in every seconds
  24. clockEvent = t.every(1000, clockHeartBeat);
  25. // intialize TM1638 module
  26. module.setupDisplay(true, brightness);
  27. Initialize();
  28. }
  29.  
  30. // initialize display and variables before use
  31. void Initialize() {
  32. enabled = false;
  33. // test all LEDs and segments
  34. writeByteToLEDs(255);
  35. writeStrToDisplay("88888888");
  36. delay(1000);
  37. writeByteToLEDs(0);
  38. writeStrToDisplay(" ");
  39. }
  40.  
  41. // turn on of orr then output (status LED and relay)
  42. void TubeEnabled(bool state) {
  43. if (state == false) {
  44. digitalWrite(13, LOW);
  45. } else {
  46. digitalWrite(13, HIGH);
  47. }
  48. }
  49.  
  50. // show the countdowning (format: hh-mm-ss)
  51. void writeTimeToDisplay() {
  52. char s[8];
  53. sprintf(s, "%02d-%02d-%02d", hours, minutes, seconds);
  54. writeStrToDisplay(s);
  55. }
  56.  
  57. // write 8 char to the display
  58. void writeStrToDisplay(char s[8]) {
  59. module.setDisplayToString(s);
  60. }
  61.  
  62. // set output LEDs on or off
  63. void writeByteToLEDs(byte led) {
  64. module.setLEDs(led);
  65. }
  66.  
  67. // main procedure when hours, minutes and seconds reached the bottom
  68. void TimeIsOver() {
  69. byte buttons = 0;
  70. writeByteToLEDs(0);
  71. InitializeAnimation();
  72.  
  73. while (buttons == 0) {
  74. DoAnimation_HorizontalSnake();
  75. delay(50);
  76. buttons = module.getButtons();
  77. }
  78.  
  79. // reinitialize the system
  80. Initialize();
  81. }
  82.  
  83. // initialize animations' counter
  84. void InitializeAnimation() {
  85. animation = 0;
  86. }
  87.  
  88. // animation: ___
  89. void DoAnimation_Circle() {
  90. byte values[8] = {0,0,0,0,0,0,0,0};
  91. switch (animation) {
  92. case 0: values[0] = 1; break;
  93. case 1: values[1] = 1; break;
  94. case 2: values[2] = 1; break;
  95. case 3: values[3] = 1; break;
  96. case 4: values[4] = 1; break;
  97. case 5: values[5] = 1; break;
  98. case 6: values[6] = 1; break;
  99. case 7: values[7] = 1; break;
  100. case 8: values[7] = 2; break;
  101. case 9: values[7] = 4; break;
  102. case 10: values[7] = 8; break;
  103. case 11: values[6] = 8; break;
  104. case 12: values[5] = 8; break;
  105. case 13: values[4] = 8; break;
  106. case 14: values[3] = 8; break;
  107. case 15: values[2] = 8; break;
  108. case 16: values[1] = 8; break;
  109. case 17: values[0] = 8; break;
  110. case 18: values[0] = 16; break;
  111. case 19: values[0] = 32; break;
  112. }
  113. module.setDisplay(values);
  114.  
  115. animation++;
  116. if (animation > 19) {
  117. animation = 0;
  118. }
  119. }
  120.  
  121. // animation: ___
  122. void DoAnimation_DoubleDouble() {
  123. byte values[8] = {0,0,0,0,0,0,0,0};
  124. switch (animation) {
  125. case 0: values[0] = 48; values[7] = 6; break;
  126. case 1: values[0] = 9; values[7] = 9; break;
  127. case 2: values[1] = 9; values[6] = 9; break;
  128. case 3: values[2] = 9; values[5] = 9; break;
  129. case 4: values[3] = 9; values[4] = 9; break;
  130. case 5: values[3] = 6; values[4] = 48; break;
  131. case 6: values[3] = 64; values[4] = 64; break;
  132. case 7: values[2] = 64; values[5] = 64; break;
  133. case 8: values[1] = 64; values[6] = 64; break;
  134. case 9: values[0] = 64; values[7] = 64; break;
  135. }
  136. module.setDisplay(values);
  137.  
  138. animation++;
  139. if (animation > 9) {
  140. animation = 0;
  141. }
  142. }
  143.  
  144. // animation: ___
  145. void DoAnimation_HorizontalSnake() {
  146. byte values[8] = {0,0,0,0,0,0,0,0};
  147. switch (animation) {
  148. case 0: values[0] = 8; break;
  149. case 1: values[1] = 8; break;
  150. case 2: values[2] = 8; break;
  151. case 3: values[3] = 8; break;
  152. case 4: values[4] = 8; break;
  153. case 5: values[5] = 8; break;
  154. case 6: values[6] = 8; break;
  155. case 7: values[7] = 8; break;
  156. case 8: values[7] = 4; break;
  157. case 9: values[7] = 64; break;
  158. case 10: values[6] = 64; break;
  159. case 11: values[5] = 64; break;
  160. case 12: values[4] = 64; break;
  161. case 13: values[3] = 64; break;
  162. case 14: values[2] = 64; break;
  163. case 15: values[1] = 64; break;
  164. case 16: values[0] = 64; break;
  165. case 17: values[0] = 32; break;
  166. case 18: values[0] = 1; break;
  167. case 19: values[1] = 1; break;
  168. case 20: values[2] = 1; break;
  169. case 21: values[3] = 1; break;
  170. case 22: values[4] = 1; break;
  171. case 23: values[5] = 1; break;
  172. case 24: values[6] = 1; break;
  173. case 25: values[7] = 1; break;
  174. case 26: values[7] = 2; break;
  175. case 27: values[7] = 64; break;
  176. case 28: values[6] = 64; break;
  177. case 29: values[5] = 64; break;
  178. case 30: values[4] = 64; break;
  179. case 31: values[3] = 64; break;
  180. case 32: values[2] = 64; break;
  181. case 33: values[1] = 64; break;
  182. case 34: values[0] = 64; break;
  183. case 35: values[0] = 16; break;
  184. }
  185. module.setDisplay(values);
  186.  
  187. animation++;
  188. if (animation > 35) {
  189. animation = 0;
  190. }
  191. }
  192.  
  193. void DoAnimation_RandomLines() {
  194. byte values[8] = {0,0,0,0,0,0,0,0};
  195. switch (random(7)) {
  196. case 0: values[random(8)] = 1; break;
  197. case 1: values[random(8)] = 2; break;
  198. case 2: values[random(8)] = 4; break;
  199. case 3: values[random(8)] = 8; break;
  200. case 4: values[random(8)] = 16; break;
  201. case 5: values[random(8)] = 32; break;
  202. case 6: values[random(8)] = 64; break;
  203. }
  204. module.setDisplay(values);
  205. }
  206.  
  207. // heartbeat for the clock signal
  208. void clockHeartBeat() {
  209. DoIt = true;
  210. }
  211.  
  212. // the main loop
  213. void loop() {
  214. byte buttons = module.getButtons();
  215. // handling the buttons
  216. if (buttons != 0) {
  217. byte abutton = buttons >> 1;
  218. switch (abutton) {
  219. case 0: // start
  220. if (btnpressed == false) {
  221. if (enabled == true) {
  222. enabled = false;
  223. DoIt = false;
  224. } else {
  225. enabled = true;
  226. DoIt = false;
  227. t.update(clockEvent);
  228. }
  229. TubeEnabled(enabled);
  230. writeByteToLEDs(1);
  231. }
  232. btnpressed = true;
  233. break;
  234. case 1:
  235. if (btnpressed == false) {
  236. brightness++;
  237. if (brightness > 7) {
  238. brightness = 0;
  239. }
  240. module.setupDisplay(true, brightness);
  241. writeByteToLEDs(2);
  242. }
  243. btnpressed = true;
  244. break;
  245. case 2:
  246. if (hours > 0) {
  247. hours--;
  248. }
  249. writeByteToLEDs(4);
  250. break;
  251. case 4:
  252. if (hours < 99) {
  253. hours++;
  254. }
  255. writeByteToLEDs(8);
  256. break;
  257. case 8:
  258. if (minutes > 0) {
  259. minutes--;
  260. }
  261. writeByteToLEDs(16);
  262. break;
  263. case 16:
  264. if (minutes < 59) {
  265. minutes++;
  266. }
  267. writeByteToLEDs(32);
  268. break;
  269. case 32:
  270. if (seconds > 0) {
  271. seconds--;
  272. }
  273. writeByteToLEDs(64);
  274. break;
  275. case 64:
  276. if (seconds < 59) {
  277. seconds++;
  278. }
  279. writeByteToLEDs(128);
  280. break;
  281. }
  282. } else {
  283. btnpressed = false;
  284. }
  285.  
  286. // if counting is enabled
  287. if ((enabled == true) && (DoIt == true)) {
  288. DoIt = false;
  289. if (seconds > 0) {
  290. seconds--;
  291. } else {
  292. if (minutes > 0) {
  293. minutes--;
  294. seconds = 59;
  295. } else {
  296. if (hours > 0) {
  297. hours--;
  298. minutes = 59;
  299. seconds = 59;
  300. } else {
  301. // if hours, minutes and seconds reached the bottom
  302. hours = 0;
  303. minutes = 0;
  304. seconds = 0;
  305. enabled = false;
  306. TubeEnabled(false);
  307. // play animation while any button will be pressed
  308. TimeIsOver();
  309. }
  310. }
  311. }
  312. }
  313.  
  314. // always show time in the main loop during normal operation
  315. writeTimeToDisplay();
  316.  
  317. // if a button has been pressed, turn off the attached LED
  318. if (buttons != 0) {
  319. writeByteToLEDs(0);
  320. }
  321.  
  322. // updating the timer
  323. if (enabled == true) {
  324. t.update();
  325. }
  326.  
  327. // delaying the main loop, debouncing button presses, etc.
  328. delay(100);
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement