Example to detect interference on the various channels available. This is a good diagnostic tool to check whether you're picking a good channel for your application.
1
2
3
4
5
6
7
8
9
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
51
52
53
54
55
56#define CE_PIN 7
57#define CSN_PIN 8
58
59RF24 radio(CE_PIN, CSN_PIN);
60
61
62
63
64
65const uint8_t num_channels = 126;
66uint8_t values[num_channels];
67
68
69
70
71const uint8_t noiseAddress[][2] = { { 0x55, 0x55 }, { 0xAA, 0xAA }, { 0xA0, 0xAA }, { 0xAB, 0xAA }, { 0xAC, 0xAA }, { 0xAD, 0xAA } };
72
73const int num_reps = 100;
74bool constCarrierMode = 0;
75
76void printHeader();
77
78
79void setup(void) {
80
81
82 Serial.begin(115200);
83 while (!Serial) {
84
85 }
86 Serial.println(F("RF24/examples/scanner/"));
87
88
89 if (!radio.begin()) {
90 Serial.println(F("radio hardware not responding!"));
91 while (true) {
92
93 }
94 }
95 radio.stopConstCarrier();
96 radio.setAutoAck(false);
97 radio.disableCRC();
98 radio.setAddressWidth(2);
99 for (uint8_t i = 0; i < 6; ++i) {
100 radio.openReadingPipe(i, noiseAddress[i]);
101 }
102
103
104 Serial.print(F("Select your Data Rate. "));
105 Serial.print(F("Enter '1' for 1 Mbps, '2' for 2 Mbps, '3' for 250 kbps. "));
106 Serial.println(F("Defaults to 1Mbps."));
107 while (!Serial.available()) {
108
109 }
110 uint8_t dataRate = Serial.parseInt();
111 if (dataRate == 50) {
112 Serial.println(F("Using 2 Mbps."));
114 } else if (dataRate == 51) {
115 Serial.println(F("Using 250 kbps."));
117 } else {
118 Serial.println(F("Using 1 Mbps."));
120 }
121 Serial.println(F("***Enter a channel number to emit a constant carrier wave."));
122 Serial.println(F("***Enter a negative number to switch back to scanner mode."));
123
124
125 radio.startListening();
126 radio.stopListening();
127 radio.flush_rx();
128
129
130
131
132
133
134 printHeader();
135}
136
137void loop(void) {
138
139
140
141 if (Serial.available()) {
142 int8_t c = Serial.parseInt();
143 if (c >= 0) {
144 c = min((int8_t)125, c);
145 constCarrierMode = 1;
146 radio.stopListening();
148 Serial.print("\nStarting Carrier Wave Output on channel ");
149 Serial.println(c);
150
152 } else {
153 constCarrierMode = 0;
154 radio.stopConstCarrier();
155 radio.setAddressWidth(2);
156 radio.openReadingPipe(0, noiseAddress[0]);
157 Serial.println("\nStopping Carrier Wave Output");
158 printHeader();
159 }
160
161
162 while (Serial.peek() != -1) {
163 if (Serial.peek() == '\r' || Serial.peek() == '\n') {
164 Serial.read();
165 } else {
166 break;
167 }
168 }
169 }
170
171
172
173 if (constCarrierMode == 0) {
174
175 memset(values, 0, sizeof(values));
176
177
178 int rep_counter = num_reps;
179 while (rep_counter--) {
180 int i = num_channels;
181 while (i--) {
182
183 radio.setChannel(i);
184
185
186 radio.startListening();
188 bool foundSignal = radio.testRPD();
189 radio.stopListening();
190
191
192 if (foundSignal || radio.testRPD() || radio.available()) {
193 ++values[i];
194 radio.flush_rx();
195 }
196 }
197 }
198
199
200 for (int i = 0; i < num_channels; ++i) {
201 if (values[i])
202 Serial.print(min((uint8_t)0xf, values[i]), HEX);
203 else
204 Serial.print(F("-"));
205 }
206 Serial.println();
207
208 }
209 else {
210
211 Serial.print(F("."));
213 }
214}
215
216void printHeader() {
217
218 for (uint8_t i = 0; i < num_channels; ++i)
219 Serial.print(i / 100);
220 Serial.println();
221
222
223 for (uint8_t i = 0; i < num_channels; ++i)
224 Serial.print((i % 100) / 10);
225 Serial.println();
226
227
228 for (uint8_t i = 0; i < num_channels; ++i)
229 Serial.print(i % 10);
230 Serial.println();
231
232
233 for (uint8_t i = 0; i < num_channels; ++i)
234 Serial.print(F("~"));
235 Serial.println();
236}
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
#define delayMicroseconds(usec)