Advertisement
Guest User

Untitled

a guest
Jul 18th, 2012
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. // Binary clock for a 4x4 LED matrix
  2.  
  3. #include <Time.h>
  4.  
  5. uint8_t columns[4] = {13,12,11,10};
  6. uint8_t rows[4] = {9,8,7,6};
  7. uint8_t matrix[4][4] = {
  8. { 0, 0, 0, 0 },
  9. { 0, 0, 0, 0 },
  10. { 0, 0, 0, 0 },
  11. { 0, 0, 0, 0 }
  12. };
  13.  
  14. void setup() {
  15. // Init the output pins
  16. for (int i = 0; i < 4; i++) {
  17. pinMode(rows[i], OUTPUT);
  18. }
  19. for (int i = 0; i < 4; i++) {
  20. pinMode(columns[i], OUTPUT);
  21. }
  22.  
  23. setClock(17,12); // Set the current time here
  24.  
  25. }
  26.  
  27. void loop() {
  28.  
  29. setMatrix(); // Sets the matrix arrays to the current time in binary
  30. showMatrix(); // Does the multiplexing for the LED matrix
  31.  
  32. }
  33.  
  34. void showMatrix() {
  35. // Parse all rows
  36. for (int x = 0; x < 4; x++) {
  37. // Set previous row to 0 and
  38. // matrix value on the corresponding pin
  39. for (int y = 0; y < 4; y++) {
  40. digitalWrite(rows[y], !matrix[x][y]);
  41. }
  42. // Set current column to 1
  43. digitalWrite(columns[x], 1);
  44. delay(1);
  45. // Delete current column
  46. digitalWrite(columns[x], 0);
  47. }
  48.  
  49. }
  50.  
  51. void setMatrix() {
  52.  
  53. int munit = minute()%10; //sets the variable munit and hunit for the unit digits
  54. int hunit = hour()%10;
  55.  
  56. //minutes units
  57. if(munit == 1 || munit == 3 || munit == 5 || munit == 7 || munit == 9) { matrix[3][3] = 1; } else { matrix[3][3] = 0; }
  58. if(munit == 2 || munit == 3 || munit == 6 || munit == 7) { matrix[2][3] = 1; } else { matrix[2][3] = 0; }
  59. if(munit == 4 || munit == 5 || munit == 6 || munit == 7) { matrix[1][3] = 1; } else { matrix[1][3] = 0; }
  60. if(munit == 8 || munit == 9) { matrix[0][3] = 1;} else { matrix[0][3] = 0; }
  61.  
  62. //minutes
  63. if((minute() >= 10 && minute() < 20) || (minute() >= 30 && minute() < 40) || (minute() >= 50 && minute() < 60)) { matrix[3][2] = 1; } else {matrix[3][2] = 0;}
  64. if(minute() >= 20 && minute() < 40) {matrix[2][2] = 1;} else {matrix[2][2] = 0;}
  65. if(minute() >= 40 && minute() < 60) {matrix[1][2] = 1;} else {matrix[1][2] = 0;}
  66.  
  67. //hour units
  68. if(hunit == 1 || hunit == 3 || hunit == 5 || hunit == 7 || hunit == 9) {matrix[3][1] = 1;} else {matrix[3][1] = 0;}
  69. if(hunit == 2 || hunit == 3 || hunit == 6 || hunit == 7) {matrix[2][1] = 1;} else {matrix[2][1] = 0;}
  70. if(hunit == 4 || hunit == 5 || hunit == 6 || hunit == 7) {matrix[1][1] = 1;} else {matrix[1][1] = 0;}
  71. if(hunit == 8 || hunit == 9) {matrix[0][1] = 1;} else {matrix[0][1] = 0;}
  72.  
  73. //hour
  74. if(hour() >= 10 && hour() < 20) {matrix[3][0] = 1;} else {matrix[3][0] = 0;}
  75. if(hour() >= 20 && hour() < 24) {matrix[2][0] = 1;} else {matrix[2][0] = 0;}
  76.  
  77. }
  78.  
  79. void setClock(int hour, int minute) { // Helper method to set time more easily (seconds and date can be ignored for this program).
  80.  
  81. setTime(hour,minute,00,00,00,2000);
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement