Optimized high speed nRF24L01+ driver class documentation v1.4.10
TMRh20 2020 - Optimized fork of the nRF24L01+ driver
Loading...
Searching...
No Matches
examples/old_backups/pingpair_sleepy/pingpair_sleepy.ino

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.

1/*
2 Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7
8 TMRh20 2014 - Updates to the library allow sleeping both in TX and RX modes:
9 TX Mode: The radio can be powered down (.9uA current) and the Arduino slept using the watchdog timer
10 RX Mode: The radio can be left in standby mode (22uA current) and the Arduino slept using an interrupt pin
11*/
12
27#include <SPI.h>
28#include <avr/sleep.h>
29#include <avr/power.h>
30#include "nRF24L01.h"
31#include "RF24.h"
32#include "printf.h"
33
34
35// Set up nRF24L01 radio on SPI bus plus pins 7 & 8
36RF24 radio(7, 8);
37
38// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
39// Leave open to be the 'ping' transmitter
40const int role_pin = 5;
41
42const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL }; // Radio pipe addresses for the 2 nodes to communicate.
43
44// Role management
45// Set up role. This sketch uses the same software for all the nodes
46// in this system. Doing so greatly simplifies testing. The hardware itself specifies
47// which node it is.
48
49// The various roles supported by this sketch
50typedef enum { role_ping_out = 1,
51 role_pong_back } role_e;
52
53// The debug-friendly names of those roles
54const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back" };
55
56// The role of the current running sketch
57role_e role;
58
59
60// Sleep declarations
61typedef enum { wdt_16ms = 0,
62 wdt_32ms,
63 wdt_64ms,
64 wdt_128ms,
65 wdt_250ms,
66 wdt_500ms,
67 wdt_1s,
68 wdt_2s,
69 wdt_4s,
70 wdt_8s } wdt_prescalar_e;
71
72void setup_watchdog(uint8_t prescalar);
73void do_sleep(void);
74
75const short sleep_cycles_per_transmission = 4;
76volatile short sleep_cycles_remaining = sleep_cycles_per_transmission;
77
78
79
80void setup() {
81
82 // set up the role pin
83 pinMode(role_pin, INPUT);
84 digitalWrite(role_pin, HIGH);
85 delay(20); // Just to get a solid reading on the role pin
86
87 // read the address pin, establish our role
88 if (digitalRead(role_pin))
89 role = role_ping_out;
90 else
91 role = role_pong_back;
92
93 Serial.begin(115200);
95 Serial.print(F("\n\rRF24/examples/pingpair_sleepy/\n\rROLE: "));
96 Serial.println(role_friendly_name[role]);
97
98 // Prepare sleep parameters
99 // Only the ping out role uses WDT. Wake up every 4s to send a ping
100 //if ( role == role_ping_out )
101 setup_watchdog(wdt_4s);
102
103 // Setup and configure rf radio
104
105 radio.begin();
106
107 // Open pipes to other nodes for communication
108
109 // This simple sketch opens two pipes for these two nodes to communicate
110 // back and forth.
111 // Open 'our' pipe for writing
112 // Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
113
114 if (role == role_ping_out) {
115 radio.openWritingPipe(pipes[0]);
116 radio.openReadingPipe(1, pipes[1]);
117 } else {
118 radio.openWritingPipe(pipes[1]);
119 radio.openReadingPipe(1, pipes[0]);
120 }
121
122 // Start listening
123 radio.startListening();
124
125 // Dump the configuration of the rf unit for debugging
126 //radio.printDetails();
127}
128
129void loop() {
130
131
132 if (role == role_ping_out) { // Ping out role. Repeatedly send the current time
133 radio.powerUp(); // Power up the radio after sleeping
134 radio.stopListening(); // First, stop listening so we can talk.
135
136 unsigned long time = millis(); // Take the time, and send it.
137 Serial.print(F("Now sending... "));
138 Serial.println(time);
139
140 radio.write(&time, sizeof(unsigned long));
141
142 radio.startListening(); // Now, continue listening
143
144 unsigned long started_waiting_at = millis(); // Wait here until we get a response, or timeout (250ms)
145 bool timeout = false;
146 while (!radio.available()) {
147 if (millis() - started_waiting_at > 250) { // Break out of the while loop if nothing available
148 timeout = true;
149 break;
150 }
151 }
152
153 if (timeout) { // Describe the results
154 Serial.println(F("Failed, response timed out."));
155 } else {
156 unsigned long got_time; // Grab the response, compare, and send to debugging spew
157 radio.read(&got_time, sizeof(unsigned long));
158
159 printf("Got response %lu, round-trip delay: %lu\n\r", got_time, millis() - got_time);
160 }
161
162 // Shut down the system
163 delay(500); // Experiment with some delay here to see if it has an effect
164 // Power down the radio.
165 radio.powerDown(); // NOTE: The radio MUST be powered back up again manually
166
167 // Sleep the MCU.
168 do_sleep();
169 }
170
171
172 // Pong back role. Receive each packet, dump it out, and send it back
173 if (role == role_pong_back) {
174
175 if (radio.available()) { // if there is data ready
176
177 unsigned long got_time;
178 while (radio.available()) { // Dump the payloads until we've gotten everything
179 radio.read(&got_time, sizeof(unsigned long)); // Get the payload, and see if this was the last one.
180 // Spew it. Include our time, because the ping_out millis counter is unreliable
181 printf("Got payload %lu @ %lu...", got_time, millis()); // due to it sleeping
182 }
183
184 radio.stopListening(); // First, stop listening so we can talk
185 radio.write(&got_time, sizeof(unsigned long)); // Send the final one back.
186 Serial.println(F("Sent response."));
187 radio.startListening(); // Now, resume listening so we catch the next packets.
188 } else {
189 Serial.println(F("Sleeping"));
190 delay(50); // Delay so the serial data can print out
191 do_sleep();
192 }
193 }
194}
195
196void wakeUp() {
197 sleep_disable();
198}
199
200// Sleep helpers
201
202//Prescaler values
203// 0=16ms, 1=32ms,2=64ms,3=125ms,4=250ms,5=500ms
204// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
205
206void setup_watchdog(uint8_t prescalar) {
207
208 uint8_t wdtcsr = prescalar & 7;
209 if (prescalar & 8)
210 wdtcsr |= _BV(WDP3);
211 MCUSR &= ~_BV(WDRF); // Clear the WD System Reset Flag
212 WDTCSR = _BV(WDCE) | _BV(WDE); // Write the WD Change enable bit to enable changing the prescaler and enable system reset
213 WDTCSR = _BV(WDCE) | wdtcsr | _BV(WDIE); // Write the prescalar bits (how long to sleep, enable the interrupt to wake the MCU
214}
215
216ISR(WDT_vect) {
217 //--sleep_cycles_remaining;
218 Serial.println(F("WDT"));
219}
220
221void do_sleep(void) {
222 set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
223 sleep_enable();
224 attachInterrupt(0, wakeUp, LOW);
225 WDTCSR |= _BV(WDIE);
226 sleep_mode(); // System sleeps here
227 // The WDT_vect interrupt wakes the MCU from here
228 sleep_disable(); // System continues execution here when watchdog timed out
229 detachInterrupt(0);
230 WDTCSR &= ~_BV(WDIE);
231}
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
Definition RF24.h:116
#define delay(millisec)
#define INPUT
#define pinMode(pin, direction)
#define _BV(x)
#define HIGH
#define LOW
#define digitalWrite(pin, value)
#define millis()
void printf_begin(void)
Definition printf.h:33