This example demonstrates the basic getting started functionality, but with failure handling for the radio chip. Addresses random radio failures etc, potentially due to loose wiring on breadboards etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#include <SPI.h>
42
43
44
45bool radioNumber = 0;
46
47
49
50
51byte addresses[][6] = { "1Node", "2Node" };
52
53
54bool role = 0;
55
56
57
58
59void configureRadio() {
60
61 radio.begin();
62
63
64
66
67
68 if (radioNumber) {
69 radio.openWritingPipe(addresses[1]);
70 radio.openReadingPipe(1, addresses[0]);
71 } else {
72 radio.openWritingPipe(addresses[0]);
73 radio.openReadingPipe(1, addresses[1]);
74 }
75
76
77 radio.startListening();
78 radio.printDetails();
79}
80
81
82
83
84void setup() {
85 Serial.begin(115200);
86 Serial.println(F("RF24/examples/GettingStarted"));
87 Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
88
90
91 configureRadio();
92}
93
94uint32_t configTimer =
millis();
95
96void loop() {
97
98 if (radio.failureDetected) {
99 radio.failureDetected = false;
101 Serial.println("Radio failure detected, restarting radio");
102 configureRadio();
103 }
104
105
106 if (
millis() - configTimer > 5000) {
109 radio.failureDetected = true;
110 Serial.print("Radio configuration error detected");
111 }
112 }
113
114
115
116
117 if (role == 1) {
118
119 radio.stopListening();
120
121 Serial.println(F("Now sending"));
122
123 unsigned long start_time = micros();
124 if (!radio.write(&start_time, sizeof(unsigned long))) {
125 Serial.println(F("failed"));
126 }
127
128 radio.startListening();
129
130 unsigned long started_waiting_at = micros();
131 bool timeout = false;
132
133 while (!radio.available())
134 {
135 if (micros() - started_waiting_at > 200000)
136 {
137 timeout = true;
138 break;
139 }
140 }
141
142 if (timeout) {
143
144 Serial.println(F("Failed, response timed out."));
145 } else {
146
147
148 unsigned long got_time;
149
150
151 uint32_t failTimer =
millis();
152 while (radio.available())
153 {
154 if (
millis() - failTimer > 250) {
155 radio.failureDetected = true;
156 Serial.println("Radio available failure detected");
157 break;
158 }
159 radio.read(&got_time, sizeof(unsigned long));
160 }
161 unsigned long end_time = micros();
162
163
164 Serial.print(F("Sent "));
165 Serial.print(start_time);
166 Serial.print(F(", Got response "));
167 Serial.print(got_time);
168 Serial.print(F(", Round-trip delay "));
169 Serial.print(end_time - start_time);
170 Serial.println(F(" microseconds"));
171 }
172
174 }
175
176
177
178
179 if (role == 0) {
180 unsigned long got_time;
181
182 if (radio.available()) {
183 uint32_t failTimer =
millis();
184
185 while (radio.available())
186 {
187 if (
millis() - failTimer > 500) {
188 Serial.println("Radio available failure detected");
189 radio.failureDetected = true;
190 break;
191 }
192 radio.read(&got_time, sizeof(unsigned long));
193 }
194
195 radio.stopListening();
196 radio.write(&got_time, sizeof(unsigned long));
197 radio.startListening();
198 Serial.print(F("Sent response "));
199 Serial.println(got_time);
200 }
201 }
202
203
204
205
206 if (Serial.available()) {
207 char c = toupper(Serial.read());
208 if (c == 'T' && role == 0) {
209 Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
210 role = 1;
211 } else if (c == 'R' && role == 1) {
212 Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
213 role = 0;
214 radio.startListening();
215 }
216 }
217}
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.