Written by TMRh20 in 2019
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.
51byte addresses[][6] = {
"1Node",
"2Node" };
59void configureRadio() {
69 radio.openWritingPipe(addresses[1]);
70 radio.openReadingPipe(1, addresses[0]);
72 radio.openWritingPipe(addresses[0]);
73 radio.openReadingPipe(1, addresses[1]);
77 radio.startListening();
86 Serial.println(F(
"RF24/examples/GettingStarted"));
87 Serial.println(F(
"*** PRESS 'T' to begin transmitting to the other node"));
94uint32_t configTimer =
millis();
98 if (radio.failureDetected) {
99 radio.failureDetected =
false;
101 Serial.println(
"Radio failure detected, restarting radio");
106 if (
millis() - configTimer > 5000) {
109 radio.failureDetected =
true;
110 Serial.print(
"Radio configuration error detected");
119 radio.stopListening();
121 Serial.println(F(
"Now sending"));
123 unsigned long start_time = micros();
124 if (!radio.write(&start_time,
sizeof(
unsigned long))) {
125 Serial.println(F(
"failed"));
128 radio.startListening();
130 unsigned long started_waiting_at = micros();
131 bool timeout =
false;
133 while (!radio.available())
135 if (micros() - started_waiting_at > 200000)
144 Serial.println(F(
"Failed, response timed out."));
148 unsigned long got_time;
151 uint32_t failTimer =
millis();
152 while (radio.available())
154 if (
millis() - failTimer > 250) {
155 radio.failureDetected =
true;
156 Serial.println(
"Radio available failure detected");
159 radio.read(&got_time,
sizeof(
unsigned long));
161 unsigned long end_time = micros();
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"));
180 unsigned long got_time;
182 if (radio.available()) {
183 uint32_t failTimer =
millis();
185 while (radio.available())
187 if (
millis() - failTimer > 500) {
188 Serial.println(
"Radio available failure detected");
189 radio.failureDetected =
true;
192 radio.read(&got_time,
sizeof(
unsigned long));
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);
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"));
211 }
else if (c ==
'R' && role == 1) {
212 Serial.println(F(
"*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
214 radio.startListening();
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.