| 1 | /* |
| 2 | * Arduino 由 XBee 接收資料。輸入不同字元會亮不同數字, |
| 3 | * pin0、pin1 = XBee |
| 4 | * pin2~pin12 = LED matrix display |
| 5 | * pin13 = Relay switch = button 0 開;p 關。 |
| 6 | * History: |
| 7 | * 2010-02-06 0.0 初版 |
| 8 | */ |
| 9 | |
| 10 | #define lpinA 2 // 定義 74138 A 腳位 |
| 11 | #define lpinB 3 // 定義 74138 B 腳位 |
| 12 | #define lpinC 4 // 定義 74138 C 腳位 |
| 13 | #define hpin0 5 // 定義 LED 正極腳位 |
| 14 | #define hpin1 6 // 定義 LED 正極腳位 |
| 15 | #define hpin2 7 // 定義 LED 正極腳位 |
| 16 | #define hpin3 8 // 定義 LED 正極腳位 |
| 17 | #define hpin4 9 // 定義 LED 正極腳位 |
| 18 | #define hpin5 10 // 定義 LED 正極腳位 |
| 19 | #define hpin6 11 // 定義 LED 正極腳位 |
| 20 | #define hpin7 12 // 定義 LED 正極腳位 |
| 21 | #define pin_relay 13 // 定義控制 relay 的 pin |
| 22 | #define row_size 8 // 定義 Matrix row 大小 |
| 23 | #define col_size 8 // 定義 Matrix colum 小 |
| 24 | #define delay_time 100 // 延遲時間 |
| 25 | |
| 26 | #define data_default {0, 0, 0, 0, 0, 0, 0, 0,\ |
| 27 | 0, 0, 0, 0, 0, 0, 0, 0,\ |
| 28 | 0, 0, 0, 0, 0, 0, 0, 0,\ |
| 29 | 0, 0, 0, 0, 0, 0, 0, 0,\ |
| 30 | 0, 0, 0, 0, 0, 0, 0, 0,\ |
| 31 | 0, 0, 0, 0, 0, 0, 0, 0,\ |
| 32 | 0, 0, 0, 0, 0, 0, 0, 0,\ |
| 33 | 0, 0, 0, 0, 0, 0, 0, 0} |
| 34 | |
| 35 | #define data_A {0, 0, 0, 1, 1, 0, 0, 0,\ |
| 36 | 0, 0, 1, 0, 0, 1, 0, 0,\ |
| 37 | 0, 0, 1, 0, 0, 1, 0, 0,\ |
| 38 | 0, 1, 0, 0, 0, 0, 1, 0,\ |
| 39 | 0, 1, 1, 1, 1, 1, 1, 0,\ |
| 40 | 0, 1, 0, 0, 0, 0, 1, 0,\ |
| 41 | 0, 1, 0, 0, 0, 0, 1, 0,\ |
| 42 | 0, 1, 0, 0, 0, 0, 1, 0} |
| 43 | |
| 44 | #define data_N {0, 1, 0, 0, 0, 0, 1, 0,\ |
| 45 | 0, 1, 1, 0, 0, 0, 1, 0,\ |
| 46 | 0, 1, 1, 0, 0, 0, 1, 0,\ |
| 47 | 0, 1, 0, 1, 0, 0, 1, 0,\ |
| 48 | 0, 1, 0, 0, 1, 0, 1, 0,\ |
| 49 | 0, 1, 0, 0, 0, 1, 1, 0,\ |
| 50 | 0, 1, 0, 0, 0, 1, 1, 0,\ |
| 51 | 0, 1, 0, 0, 0, 0, 1, 0} |
| 52 | |
| 53 | #define data_relayon {1, 1, 1, 0, 0, 1, 0, 0,\ |
| 54 | 1, 0, 1, 0, 0, 1, 0, 0,\ |
| 55 | 1, 1, 0, 0, 0, 1, 0, 0,\ |
| 56 | 1, 0, 1, 0, 0, 1, 1, 1,\ |
| 57 | 0, 1, 1, 0, 1, 0, 0, 1,\ |
| 58 | 1, 0, 0, 1, 1, 1, 0, 1,\ |
| 59 | 1, 0, 0, 1, 1, 0, 1, 1,\ |
| 60 | 0, 1, 1, 0, 1, 0, 0, 1} |
| 61 | |
| 62 | #define data_relayoff {1, 1, 1, 0, 0, 1, 0, 0,\ |
| 63 | 1, 0, 1, 0, 0, 1, 0, 0,\ |
| 64 | 1, 1, 0, 0, 0, 1, 0, 0,\ |
| 65 | 1, 0, 1, 0, 0, 1, 1, 1,\ |
| 66 | 0, 1, 1, 0, 1, 1, 1, 1,\ |
| 67 | 1, 0, 0, 1, 1, 1, 1, 1,\ |
| 68 | 1, 0, 0, 1, 1, 0, 1, 0,\ |
| 69 | 0, 1, 1, 0, 1, 0, 1, 0} |
| 70 | |
| 71 | // 所有要顯示字的 array |
| 72 | boolean word_array[4][row_size][col_size] = {data_A, data_N, data_relayon, data_relayoff}; |
| 73 | // LED Matrix |
| 74 | boolean led[row_size][col_size] ; |
| 75 | // 水平方向 led |
| 76 | byte row_pin[col_size]={hpin0, hpin1, hpin2, hpin3, hpin4, hpin5, hpin6, hpin7}; |
| 77 | |
| 78 | |
| 79 | int serial_data = 0; // 由 serial 讀入的資料 |
| 80 | |
| 81 | void setup() |
| 82 | { |
| 83 | pinMode(2, OUTPUT); |
| 84 | pinMode(3, OUTPUT); |
| 85 | pinMode(4, OUTPUT); |
| 86 | pinMode(5, OUTPUT); |
| 87 | pinMode(6, OUTPUT); |
| 88 | pinMode(7, OUTPUT); |
| 89 | pinMode(8, OUTPUT); |
| 90 | pinMode(9, OUTPUT); |
| 91 | pinMode(10, OUTPUT); |
| 92 | pinMode(11, OUTPUT); |
| 93 | pinMode(12, OUTPUT); |
| 94 | pinMode(13, OUTPUT); |
| 95 | clear_led(); |
| 96 | Serial.begin(9600); |
| 97 | } |
| 98 | |
| 99 | void loop() |
| 100 | { |
| 101 | clear_led(); |
| 102 | //Serial.print("debug : serial_data = "); |
| 103 | //Serial.println(serial_data); |
| 104 | //delay(500); |
| 105 | if (Serial.available() > 0) |
| 106 | { |
| 107 | serial_data = Serial.read(); |
| 108 | Serial.print("I receive: "); |
| 109 | Serial.println(serial_data); |
| 110 | } |
| 111 | switch (serial_data) |
| 112 | { |
| 113 | case 0: |
| 114 | //default serial_data value received nothing, do nothing |
| 115 | break; |
| 116 | case 48: // key 0 |
| 117 | digitalWrite(pin_relay, HIGH); |
| 118 | word_to_led(2); |
| 119 | display_led(led, 500); |
| 120 | break; |
| 121 | break; |
| 122 | case 49: // key 1 |
| 123 | word_to_led(0); |
| 124 | display_led(led, 500); |
| 125 | break; |
| 126 | case 50: // key 2 |
| 127 | word_to_led(1); |
| 128 | display_led(led, 500); |
| 129 | break; |
| 130 | case 112: // key |
| 131 | digitalWrite(pin_relay, LOW); |
| 132 | word_to_led(3); |
| 133 | display_led(led, 500); |
| 134 | break; |
| 135 | break; |
| 136 | default: |
| 137 | Serial.println("key is not defined"); |
| 138 | delay(delay_time); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void clear_led() |
| 143 | { |
| 144 | // serial_data = 0; |
| 145 | for (int i = 0; i < col_size; i++) |
| 146 | digitalWrite(row_pin[i], LOW); |
| 147 | } |
| 148 | |
| 149 | void display_led(byte led[row_size][col_size], int continue_time) |
| 150 | { |
| 151 | for (int k = 0; k < continue_time; k++) |
| 152 | { |
| 153 | for (int i = 0; i < col_size; i++) |
| 154 | { |
| 155 | // 掃描第一個 col |
| 156 | low_74138pin(i); |
| 157 | for (int j = 0; j < row_size; j++) |
| 158 | { |
| 159 | if (led[j][i] == 1) |
| 160 | { |
| 161 | digitalWrite(row_pin[j], HIGH); |
| 162 | //delay(delay_time); |
| 163 | } |
| 164 | } |
| 165 | clear_led(); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void low_74138pin(int num) |
| 171 | { |
| 172 | switch (num) |
| 173 | { |
| 174 | case 0: |
| 175 | digitalWrite(lpinA, LOW); |
| 176 | digitalWrite(lpinB, LOW); |
| 177 | digitalWrite(lpinC, LOW); |
| 178 | break; |
| 179 | case 1: |
| 180 | digitalWrite(lpinA, HIGH); |
| 181 | digitalWrite(lpinB, LOW); |
| 182 | digitalWrite(lpinC, LOW); |
| 183 | break; |
| 184 | case 2: |
| 185 | digitalWrite(lpinA, LOW); |
| 186 | digitalWrite(lpinB, HIGH); |
| 187 | digitalWrite(lpinC, LOW); |
| 188 | break; |
| 189 | case 3: |
| 190 | digitalWrite(lpinA, HIGH); |
| 191 | digitalWrite(lpinB, HIGH); |
| 192 | digitalWrite(lpinC, LOW); |
| 193 | break; |
| 194 | case 4: |
| 195 | digitalWrite(lpinA, LOW); |
| 196 | digitalWrite(lpinB, LOW); |
| 197 | digitalWrite(lpinC, HIGH); |
| 198 | break; |
| 199 | case 5: |
| 200 | digitalWrite(lpinA, HIGH); |
| 201 | digitalWrite(lpinB, LOW); |
| 202 | digitalWrite(lpinC, HIGH); |
| 203 | break; |
| 204 | case 6: |
| 205 | digitalWrite(lpinA, LOW); |
| 206 | digitalWrite(lpinB, HIGH); |
| 207 | digitalWrite(lpinC, HIGH); |
| 208 | break; |
| 209 | case 7: |
| 210 | digitalWrite(lpinA, HIGH); |
| 211 | digitalWrite(lpinB, HIGH); |
| 212 | digitalWrite(lpinC, HIGH); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // 將第 n 個 word 讀入 led |
| 217 | void word_to_led(int n){ |
| 218 | for (int i = 0; i < row_size; i++) |
| 219 | for (int j = 0; j < col_size; j++) |
| 220 | led[i][j] = word_array[n][i][j]; |
| 221 | } |