| 1 | {{{ |
| 2 | /* |
| 3 | * Server 廣播發出訊號,[node][val] |
| 4 | * B1 : 當 B node 收到後,會閃 LED 燈 1 次並回傳 1 |
| 5 | * B2 : 當 B node 收到後,會閃 LED 燈 2 次並回傳 2 |
| 6 | * C1 : 當 C node 收到後,會閃 LED 燈 1 次並回傳 1 |
| 7 | */ |
| 8 | #include <NewSoftSerial.h> |
| 9 | #define node_A_sh 13A200 |
| 10 | #define node_A_sl 403D0190 |
| 11 | #define node_B_sl 403D018A |
| 12 | #define node_C_sl 403D01D0 |
| 13 | |
| 14 | // set pin 9 as RX |
| 15 | uint8_t ssRX = 9; |
| 16 | // set pin 10 as TX |
| 17 | uint8_t ssTX = 10; |
| 18 | // enable soft serial nss |
| 19 | NewSoftSerial nss(ssRX, ssTX); |
| 20 | |
| 21 | int data; |
| 22 | int nodeACount=1; |
| 23 | int nodeBCount=1; |
| 24 | |
| 25 | void setup() |
| 26 | { |
| 27 | Serial.begin(9600); |
| 28 | // start soft serial |
| 29 | nss.begin(9600); |
| 30 | nss.print("A"); |
| 31 | nss.print(byte(nodeACount)); |
| 32 | nss.print("B"); |
| 33 | nss.print(byte(nodeBCount)); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | void loop() |
| 38 | { |
| 39 | if(nss.available()) |
| 40 | { |
| 41 | data = nss.read(); |
| 42 | switch(data) |
| 43 | { |
| 44 | case 'A': |
| 45 | nodeACount++; |
| 46 | nodeACount %= 6; |
| 47 | Serial.print("nodeACount = "); |
| 48 | Serial.println(nodeACount); |
| 49 | nss.print("A"); |
| 50 | nss.print(byte(nodeACount)); |
| 51 | break; |
| 52 | case 'B': |
| 53 | nodeBCount++; |
| 54 | nodeBCount %= 6; |
| 55 | Serial.print("nodeBCount = "); |
| 56 | Serial.println(nodeBCount); |
| 57 | nss.print("B"); |
| 58 | nss.print(byte(nodeBCount)); |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | }}} |