nRF24L01 Help!

Hello there,

I’ve written a code to send a number from an arduino nano to an arduino uno. The number will always be a whole number and will be between 1-48. I cant seem to get the nrf units to talk to eachother. Ive been through example codes, multiple libraries and the nrf guide on here with no avail.

Here is the code that takes care of the TX:

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

RF24 radio(7, 8);
const byte rxAddr[6] = "96920";
int data[1]; 

//devices
int vibe = 4;
int fire = 3;
int select = 2;
int mode = 5;
int fled = 6;
int lsw = 9;
int laser = 10;

//variables
int total = 12;
int fval = 0;
int charge = 0;
int mval = 0;
int lsval = 0;
int sval = 0;

void setup()
{
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(rxAddr);
  radio.stopListening();
  pinMode(vibe, OUTPUT);
  pinMode(fire, INPUT);
  digitalWrite(fire, HIGH);
  pinMode(select, INPUT);
  pinMode(mode, INPUT);
  pinMode(fled, OUTPUT);
  pinMode(lsw, INPUT);
  pinMode(laser, OUTPUT);
  digitalWrite(vibe, LOW);
  digitalWrite(fled, LOW);
  digitalWrite(laser, LOW);
  delay(200);
  digitalWrite(vibe, HIGH);
  digitalWrite(fled, HIGH);
  //digitalWrite(laser, HIGH);
  delay(200);
  digitalWrite(vibe, LOW);
  digitalWrite(fled, LOW);
  digitalWrite(laser, LOW);
  delay(200);
  digitalWrite(vibe, HIGH);
  digitalWrite(fled, HIGH);
  // digitalWrite(laser, HIGH);
  delay(200);
  digitalWrite(vibe, LOW);
  digitalWrite(fled, LOW);
  digitalWrite(laser, LOW);
  delay(200);
}

void loop() {

  rs();
  sval = digitalRead(select);
  Serial.print("Select: ");
  Serial.println(sval);
  if (sval == 1) {
    charge = ++charge;
    sv();
  }
  Serial.print("Charge: ");
  Serial.println(charge);
  if (charge >= (total + 1) && charge != 86) {
    charge = 1;
  }
  fval = digitalRead(fire);
  Serial.print("Fire: ");
  Serial.println(fval);
  if (fval == 1) {
    detonate();
  }
  delay(50);
}

void rs() {
  mval = digitalRead(mode);
  lsval = digitalRead(lsw);
  Serial.print("Mode: ");
  Serial.println(mval);
  Serial.print("Laser: ");
  Serial.println(lsval);
  if (mval == 1) {
    charge = 86;
  }
}

void detonate() {
data[0] = charge;
  radio.write(&data, sizeof(data));
  Serial.print("Firing: ");
  Serial.println(charge);
  digitalWrite(vibe, HIGH);
  digitalWrite(fled, HIGH);
  if (lsval == 1) {
    digitalWrite(laser, HIGH);
  }
  delay(200);
  digitalWrite(vibe, LOW);
  digitalWrite(fled, LOW);
  // delay(200);
  digitalWrite(laser, LOW);
  charge = ++charge;
}

void sv() {
  if (charge > total) {
    digitalWrite(vibe, HIGH);
    delay(350);
    digitalWrite(vibe, LOW);
    delay(50);
    digitalWrite(vibe, HIGH);
    delay(350);
    digitalWrite(vibe, LOW);
    return;
  }
  int i = 0;
  while (i < charge) {
    digitalWrite(vibe, HIGH);
    delay(150);
    digitalWrite(vibe, LOW);
    delay(100);
    i = ++i;
  }
}

and for now all i am trying to do with the RX is display the number in the serial window

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte rxAddr[6] = "96920";
int data[1];

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, rxAddr);
  radio.startListening();
}
void loop() {
  if (radio.available()) {
    radio.read( &data, sizeof(data));
//Serial.println(data);
 int val = data;
 Serial.println(val);
  }
}

Any help would be greatly appreciated.

Have you looked at and tried the examples in Simple nRF24L01+ 2.4GHz transceiver demo

I suggest that you try Robin2's simple rf24 demo (linked above). Upload the send and receive code unmodified (make sure the pins are set as in the programs). Once the code has uploaded, remove and reapply power from the Arduinos. The rf24 will not reset when the Arduino resets so needs a power cycle to reset.

Robin2's tutorial is the best that I have seen and I have been using those radios for years.

Okay, so after thoroughly reading through the linked codes I got my nRF modules working 100%. Now when I tried to merge the examples with my code what I ended up with was a rx module receiving messages consistentantly but not the correct number. Looking at my tx code, added serial debugging and realized my issue is with getting int’s into a char array. Ive went over the array page in the reference section but just am not getting it. Here is my current tx code:

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

#define CE_PIN 7
#define CSN_PIN 8

const byte rxAddr[6] = {'P', 'y', 'r', '0', '8', '6'};
RF24 radio(CE_PIN, CSN_PIN);
char data[3];
char num1 = '0';
char num2 = '0';

//devices
int vibe = 4;
int fire = 3;
int select = 2;
int mode = 5;
int fled = 6;
int lsw = 9;
int laser = 10;

//variables
int total = 12;
int fval = 0;
int charge = 0;
int mval = 0;
int lsval = 0;
int sval = 0;

void setup()
{
  Serial.begin(9600);
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.openWritingPipe(rxAddr);
  //radio.stopListening();
  pinMode(vibe, OUTPUT);
  pinMode(fire, INPUT);
  digitalWrite(fire, HIGH);
  pinMode(select, INPUT);
  pinMode(mode, INPUT);
  pinMode(fled, OUTPUT);
  pinMode(lsw, INPUT);
  pinMode(laser, OUTPUT);
  digitalWrite(vibe, LOW);
  digitalWrite(fled, LOW);
  digitalWrite(laser, LOW);
  delay(200);
  digitalWrite(vibe, HIGH);
  digitalWrite(fled, HIGH);
  //digitalWrite(laser, HIGH);
  delay(200);
  digitalWrite(vibe, LOW);
  digitalWrite(fled, LOW);
  digitalWrite(laser, LOW);
  delay(200);
  digitalWrite(vibe, HIGH);
  digitalWrite(fled, HIGH);
  // digitalWrite(laser, HIGH);
  delay(200);
  digitalWrite(vibe, LOW);
  digitalWrite(fled, LOW);
  digitalWrite(laser, LOW);
  delay(200);
}

void loop() {

  rs();
  sval = digitalRead(select);
  Serial.print("Select: ");
  Serial.println(sval);
  if (sval == 1) {
    charge = ++charge;
    sv();
  }
  Serial.print("Charge: ");
  Serial.println(charge);
  if (charge >= (total + 1) && charge != 86) {
    charge = 1;
  }
  fval = digitalRead(fire);
  Serial.print("Fire: ");
  Serial.println(fval);
  if (fval == 1) {
    detonate();
  }
  delay(50);
}

void rs() {
  mval = digitalRead(mode);
  lsval = digitalRead(lsw);
  Serial.print("Mode: ");
  Serial.println(mval);
  Serial.print("Laser: ");
  Serial.println(lsval);
  if (mval == 1) {
    charge = 86;
  }
}

void detonate() {
  datawrite();
  radio.write(&data, sizeof(data));
  Serial.print("Firing: ");
  Serial.println(data);
  digitalWrite(vibe, HIGH);
  digitalWrite(fled, HIGH);
  if (lsval == 1) {
    digitalWrite(laser, HIGH);
  }
  delay(200);
  digitalWrite(vibe, LOW);
  digitalWrite(fled, LOW);
  // delay(200);
  digitalWrite(laser, LOW);
  charge = ++charge;
}

void sv() {
  if (charge > total) {
    digitalWrite(vibe, HIGH);
    delay(350);
    digitalWrite(vibe, LOW);
    delay(50);
    digitalWrite(vibe, HIGH);
    delay(350);
    digitalWrite(vibe, LOW);
    return;
  }
  int i = 0;
  while (i < charge) {
    digitalWrite(vibe, HIGH);
    delay(150);
    digitalWrite(vibe, LOW);
    delay(100);
    i = ++i;
  }
}
void datawrite() {
  int val1;
  int val2;
  
  if (charge < 10) {
    num1 = '0';
    val2 = charge % 10;
    num2 = 'val2';
    data[0] = num1;
    data[1] = num2;
  }
  else if (charge > 9) {
    val1 = charge / 10;
    val2 = charge % 10;
    num1 = 'val1';
    num2 = 'val2';
    data[0] = num1;
    data[1] = num2;
  }
}

I know the issue is how I am getting my variable “charge” into the array that is being sent but not sure how to fix it.

Thanks

    num2 = 'val2';
    data[0] = num1;
    data[1] = num2;

You are trying to put 'val2' into single character variable num2

What exactly do you want to transmit and receive ?

If it is charge then why not simply send that ?

radio.write(&charge, sizeof(charge));

I was under the impression the nrf needed a char array to send. If I used radio.write(&charge, sizeof(charge)); would I still need to declare the data array at the beginning? Also for the receiver would the code still be reading the data from the radio into an array or can I read directly into int’s?

If you are going to send an int then you don't need the array unless you use it for something else

At the Rx end just read the value into an int variable. No need for the array their either

ZakAttack92:
I was under the impression the nrf needed a char array to send. If I used radio.write(&charge, sizeof(charge)); would I still need to declare the data array at the beginning? Also for the receiver would the code still be reading the data from the radio into an array or can I read directly into int's?

You can send any type of data as long as the total message fits within 32 bytes. And a message can contain different datatypes if you use a struct

It will be much much easier to help if you give us some examples of the data you want to send.

...R

Thank you guys! Once sending the values as integers everything worked exactly how I wanted it to. Thanks for clarifying that the nrf's are not data type specific.