Updated by TMRh20
This is an example of how to use the RF24 class to create a battery- efficient system. It is just like the GettingStarted_CallResponse example, but the
ping node powers down the radio and sleeps the MCU after every ping/pong cycle, and the receiver sleeps between payloads.
40const int role_pin = 5;
42const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
50typedef enum { role_ping_out = 1,
51 role_pong_back } role_e;
54const char* role_friendly_name[] = {
"invalid",
"Ping out",
"Pong back" };
61typedef enum { wdt_16ms = 0,
70 wdt_8s } wdt_prescalar_e;
72void setup_watchdog(uint8_t prescalar);
75const short sleep_cycles_per_transmission = 4;
76volatile short sleep_cycles_remaining = sleep_cycles_per_transmission;
88 if (digitalRead(role_pin))
91 role = role_pong_back;
95 Serial.print(F(
"\n\rRF24/examples/pingpair_sleepy/\n\rROLE: "));
96 Serial.println(role_friendly_name[role]);
101 setup_watchdog(wdt_4s);
114 if (role == role_ping_out) {
115 radio.openWritingPipe(pipes[0]);
116 radio.openReadingPipe(1, pipes[1]);
118 radio.openWritingPipe(pipes[1]);
119 radio.openReadingPipe(1, pipes[0]);
123 radio.startListening();
132 if (role == role_ping_out) {
134 radio.stopListening();
136 unsigned long time =
millis();
137 Serial.print(F(
"Now sending... "));
138 Serial.println(time);
140 radio.write(&time,
sizeof(
unsigned long));
142 radio.startListening();
144 unsigned long started_waiting_at =
millis();
145 bool timeout =
false;
146 while (!radio.available()) {
147 if (
millis() - started_waiting_at > 250) {
154 Serial.println(F(
"Failed, response timed out."));
156 unsigned long got_time;
157 radio.read(&got_time,
sizeof(
unsigned long));
159 printf(
"Got response %lu, round-trip delay: %lu\n\r", got_time,
millis() - got_time);
173 if (role == role_pong_back) {
175 if (radio.available()) {
177 unsigned long got_time;
178 while (radio.available()) {
179 radio.read(&got_time,
sizeof(
unsigned long));
181 printf(
"Got payload %lu @ %lu...", got_time,
millis());
184 radio.stopListening();
185 radio.write(&got_time,
sizeof(
unsigned long));
186 Serial.println(F(
"Sent response."));
187 radio.startListening();
189 Serial.println(F(
"Sleeping"));
206void setup_watchdog(uint8_t prescalar) {
208 uint8_t wdtcsr = prescalar & 7;
212 WDTCSR =
_BV(WDCE) |
_BV(WDE);
213 WDTCSR =
_BV(WDCE) | wdtcsr |
_BV(WDIE);
218 Serial.println(F(
"WDT"));
222 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
224 attachInterrupt(0, wakeUp,
LOW);
230 WDTCSR &= ~_BV(WDIE);
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
#define pinMode(pin, direction)
#define digitalWrite(pin, value)