Advertisement
x2Jiggy

LED Binary Clock Sketch Arduino

Jun 15th, 2014
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. // LED Binary clock by x2Jiggy.com 11/06/2014
  2.  
  3. /* Sketch queries a DS1307 RTC for the current time then uses
  4. those values to switch an array of LEDs to display the time in
  5. binary format */
  6.  
  7. #include <Wire.h> // Communicate with the RTC over I2C
  8. #include "RTClib.h" // Adafruit DS1307 Real Time Clock Library
  9.  
  10. /* Static Variables */
  11. static int h1 = A0;
  12. static int h2 = 2;
  13. static int h3 = 3;
  14. static int h4 = 4;
  15. static int h5 = 5;
  16. static int h6 = 6;
  17.  
  18. static int m1 = 7;
  19. static int m2 = 8;
  20. static int m3 = 9;
  21. static int m4 = 10;
  22. static int m5 = 11;
  23. static int m6 = 12;
  24. static int m7 = 13;
  25.  
  26. RTC_DS1307 RTC;
  27.  
  28. void setup () {
  29.  
  30. /* Set inputs and outputs */
  31. pinMode(h1, OUTPUT);
  32. pinMode(h2, OUTPUT);
  33. pinMode(h3, OUTPUT);
  34. pinMode(h4, OUTPUT);
  35. pinMode(h5, OUTPUT);
  36. pinMode(h6, OUTPUT);
  37.  
  38. pinMode(m1, OUTPUT);
  39. pinMode(m2, OUTPUT);
  40. pinMode(m3, OUTPUT);
  41. pinMode(m4, OUTPUT);
  42. pinMode(m5, OUTPUT);
  43. pinMode(m6, OUTPUT);
  44. pinMode(m7, OUTPUT);
  45.  
  46. /* Start 2IC and RTC */
  47. //Serial.begin(9600);
  48. Wire.begin();
  49. RTC.begin();
  50.  
  51. }
  52.  
  53. void loop () {
  54.  
  55. /* Get current time */
  56.  
  57. DateTime now = RTC.now();
  58. int tHour = now.hour();
  59. int tMin = now.minute();
  60.  
  61. /* Set First Hour Digit */
  62.  
  63. if (tHour < 10){
  64. digitalWrite(h1, LOW);
  65. digitalWrite(h2, LOW);
  66. }
  67. else if (tHour < 20) {
  68. digitalWrite(h1, HIGH);
  69. digitalWrite(h2, LOW);
  70. }
  71. else {
  72. digitalWrite(h1, LOW);
  73. digitalWrite(h2, HIGH);
  74. }
  75.  
  76. /* Set Second Hour Digit */
  77.  
  78. // Reduce Hour int to second digit only
  79. int hDigit;
  80. if (tHour > 20) hDigit = tHour - 20;
  81. else if (tHour > 10) hDigit = tHour - 10;
  82. else hDigit = tHour;
  83.  
  84. // Check digit against binary value of each LED and switch accordingly
  85. if (hDigit != 0 && hDigit != 10) {
  86.  
  87. if (hDigit >= 8) {
  88. hDigit = hDigit - 8;
  89. digitalWrite(h6, HIGH);
  90. }
  91. else digitalWrite(h6, LOW);
  92.  
  93. if (hDigit >= 4) {
  94. hDigit = hDigit - 4;
  95. digitalWrite(h5, HIGH);
  96. }
  97. else digitalWrite(h5, LOW);
  98.  
  99. if (hDigit >= 2) {
  100. hDigit = hDigit - 2;
  101. digitalWrite(h4, HIGH);
  102. }
  103. else digitalWrite(h4, LOW);
  104.  
  105. if (hDigit >= 1) digitalWrite(h3, HIGH);
  106. else digitalWrite(h3, LOW);
  107.  
  108. }
  109. else {
  110. digitalWrite(h6, LOW);
  111. digitalWrite(h5, LOW);
  112. digitalWrite(h4, LOW);
  113. digitalWrite(h3, LOW);
  114. }
  115.  
  116. /* Set First Minute Digit */
  117.  
  118. if (tMin >= 10) {
  119.  
  120. int mDigit1 = tMin - (tMin % 10); // Remove remainder making value divisible by 10
  121. mDigit1 = mDigit1 / 10; // Divide by 10 to get first minute digit
  122.  
  123. if (mDigit1 >= 4) {
  124. mDigit1 = mDigit1 - 4;
  125. digitalWrite(m3, HIGH);
  126. }
  127. else digitalWrite(m3, LOW);
  128.  
  129. if (mDigit1 >= 2) {
  130. mDigit1 = mDigit1 - 2;
  131. digitalWrite(m2, HIGH);
  132. }
  133. else digitalWrite(m2, LOW);
  134.  
  135. if (mDigit1 >= 1) digitalWrite(m1, HIGH);
  136. else digitalWrite(m1, LOW);
  137.  
  138. }
  139. else {
  140. digitalWrite(m1, LOW);
  141. digitalWrite(m2, LOW);
  142. digitalWrite(m3, LOW);
  143. }
  144.  
  145. /* Set Second Minute Digit */
  146.  
  147. int mDigit2; // Reduce minute int to second digit only
  148. if (tMin > 50) mDigit2 = tMin - 50;
  149. else if (tMin > 40) mDigit2 = tMin - 40;
  150. else if (tMin > 30) mDigit2 = tMin - 30;
  151. else if (tMin > 20) mDigit2 = tMin - 20;
  152. else if (tMin > 10) mDigit2 = tMin - 10;
  153. else mDigit2 = tMin;
  154.  
  155. // Check digit against binary value of each LED and switch accordingly
  156.  
  157. if (mDigit2 != 0 && mDigit2 != 10) {
  158.  
  159. if (mDigit2 >= 8) {
  160. mDigit2 = mDigit2 - 8;
  161. digitalWrite(m7, HIGH);
  162. }
  163. else digitalWrite(m7, LOW);
  164.  
  165. if (mDigit2 >= 4) {
  166. mDigit2 = mDigit2 - 4;
  167. digitalWrite(m6, HIGH);
  168. }
  169. else digitalWrite(m6, LOW);
  170.  
  171. if (mDigit2 >= 2) {
  172. mDigit2 = mDigit2 - 2;
  173. digitalWrite(m5, HIGH);
  174. }
  175. else digitalWrite(m5, LOW);
  176.  
  177. if (mDigit2 >= 1) digitalWrite(m4, HIGH);
  178. else digitalWrite(m4, LOW);
  179.  
  180. }
  181. else {
  182. digitalWrite(m4, LOW);
  183. digitalWrite(m5, LOW);
  184. digitalWrite(m6, LOW);
  185. digitalWrite(m7, LOW);
  186. }
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement