Transmitting Directions with NRF24L01 + RF24.h

I’m new to the coding side of Arduino and trying to understand how to transmit the state of an analogue joystick as text or integer over a RF24 connection. I can transmit a static “hello world” but beyond that confuses the heck out of me.

    #include <SPI.h>
    #include <RF24.h>
    RF24 radio(7, 8); // CE, CSN
     char B[2] = "B";
     char F[2] = "F";
     char L[2] = "L";
     char R[2] = "R";
     char O[2] = "O";     
    const byte address[6] = "00001";
    void setup() {
      radio.begin();
      radio.openWritingPipe(address);
      radio.setPALevel(RF24_PA_MIN);
      radio.stopListening();
    }
    void loop() {

    int xAxis = analogRead(A0); // Read Joysticks X-axis
    int yAxis = analogRead(A1); // Read Joysticks Y-axis
     // Y-axis used for forward and backward control
      if (yAxis < 470) {
      // Backward
    radio.write(B);
      }
      else if (yAxis > 550) {
      // Forward
    radio.write(F);
      }
      // Off
      else {
    radio.write(O);
      }
      // X-axis used for left and right control
      if (xAxis < 470) {
      // Move to left
    radio.write(L);
        }  
      if (xAxis > 550) {
      // Move right
    radio.write(R);   
      // Off
      else {
    radio.write(O);
      }
        } 
    }

Am I going about this in a very backward way? any help would be GREATLY appreciated.

Thanks

What does your receiving side look like?

It might make it easier to parse if you didn't use 'O' to mean both x axis is centered and y axis is centered. Give them their own letters.

Robin2's simple rf24 tutorial may have information that will help.

Personally I would read all of the analogue values and put them in a struct and send that with one write() command, read the data on the Rx side and interpret it there

Code to send a struct

// 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");
  }
}

Code to receive a struct

// 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;
  }
}

NOTE : the above code is an example only. It does not read and transmit analogue inputs or interpret them on the Rx side. An array of ints could be used instead of the struct but I find the use of a struct useful in the case where data of differing types may be involved

Both of the above are based on the example programs in Simple nRF24L01+ 2.4GHz transceiver demo

You are constantly transmitting 2 bytes for each axis, which is unnecessary. You should transmit one byte where the four low-order bits contains the state of the joystick. This byte should only be transmittet when it changes or at least once every second.

My claim ist that the code never sent anything and did not compile.

    radio.write(B);

All the writes only have a single parameter, I'm not aware of a library function that would allow that.