| 2 | /* |
| 3 | * Arduino 由 XBee 接收資料。輸入 1、2、3、4 會亮不同 led,按 2 會打開 relay |
| 4 | */ |
| 5 | |
| 6 | int serial_data = 0; // 由 serial 讀入的資料 |
| 7 | |
| 8 | void setup() |
| 9 | { |
| 10 | pinMode(2, OUTPUT); |
| 11 | pinMode(3, OUTPUT); |
| 12 | pinMode(4, OUTPUT); |
| 13 | pinMode(5, OUTPUT); |
| 14 | pinMode(6, OUTPUT); |
| 15 | pinMode(7, OUTPUT); |
| 16 | pinMode(8, OUTPUT); |
| 17 | pinMode(9, OUTPUT); |
| 18 | pinMode(10, OUTPUT); |
| 19 | pinMode(11, OUTPUT); |
| 20 | pinMode(12, OUTPUT); |
| 21 | pinMode(13, OUTPUT); |
| 22 | Serial.begin(9600); |
| 23 | } |
| 24 | |
| 25 | void loop() |
| 26 | { |
| 27 | clear_statue(); |
| 28 | //Serial.print("debug : serial_data = "); |
| 29 | //Serial.println(serial_data); |
| 30 | //delay(500); |
| 31 | if (Serial.available() > 0) |
| 32 | { |
| 33 | serial_data = Serial.read(); |
| 34 | Serial.print("I receive: "); |
| 35 | Serial.println(serial_data); |
| 36 | } |
| 37 | switch (serial_data) |
| 38 | { |
| 39 | case 49: |
| 40 | digitalWrite(2, HIGH); |
| 41 | digitalWrite(6, HIGH); |
| 42 | digitalWrite(10, HIGH); |
| 43 | break; |
| 44 | case 50: |
| 45 | digitalWrite(3, HIGH); |
| 46 | digitalWrite(7, HIGH); |
| 47 | digitalWrite(11, HIGH); |
| 48 | break; |
| 49 | case 51: |
| 50 | digitalWrite(4, HIGH); |
| 51 | digitalWrite(8, HIGH); |
| 52 | digitalWrite(12, HIGH); |
| 53 | break; |
| 54 | case 52: |
| 55 | digitalWrite(5, HIGH); |
| 56 | digitalWrite(9, HIGH); |
| 57 | digitalWrite(13, HIGH); |
| 58 | break; |
| 59 | } |
| 60 | delay(200); |
| 61 | } |
| 62 | |
| 63 | void clear_statue() |
| 64 | { |
| 65 | serial_data = 0; |
| 66 | for (int i = 2; i <= 13; i++) |
| 67 | digitalWrite(i, LOW); |
| 68 | } |
| 69 | |