Hey guys,
I'm having a slight problem getting a pair nRF24s to work. They are working fine with the Getting Started example that comes with TMRH20's RF24 library.
The radios are connected to Unos. One of the units will be connected to a PC with USB. The PC will send 4-byte commands through the serial port and the radio will relay that to the second unit which will print the command out of its serial port.
The concept is simple so I think I may be missing something simple. The problem may be that I'm not handling the data types correctly. I have attached the code below, I'm just using a single sketch for simplicity and changing the role variable for the Transmitter and Receiver. Can anyone see what could be wrong?
Cheers!
#include <SPI.h>
#include "RF24.h"
/****************** User Config ***************************/
/*** Set this radio as radio number 0 or 1 ***/
bool radioNumber = 0;
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(7,8);
/**********************************************************/
byte addresses[][6] = {"1Node","2Node"};
// Used to control whether this node is sending or receiving
bool role = 0;
void setup() {
Serial.begin(9600);
radio.begin();
// Set the PA Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
radio.setPALevel(RF24_PA_LOW);
// Open a writing and reading pipe on each radio, with opposite addresses
if(radioNumber){
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}
// Start the radio listening for data
radio.startListening();
}
void loop() {
/****************** Tx Role ***************************/
if (role == 1) {
if(Serial.available()) {
byte incomingByte;
radio.stopListening(); // First, stop listening so we can talk
incomingByte = Serial.read();
radio.write(&incomingByte, sizeof(incomingByte));
}
}
/****************** Rx Role ***************************/
if ( role == 0 )
{
if( radio.available()){
while (radio.available()) { // While there is data ready
byte got_payload;
radio.read( &got_payload, sizeof(byte) ); // Get the payload
Serial.print(got_payload);
}
}
}
}