I am trying to use an arduino uno with 1 NRF24L01 module to send what is received through the serial port to a seperate arduino uno with a second NRF24L01. What happens with the following code is that RelaySwitch[0] is correct with the corresponding 48 or 49. Now when I serial.println the RelaySwitch[1] it prints 2569 and only 2569. On the receiver it just prints no radio available. I can't figure this out. The end objective is to create a UI with C# and on the other end I want to change the state of a Relay.
Transmit Code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
int incomingByte;
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int RelaySwitch[1]; // 1 element array holding Joystick readings
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
if (Serial.available() > 0){
incomingByte = Serial.read();
RelaySwitch[0] = incomingByte;
radio.write( RelaySwitch, sizeof(RelaySwitch) );
Serial.println(RelaySwitch[1]);
}
}//--(end main loop )---
Receive code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int RelaySwitch[1]; // 1 element array holding Joystick readings
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
delay(1000);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
if ( radio.available() )
{
// Read the data payload until we've received everything
bool done = false;
while (!done)
{
// Fetch the data payload
done = radio.read( RelaySwitch, sizeof(RelaySwitch) );
Serial.println(RelaySwitch[0]);
}
}
else
{
Serial.println("No radio available");
}
}//--(end main loop )---
How far apart are your radios? I have solved similar problems by reducing transmitter power,
if they are close together (< 20 ft.) the receiver can be swamped.
Insert in setup().
OOPS! Sorry for delay in replying.
No you only need to reduce power on transmitter but make sure the data rate is the same on both ends, which ever you choose from 250kbps or 1Mbps or 2Mbps.
Leave your receiver code as is.
That would be the first time I have ever seen someone complain about the power of their radio.
As for the OP's problem:
if ( radio.available() )
{
// Read the data payload until we've received everything
bool done = false;
while (!done)
{
// Fetch the data payload
done = radio.read( RelaySwitch, sizeof(RelaySwitch) );
Serial.println(RelaySwitch[0]);
}
}
else
{
Serial.println("No radio available");
}
There are several problems with this.
It is likely that sizeof is not doing what you expect here. There are a lot of issues with sizeof, I manage to get by not using it, I don't know why they teach beginners to use it, with all the mistakes they make.
The other problem is that your loop( ) function is going to be running thousands of times a minute, and even if your radio communication is actually working, most of the time there will be no radio transmission and your serial monitor will be swamped with more messages that "no radio available", than it will be able to handle.