| 1 | {{{ |
| 2 | /* |
| 3 | * 由 11 pins 控制 8 * 8 矩陣式 led matrix |
| 4 | */ |
| 5 | |
| 6 | #define lpinA 2 // 定義 74138 A 腳位 |
| 7 | #define lpinB 3 // 定義 74138 B 腳位 |
| 8 | #define lpinC 4 // 定義 74138 C 腳位 |
| 9 | #define hpin0 5 // 定義 LED 正極腳位 |
| 10 | #define hpin1 6 // 定義 LED 正極腳位 |
| 11 | #define hpin2 7 // 定義 LED 正極腳位 |
| 12 | #define hpin3 8 // 定義 LED 正極腳位 |
| 13 | #define hpin4 9 // 定義 LED 正極腳位 |
| 14 | #define hpin5 10 // 定義 LED 正極腳位 |
| 15 | #define hpin6 11 // 定義 LED 正極腳位 |
| 16 | #define hpin7 12 // 定義 LED 正極腳位 |
| 17 | #define row_size 8 // 定義 Matrix row 大小 |
| 18 | #define col_size 8 // 定義 Matrix colum 小 |
| 19 | #define delay_time 0 // 延遲時間 |
| 20 | |
| 21 | #define data_A {0, 0, 0, 1, 1, 0, 0, 0,\ |
| 22 | 0, 0, 1, 0, 0, 1, 0, 0,\ |
| 23 | 0, 0, 1, 0, 0, 1, 0, 0,\ |
| 24 | 0, 1, 0, 0, 0, 0, 1, 0,\ |
| 25 | 0, 1, 1, 1, 1, 1, 1, 0,\ |
| 26 | 0, 1, 0, 0, 0, 0, 1, 0,\ |
| 27 | 0, 1, 0, 0, 0, 0, 1, 0,\ |
| 28 | 0, 1, 0, 0, 0, 0, 1, 0} |
| 29 | |
| 30 | #define data_N {0, 1, 0, 0, 0, 0, 1, 0,\ |
| 31 | 0, 1, 1, 0, 0, 0, 1, 0,\ |
| 32 | 0, 1, 1, 0, 0, 0, 1, 0,\ |
| 33 | 0, 1, 0, 1, 0, 0, 1, 0,\ |
| 34 | 0, 1, 0, 0, 1, 0, 1, 0,\ |
| 35 | 0, 1, 0, 0, 0, 1, 1, 0,\ |
| 36 | 0, 1, 0, 0, 0, 1, 1, 0,\ |
| 37 | 0, 1, 0, 0, 0, 0, 1, 0} |
| 38 | |
| 39 | #define data_all_led {1, 1, 1, 1, 1, 1, 1, 1,\ |
| 40 | 1, 1, 1, 0, 1, 1, 1, 1,\ |
| 41 | 1, 1, 1, 1, 1, 1, 1, 1,\ |
| 42 | 1, 1, 1, 1, 1, 1, 1, 1,\ |
| 43 | 1, 1, 0, 1, 1, 1, 1, 1,\ |
| 44 | 1, 1, 1, 1, 1, 1, 1, 1,\ |
| 45 | 1, 0, 1, 1, 1, 1, 1, 1,\ |
| 46 | 1, 1, 1, 1, 1, 1, 1, 1} |
| 47 | |
| 48 | |
| 49 | // 所有要顯示字的 array |
| 50 | boolean word_array[4][row_size][col_size] = {data_all_led}; |
| 51 | |
| 52 | // LED Matrix |
| 53 | boolean led[row_size][col_size] ; |
| 54 | // 水平方向 led |
| 55 | byte row_pin[row_size]={hpin0, hpin1, hpin2, hpin3, hpin4, hpin5, hpin6, hpin7}; |
| 56 | |
| 57 | // 初始設定 |
| 58 | void setup() |
| 59 | { |
| 60 | for (int i = 0; i < 13; i++) |
| 61 | pinMode(i, OUTPUT); |
| 62 | clear_led(); |
| 63 | } |
| 64 | |
| 65 | // 主程式迴圈 |
| 66 | void loop() |
| 67 | { |
| 68 | // HEX 000 |
| 69 | clear_led(); |
| 70 | |
| 71 | // 將第一個 word 讀入 led |
| 72 | word_to_led(0); |
| 73 | display_led(led, 1000); |
| 74 | delay(delay_time); |
| 75 | } |
| 76 | |
| 77 | // 將第 n 個 word 讀入 led |
| 78 | void word_to_led(int n){ |
| 79 | for (int i = 0; i < row_size; i++) |
| 80 | for (int j = 0; j < col_size; j++) |
| 81 | led[i][j] = word_array[n][i][j]; |
| 82 | } |
| 83 | |
| 84 | // 將 led 全關 |
| 85 | void clear_led() |
| 86 | { |
| 87 | for (int i = 0; i < row_size; i++) |
| 88 | digitalWrite(row_pin[i], LOW); |
| 89 | } |
| 90 | |
| 91 | void display_led(byte led[row_size][col_size], int continue_time) |
| 92 | { |
| 93 | for (int k = 0; k < continue_time; k++) |
| 94 | { |
| 95 | for (int i = 0; i < col_size; i++) |
| 96 | { |
| 97 | // 掃描第一個 col 減少亮度不均 |
| 98 | low_74138pin(i); |
| 99 | for (int j = 0; j < row_size; j++) |
| 100 | if (led[j][i] == 1) |
| 101 | digitalWrite(row_pin[j], HIGH); |
| 102 | clear_led(); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void low_74138pin(int num) |
| 108 | { |
| 109 | switch (num) |
| 110 | { |
| 111 | case 0: |
| 112 | digitalWrite(lpinA, LOW); |
| 113 | digitalWrite(lpinB, LOW); |
| 114 | digitalWrite(lpinC, LOW); |
| 115 | break; |
| 116 | case 1: |
| 117 | digitalWrite(lpinA, HIGH); |
| 118 | digitalWrite(lpinB, LOW); |
| 119 | digitalWrite(lpinC, LOW); |
| 120 | break; |
| 121 | case 2: |
| 122 | digitalWrite(lpinA, LOW); |
| 123 | digitalWrite(lpinB, HIGH); |
| 124 | digitalWrite(lpinC, LOW); |
| 125 | break; |
| 126 | case 3: |
| 127 | digitalWrite(lpinA, HIGH); |
| 128 | digitalWrite(lpinB, HIGH); |
| 129 | digitalWrite(lpinC, LOW); |
| 130 | break; |
| 131 | case 4: |
| 132 | digitalWrite(lpinA, LOW); |
| 133 | digitalWrite(lpinB, LOW); |
| 134 | digitalWrite(lpinC, HIGH); |
| 135 | break; |
| 136 | case 5: |
| 137 | digitalWrite(lpinA, HIGH); |
| 138 | digitalWrite(lpinB, LOW); |
| 139 | digitalWrite(lpinC, HIGH); |
| 140 | break; |
| 141 | case 6: |
| 142 | digitalWrite(lpinA, LOW); |
| 143 | digitalWrite(lpinB, HIGH); |
| 144 | digitalWrite(lpinC, HIGH); |
| 145 | break; |
| 146 | case 7: |
| 147 | digitalWrite(lpinA, HIGH); |
| 148 | digitalWrite(lpinB, HIGH); |
| 149 | digitalWrite(lpinC, HIGH); |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | |
| 155 | }}} |