/*
* Arduino 由 XBee 接收資料。輸入不同字元會亮不同數字,
* pin0、pin1 = XBee
* pin2~pin12 = LED matrix display
* pin13 = Relay switch = button 0 開;p 關。
* History:
* 2010-02-06 0.0 初版
*/
#define lpinA 2 // 定義 74138 A 腳位
#define lpinB 3 // 定義 74138 B 腳位
#define lpinC 4 // 定義 74138 C 腳位
#define hpin0 5 // 定義 LED 正極腳位
#define hpin1 6 // 定義 LED 正極腳位
#define hpin2 7 // 定義 LED 正極腳位
#define hpin3 8 // 定義 LED 正極腳位
#define hpin4 9 // 定義 LED 正極腳位
#define hpin5 10 // 定義 LED 正極腳位
#define hpin6 11 // 定義 LED 正極腳位
#define hpin7 12 // 定義 LED 正極腳位
#define pin_relay 13 // 定義控制 relay 的 pin
#define row_size 8 // 定義 Matrix row 大小
#define col_size 8 // 定義 Matrix colum 小
#define delay_time 100 // 延遲時間
#define data_default {0, 0, 0, 0, 0, 0, 0, 0,\
0, 0, 0, 0, 0, 0, 0, 0,\
0, 0, 0, 0, 0, 0, 0, 0,\
0, 0, 0, 0, 0, 0, 0, 0,\
0, 0, 0, 0, 0, 0, 0, 0,\
0, 0, 0, 0, 0, 0, 0, 0,\
0, 0, 0, 0, 0, 0, 0, 0,\
0, 0, 0, 0, 0, 0, 0, 0}
#define data_A {0, 0, 0, 1, 1, 0, 0, 0,\
0, 0, 1, 0, 0, 1, 0, 0,\
0, 0, 1, 0, 0, 1, 0, 0,\
0, 1, 0, 0, 0, 0, 1, 0,\
0, 1, 1, 1, 1, 1, 1, 0,\
0, 1, 0, 0, 0, 0, 1, 0,\
0, 1, 0, 0, 0, 0, 1, 0,\
0, 1, 0, 0, 0, 0, 1, 0}
#define data_N {0, 1, 0, 0, 0, 0, 1, 0,\
0, 1, 1, 0, 0, 0, 1, 0,\
0, 1, 1, 0, 0, 0, 1, 0,\
0, 1, 0, 1, 0, 0, 1, 0,\
0, 1, 0, 0, 1, 0, 1, 0,\
0, 1, 0, 0, 0, 1, 1, 0,\
0, 1, 0, 0, 0, 1, 1, 0,\
0, 1, 0, 0, 0, 0, 1, 0}
#define data_relayon {1, 1, 1, 0, 0, 1, 0, 0,\
1, 0, 1, 0, 0, 1, 0, 0,\
1, 1, 0, 0, 0, 1, 0, 0,\
1, 0, 1, 0, 0, 1, 1, 1,\
0, 1, 1, 0, 1, 0, 0, 1,\
1, 0, 0, 1, 1, 1, 0, 1,\
1, 0, 0, 1, 1, 0, 1, 1,\
0, 1, 1, 0, 1, 0, 0, 1}
#define data_relayoff {1, 1, 1, 0, 0, 1, 0, 0,\
1, 0, 1, 0, 0, 1, 0, 0,\
1, 1, 0, 0, 0, 1, 0, 0,\
1, 0, 1, 0, 0, 1, 1, 1,\
0, 1, 1, 0, 1, 1, 1, 1,\
1, 0, 0, 1, 1, 1, 1, 1,\
1, 0, 0, 1, 1, 0, 1, 0,\
0, 1, 1, 0, 1, 0, 1, 0}
// 所有要顯示字的 array
boolean word_array[4][row_size][col_size] = {data_A, data_N, data_relayon, data_relayoff};
// LED Matrix
boolean led[row_size][col_size] ;
// 水平方向 led
byte row_pin[col_size]={hpin0, hpin1, hpin2, hpin3, hpin4, hpin5, hpin6, hpin7};
int serial_data = 0; // 由 serial 讀入的資料
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
clear_led();
Serial.begin(9600);
}
void loop()
{
clear_led();
//Serial.print("debug : serial_data = ");
//Serial.println(serial_data);
//delay(500);
if (Serial.available() > 0)
{
serial_data = Serial.read();
Serial.print("I receive: ");
Serial.println(serial_data);
}
switch (serial_data)
{
case 0:
//default serial_data value received nothing, do nothing
break;
case 48: // key 0
digitalWrite(pin_relay, HIGH);
word_to_led(2);
display_led(led, 500);
break;
break;
case 49: // key 1
word_to_led(0);
display_led(led, 500);
break;
case 50: // key 2
word_to_led(1);
display_led(led, 500);
break;
case 112: // key
digitalWrite(pin_relay, LOW);
word_to_led(3);
display_led(led, 500);
break;
break;
default:
Serial.println("key is not defined");
delay(delay_time);
}
}
void clear_led()
{
// serial_data = 0;
for (int i = 0; i < col_size; i++)
digitalWrite(row_pin[i], LOW);
}
void display_led(byte led[row_size][col_size], int continue_time)
{
for (int k = 0; k < continue_time; k++)
{
for (int i = 0; i < col_size; i++)
{
// 掃描第一個 col
low_74138pin(i);
for (int j = 0; j < row_size; j++)
{
if (led[j][i] == 1)
{
digitalWrite(row_pin[j], HIGH);
//delay(delay_time);
}
}
clear_led();
}
}
}
void low_74138pin(int num)
{
switch (num)
{
case 0:
digitalWrite(lpinA, LOW);
digitalWrite(lpinB, LOW);
digitalWrite(lpinC, LOW);
break;
case 1:
digitalWrite(lpinA, HIGH);
digitalWrite(lpinB, LOW);
digitalWrite(lpinC, LOW);
break;
case 2:
digitalWrite(lpinA, LOW);
digitalWrite(lpinB, HIGH);
digitalWrite(lpinC, LOW);
break;
case 3:
digitalWrite(lpinA, HIGH);
digitalWrite(lpinB, HIGH);
digitalWrite(lpinC, LOW);
break;
case 4:
digitalWrite(lpinA, LOW);
digitalWrite(lpinB, LOW);
digitalWrite(lpinC, HIGH);
break;
case 5:
digitalWrite(lpinA, HIGH);
digitalWrite(lpinB, LOW);
digitalWrite(lpinC, HIGH);
break;
case 6:
digitalWrite(lpinA, LOW);
digitalWrite(lpinB, HIGH);
digitalWrite(lpinC, HIGH);
break;
case 7:
digitalWrite(lpinA, HIGH);
digitalWrite(lpinB, HIGH);
digitalWrite(lpinC, HIGH);
}
}
// 將第 n 個 word 讀入 led
void word_to_led(int n){
for (int i = 0; i < row_size; i++)
for (int j = 0; j < col_size; j++)
led[i][j] = word_array[n][i][j];
}