1 | #include <SPI.h>
|
2 | #include "RF24.h"
|
3 |
|
4 | /****************** User Config ***************************/
|
5 | /*** Set this radio as radio number 0 or 1 ***/
|
6 | bool radioNumber = 1;
|
7 |
|
8 | /* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
|
9 | RF24 radio(7,8);
|
10 | /**********************************************************/
|
11 |
|
12 | byte addresses[][6] = {"1Node","2Node"};
|
13 |
|
14 | // Used to control whether this node is sending or receiving
|
15 | bool role = 0;
|
16 |
|
17 | void setup() {
|
18 | Serial.begin(115200);
|
19 | Serial.println(F("RF24/examples/GettingStarted"));
|
20 | Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
|
21 |
|
22 | radio.begin();
|
23 |
|
24 | // Set the PA Level low to prevent power supply related issues since this is a
|
25 | // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
|
26 | radio.setPALevel(RF24_PA_LOW);
|
27 |
|
28 | // Open a writing and reading pipe on each radio, with opposite addresses
|
29 | if(radioNumber){
|
30 | radio.openWritingPipe(addresses[1]);
|
31 | radio.openReadingPipe(1,addresses[0]);
|
32 | }else{
|
33 | radio.openWritingPipe(addresses[0]);
|
34 | radio.openReadingPipe(1,addresses[1]);
|
35 | }
|
36 |
|
37 | // Start the radio listening for data
|
38 | radio.startListening();
|
39 | }
|
40 |
|
41 | void loop() {
|
42 |
|
43 |
|
44 | /****************** Ping Out Role ***************************/
|
45 | if (role == 1) {
|
46 | Serial.println(F("*** role = 1"));
|
47 |
|
48 | radio.stopListening(); // First, stop listening so we can talk.
|
49 |
|
50 |
|
51 | Serial.println(F("Now sending"));
|
52 |
|
53 | unsigned long start_time = micros(); // Take the time, and send it. This will block until complete
|
54 | if (!radio.write( &start_time, sizeof(unsigned long) )){
|
55 | Serial.println(F("failed"));
|
56 | }
|
57 |
|
58 | radio.startListening(); // Now, continue listening
|
59 |
|
60 | unsigned long started_waiting_at = micros(); // Set up a timeout period, get the current microseconds
|
61 | boolean timeout = false; // Set up a variable to indicate if a response was received or not
|
62 |
|
63 | while ( ! radio.available() ){ // While nothing is received
|
64 | if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop
|
65 | timeout = true;
|
66 | break;
|
67 | }
|
68 | }
|
69 |
|
70 | if ( timeout ){ // Describe the results
|
71 | Serial.println(F("Failed, response timed out."));
|
72 | }else{
|
73 | unsigned long got_time; // Grab the response, compare, and send to debugging spew
|
74 | radio.read( &got_time, sizeof(unsigned long) );
|
75 | unsigned long end_time = micros();
|
76 |
|
77 | // Spew it
|
78 | Serial.print(F("Sent "));
|
79 | Serial.print(start_time);
|
80 | Serial.print(F(", Got response "));
|
81 | Serial.print(got_time);
|
82 | Serial.print(F(", Round-trip delay "));
|
83 | Serial.print(end_time-start_time);
|
84 | Serial.println(F(" microseconds"));
|
85 | }
|
86 |
|
87 | // Try again 1s later
|
88 | delay(1000);
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 | /****************** Pong Back Role ***************************/
|
94 |
|
95 | if ( role == 0 )
|
96 | {
|
97 | Serial.println(F("*** role = 0"));
|
98 | unsigned long got_time;
|
99 |
|
100 | if( radio.available()){
|
101 | Serial.println(F("*** Radio Available" )); // While there is data ready
|
102 | while (radio.available()) { // While there is data ready
|
103 | radio.read( &got_time, sizeof(unsigned long) ); // Get the payload
|
104 | Serial.println(F("***" ));
|
105 | }
|
106 | Serial.println(F("*** Radio fertig gelesen" ));
|
107 | radio.stopListening(); // First, stop listening so we can talk
|
108 | radio.write( &got_time, sizeof(unsigned long) ); // Send the final one back.
|
109 | radio.startListening(); // Now, resume listening so we catch the next packets.
|
110 | Serial.print(F("Sent response "));
|
111 | Serial.println(got_time);
|
112 | }
|
113 | }
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | /****************** Change Roles via Serial Commands ***************************/
|
119 |
|
120 | if ( Serial.available() )
|
121 | {
|
122 | Serial.println(F("*** Serial Avaiable ***"));
|
123 |
|
124 | char c = toupper(Serial.read());
|
125 | Serial.println(c);
|
126 | if ( c == 'T' && role == 0 ){
|
127 | Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
|
128 | role = 1; // Become the primary transmitter (ping out)
|
129 |
|
130 | }else
|
131 | if ( c == 'R' && role == 1 ){
|
132 | Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
|
133 | role = 0; // Become the primary receiver (pong back)
|
134 | radio.startListening();
|
135 |
|
136 | }
|
137 | }
|
138 |
|
139 |
|
140 | } // Loop
|