nRF24L01 Reading Everything

Transmitter Code

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

RF24 radio(10, 9); // CE & CSN
const byte address[6] = "00001";

byte JS1Power = A0;
byte JS2Power = A3;

void setup() {

  Serial.begin(9600);
  pinMode(JS1Power, OUTPUT);
  pinMode(JS2Power, OUTPUT);
  digitalWrite(JS1Power, HIGH);
  digitalWrite(JS2Power, HIGH);

  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

void loop() {

int X_Read = analogRead(A1);
int Y_Read = analogRead(A2);

if(X_Read != 506){
  radio.write(&X_Read, sizeof(X_Read));
  }
if(Y_Read != 512){
  radio.write(&Y_Read, sizeof(Y_Read));
  }

Receiver Code

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

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

void setup() {
  Serial.begin(9600);
}

void loop() {

if (radio.available()) {
  int X_Read;
  radio.read(&X_Read, sizeof(X_Read));
  Serial.print("\n");
  Serial.print("X_Read: ");
  Serial.print(X_Read);
  Serial.print("\n");
  Serial.print(" ");`
}

I have only said &X_Read in the code but it seems to read &Y_Read as well even though I have not told it to read for it and displays itself on X_Read for some reason...?

I wanted to transfer X and Y values of a joystick from my transmitter to my receiver and have it store an X and Y value separately but I can't do that if they combine like this for some reason. I am very confused- I feel like its the library I'm using but I don't know why exactly.

The receiver has no way of knowing what you want to read. It simply reads any data that is available

Consider putting the data in a struct in the transmitter code and sending that. After you have read the data back into a struct at the receiver end you can decide what you do with each element of the struct, if anything

As an alternative consider sending an identifier character, perhaps "X" or "Y" immediately before the X_Read or Y_Read data. Then, when received you will be able to tell what type of data it is.

Omg yes! I had been wondering about structs since a few hours ago but never figured it out. I finally decided to learn about it just now methodologically and finally worked it out! Thank you!

These 2 sketches will give you some ideas

I have not used them for some time but I know that they work

Transmitter

// SimpleTx - the master or the transmitter

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

//not all #defines used but here for documentation
#define CE_PIN   9
#define CSN_PIN 10
#define MOSI_PIN 11
#define MISO_PIN 12
#define SCK_PIN 13

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char txNum = '0';

struct data
{
  byte pinState;
  byte temperature;
} sentData;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000;

void setup()
{
  Serial.begin(115200);
  Serial.println("SimpleTx Starting");
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.setRetries(3, 5); // delay, count
  radio.openWritingPipe(slaveAddress);
}

void loop()
{
  currentMillis = millis();
  if (currentMillis - prevMillis >= txIntervalMillis)
  {
    sentData.pinState = !sentData.pinState;
    sentData.temperature = sentData.temperature + 1;
    send();
    prevMillis = millis();
  }
}

void send()
{
  bool rslt;
  rslt = radio.write( &sentData, sizeof(sentData) );
  if (rslt)
  {
    Serial.println("  Acknowledge received");
  }
  else
  {
    Serial.println("  Tx failed");
  }
}

Receiver

// SimpleRx - the slave or the receiver

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

//not all #defines used but here for documentation
#define CE_PIN   9
#define CSN_PIN 10
#define MOSI_PIN 11
#define MISO_PIN 12
#define SCK_PIN 13

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

struct data
{
  byte pinState;
  int temperature;
} receivedData;

bool newData = false;

void setup()
{
  Serial.begin(115200);
  Serial.println("SimpleRx Starting");
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.openReadingPipe(1, thisSlaveAddress);
  radio.startListening();
}

void loop()
{
  getData();
  showData();
}

void getData()
{
  if ( radio.available() )
  {
    radio.read( &receivedData, sizeof(receivedData) );
    newData = true;
  }
}

void showData()
{
  if (newData == true)
  {
    Serial.print("Data received\tpinState : ");
    Serial.print(receivedData.pinState);
    Serial.print("\t");
    Serial.print("temperature : ");
    Serial.println(receivedData.temperature);
    newData = false;
  }
}

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.