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

Updated by TMRh20

This is an example of how to user interrupts to interact with the radio, and a demonstration of how to use them to sleep when receiving, and not miss any payloads.
The pingpair_sleepy example expands on sleep functionality with a timed sleep option for the transmitter. Sleep functionality is built directly into my fork of the RF24Network library

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 Update 2014 - TMRh20
9*/
10
20#include <SPI.h>
21#include "nRF24L01.h"
22#include "RF24.h"
23#include "printf.h"
24
25// Hardware configuration
26RF24 radio(7, 8); // Set up nRF24L01 radio on SPI bus plus pins 7 & 8
27
28// Our ACK payload will simply be 4 bytes containing the number of payloads received
29static uint32_t message_count = 1; // start counting at 1
30
31// Demonstrates another method of setting up the addresses
32byte address[][5] = { 0xCC, 0xCE, 0xCC, 0xCE, 0xCC, 0xCE, 0xCC, 0xCE, 0xCC, 0xCE };
33
34/************************* Role management ****************************/
35// Set up role. This sketch uses the same software for all the nodes in this
36// system. Doing so greatly simplifies testing.
37
38// The role_pin is a digital input pin used to set the role of this radio.
39// Connect the role_pin to GND to be the 'pong' receiver
40// Leave the role_pin open to be the 'ping' transmitter
41const short role_pin = 5; // use pin 5
42typedef enum { role_sender = 1,
43 role_receiver } role_e; // The various roles supported by this sketch
44const char* role_friendly_name[] = { "invalid", "Sender", "Receiver" }; // The debug-friendly names of those roles
45role_e role; // The role of the current running sketch
46
47
48void setup() {
49
50 pinMode(role_pin, INPUT); // set up the role pin
51 digitalWrite(role_pin, HIGH); // Change this to LOW/HIGH instead of using an external pin
52 delay(20); // Just to get a solid reading on the role pin
53
54 if (digitalRead(role_pin)) // read the role_pin pin to establish our role
55 role = role_sender;
56 else
57 role = role_receiver;
58
59
60 Serial.begin(115200);
61 printf_begin(); // needed for printDetails()
62
63 // print introduction
64 Serial.print(F("\n\rRF24/examples/pingpair_irq\n\rROLE: "));
65 Serial.println(role_friendly_name[role]);
66
67
68 /********************** Setup and configure rf radio *********************/
69 radio.begin();
70
71 // Examples are usually run with both radios in close proximity to each other
72 radio.setPALevel(RF24_PA_LOW); // defaults to RF24_PA_MAX
73 radio.enableAckPayload(); // We will be using the ACK Payload feature which is not enabled by default
74 radio.enableDynamicPayloads(); // Ack payloads are dynamic payloads
75
76 // Open a writing and reading pipe on each radio, with opposite addresses
77 if (role == role_sender) {
78 radio.openWritingPipe(address[0]);
79 radio.openReadingPipe(1, address[1]);
80 } else {
81 radio.openWritingPipe(address[1]);
82 radio.openReadingPipe(1, address[0]);
83 radio.startListening(); // First we need to start listening
84
85 // Add an ACK payload for the first time around; 1 is the pipe number to acknowledge
86 radio.writeAckPayload(1, &message_count, sizeof(message_count));
87 ++message_count; // increment counter by 1 for next ACK payload
88 }
89
90 radio.printDetails(); // Dump the configuration of the rf unit for debugging
91 delay(50);
92
93 // Attach interrupt handler to interrupt #0 (using pin 2) on BOTH the sender and receiver
94 attachInterrupt(0, check_radio, LOW);
95} // setup
96
97
98void loop() {
99
100
101 /****************** Ping Out Role ***************************/
102
103 if (role == role_sender) {
104 // Repeatedly send the current time
105
106 unsigned long time = millis(); // Take the time
107 Serial.print(F("Now sending "));
108 Serial.println(time);
109 radio.startWrite(&time, sizeof(unsigned long), 0); // Send the time
110 delay(2000); // Try again soon (in 2 seconds)
111 }
112
113
114 /****************** Pong Back Role ***************************/
115 // Receiver does nothing! All the work is in Interrupt Handler
116
117 if (role == role_receiver) {}
118
119} // loop
120
121
122/********************** Interrupt Handler *********************/
123
124void check_radio(void) {
125
126 bool tx, fail, rx; // declare variables to store IRQ flags
127 radio.whatHappened(tx, fail, rx); // What happened?
128
129 if (tx) { // Have we successfully transmitted?
130 if (role == role_sender)
131 Serial.println(F("Send:OK"));
132 if (role == role_receiver)
133 Serial.println(F("Ack Payload:Sent"));
134 }
135
136 if (fail) { // Have we failed to transmit?
137 if (role == role_sender)
138 Serial.println(F("Send:Failed"));
139 if (role == role_receiver)
140 Serial.println(F("Ack Payload:Failed"));
141 }
142
143 if (rx || radio.available()) { // Did we receive a message?
144
145
146
147
148 /**************** Ping Out Role (about received ACK payload) ************************/
149 // If we're the sender, we've received an ack payload
150 if (role == role_sender) {
151 // Get the payload and dump it
152 radio.read(&message_count, sizeof(message_count));
153 Serial.print(F("Ack: "));
154 Serial.println(message_count);
155 }
156
157
158 /****************** Pong Back Role ***************************/
159 // If we're the receiver, we've received a time message
160 if (role == role_receiver) {
161 // Get the payload and dump it
162
163 static unsigned long got_time; // variable to hold the received time
164 radio.read(&got_time, sizeof(got_time)); // get the payload
165 Serial.print(F("Got payload "));
166 Serial.println(got_time);
167
168 // Add an ACK payload for the next time around; 1 is the pipe number to acknowledge
169 radio.writeAckPayload(1, &message_count, sizeof(message_count));
170 ++message_count; // increment packet counter
171 }
172 }
173} // check_radio
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
Definition RF24.h:116
@ RF24_PA_LOW
Definition RF24.h:50
#define INPUT
#define pinMode(pin, direction)
#define HIGH
#define delay(milisec)
#define LOW
#define digitalWrite(pin, value)
#define millis()
void printf_begin(void)
Definition printf.h:33