making an arduino + nRF24 wait for a serial command to transfer data.

I am able to transfer data from one arduino to another using nRF24L01 chips. I followed some tutorials on this without trouble. However, I need the data to be sent ONLY when a specific command is sent on the serial port. I want to be able to write ‘A’ on the serial port, and for arduino to reply with the latest data. This is easy with the serialcommand library and works if I am directly plugged in. However, the moment I add the nRF24L01 module, serialcommand.h does not respond with the data anymore. I tried even removing the if (radio.available()) … so no data would be received and just the fact that there is radio.begin() in setup makes the serialcommand ‘A’ not work. when I remove the radio stuff, the command ‘A’ does reply with three zeros (default data).

Here is my receiver code. Transmitter is very standard and i know it works. Any help or ideas as to why this happens is very appreciated!

#include <SoftwareSerial.h>
#include <SerialCommand.h>

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

SerialCommand sCmd;

RF24 radio(7, 8 ); // CE, CSN
const byte address[6] = “00001”;

int Array[3];

void setup() {
Serial.begin(9600);
sCmd.addCommand(“A”, serialdataPrint );

radio.begin();
radio.setRetries(15, 15);
radio.openReadingPipe(0,address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}

void loop() {
while (Serial.available() > 0){

sCmd.readSerial();

if (radio.available()) {

int Array_received[3];

radio.read(&Array_received, sizeof(Array_received));

Array[0] = Array_received[0];
Array[1] = Array_received[1];
Array[2] = Array_received[2];
}
}
}

void serialdataPrint ()
{
Serial.println(Array[0]);
Serial.println(Array[1]);
Serial.println(Array[2]);
}

BasicReceiver.ino (923 Bytes)

The sCmd.readSerial() function polls Serial to see if any characters are available. Your loop() function only executes this code if a char is available. Since sCmdreadSerial() reads those characters, your while() statement is never true so it doesn’t get executed any more.

You should remove your test

And please learn how to post code using code tags

// this library is not used
//#include <SoftwareSerial.h>
#include <SerialCommand.h>

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

SerialCommand sCmd;

RF24 radio(7, 8 ); // CE, CSN
const byte address[6] = "00001";

int Array[3];

void setup() {
  Serial.begin(9600);
  sCmd.addCommand("A", serialdataPrint );

  radio.begin();
  radio.setRetries(15, 15);
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {

  sCmd.readSerial();
  if (radio.available()) {
    radio.read(&Array, sizeof(Array));  // why not use your global Array[] directly?
  }
}


void serialdataPrint()
{
  Serial.println(Array[0]);
  Serial.println(Array[1]);
  Serial.println(Array[2]);
}

At 9600 bps it takes 10 ms for a single character to arrive.

There's no need for a while() loop, just check for Serial.available() and read the one character that is in the buffer. Unless you're doing LOTS of other things you won't find more than one character at a time there.

You also never call serialdataPrint(), and you don't seem to set a speed for your SoftwareSerial instance.

These links may be of interest

Serial Input Basics - simple reliable ways to receive data.
Simple nRF24L01+ Tutorial

...R

wvmarle:
At 9600 bps it takes 10 ms for a single character to arrive.

There's no need for a while() loop, just check for Serial.available() and read the one character that is in the buffer. Unless you're doing LOTS of other things you won't find more than one character at a time there.

You also never call serialdataPrint(), and you don't seem to set a speed for your SoftwareSerial instance.

That's what the SerialCommand library does for you. It reads the input. It parses the input and calls commands you specify as a callback [e.g. sCmd.addCommand("A", serialdataPrint );]

Thank you everyone for your replies.

I do not know why there was a while loop to be honnest. i took it from another working example but now that i think about i see that it is not necessary. I will re-write the code and keep you posted when it works.

cheers,
Alexis