nRF24L01, radios work fine, need data type help

Hardware (working fine): 2ea Uno, each with nRF24L01 radios; Win 10; Local IDE 1.8.15; RF24 library examples work.

Problem = Inexperience: (unfamiliar with manipulating data types)
Using analogRead, I store a 10 bit value in an int (no problem). I have not been successful at processing that int for a journey through the two radios to be stored as an int on the receiving arduino.

Output of code below: As the int value (from a potentiometer) sweeps from 0 to 1023, the value on the receiving arduino's serial monitor will cycle three times from -127 to +127. Seems like only one byte is getting through.

Thanks for any help!
Dan
San Jose, CA

SENDING SIDE: (I modified sketches from Robin2 )

// SimpleTx 

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


#define CE_PIN   9
#define CSN_PIN 10

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

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

char dataToSend[2];
char txNum = '9';


unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 100; // send once per second


void setup() {

  Serial.begin(9600);

  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) {
    send();
    prevMillis = millis();
  }
}

//====================

void send() {

  bool rslt;
  rslt = radio.write( &dataToSend, sizeof(dataToSend) );
  // Always use sizeof() as it gives the size as the number of bytes.
  // For example if dataToSend was an int sizeof() would correctly return 2

  Serial.print("Data Sent ");
  Serial.print(dataToSend);
  if (rslt) {
    Serial.println("  Acknowledge received");
    updateMessage();
  }
  else {
    Serial.println("  Tx failed");
  }
}

//================

void updateMessage() {
  // so you can see that new data is being sent
  //   txNum += 1;
  //   if (txNum > '9') {
  //      txNum = '0';
  //   }


  char potValue = (analogRead(A0));

  txNum = potValue;

  dataToSend[0] = txNum;
}

RECEIVING SIDE

// SimpleRx - the slave or the receiver

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

#define CE_PIN   9
#define CSN_PIN 10
char dataReceived;
int received;

const byte thisSlaveAddress[5] = {0x52,0x78,0x41,0x41,0x41};

RF24 radio(CE_PIN, CSN_PIN);

//char dataReceived[10]; // this must match dataToSend in the TX
bool newData = false;

//===========

void setup() {

    Serial.begin(9600);

    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( &dataReceived, sizeof(dataReceived) );
      radio.read( &dataReceived, sizeof(dataReceived) );
        newData = true;
        received = dataReceived;
       
    }
}

void showData() {
    if (newData == true) {
 //       Serial.print("Data received ");
        Serial.println(received);
        newData = false;
    }
}
char dataToSend[2];
rslt = radio.write( &dataToSend, sizeof(dataToSend) );

You are sending an array of chars with 2 elements

char dataReceived;
radio.read( &dataReceived, sizeof(dataReceived) );

but trying to receive a single char

To make things worse you do this

char potValue = (analogRead(A0));

analogRead() returns an int not a char

Put the value from the analgRead() in an int then send that int and receive an int

as bob suggests

int dataToSend;
...
  dataToSend =  analogRead (A0);

Thanks Bob and gcjr, (and anyone else who replied this morning while I was testing/learning).

Your advice solved the problem. I had inaccurate notions about the data types that are acceptable to the radio classes.

For future searchers with a similar question/project. Below are my sketches that worked (mostly). I was not successful at getting the Tx to detect an ack, so I commented out ack detection flow in my Tx sketch (input on that appreciated!).

Derived from Robin2's Simple nRF24L01+ 2.4GHz transceiver demo

First Sketch is for sending the analog data collected from a potentiometer.

// Reads pot value and sends it over nRF24L01

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


#define CE_PIN   9
#define CSN_PIN 10
int dataToSend;
bool rslt;
const byte slaveAddress[5] = {0x52,0x78,0x41,0x41,0x41};

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

void setup() {

    Serial.begin(9600);

    Serial.println("SimpleTx Starting");
    radio.begin(); //loads a set of most commonly used radio settings
    radio.setDataRate( RF24_250KBPS ); 
    radio.setRetries(3,5); // delay, count
    radio.openWritingPipe(slaveAddress);
}

//====================

void loop() {

    delay(300);
    send();  
      Serial.print(dataToSend);
      Serial.print("  ack=");
      Serial.println(rslt);
}

//====================

void send() {

        // commented out because tx is not hearing ack.

    rslt = radio.write( &dataToSend, sizeof(dataToSend) );

        //    if (rslt) {
        //        Serial.println("  Acknowledge received");
        
    updateMessage();
    
        //    }
        //    else {
        //        Serial.println("  Tx failed");
        //    }
}

//================

void updateMessage() {

    dataToSend =(analogRead(A0));
}

Second sketch (below) is for displaying the potentiometer data (0 to 1023) on the serial monitor.

// Receives Pot Value displays on Serial Monitor

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

#define CE_PIN   9
#define CSN_PIN 10

int dataReceived;
const byte thisSlaveAddress[5] = {0x52,0x78,0x41,0x41,0x41};
bool newData = false;

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

//===========

void setup() {

    Serial.begin(9600);

    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( &dataReceived, sizeof(dataReceived) );
        newData = true;
       
    }
}

void showData() {
    if (newData == true) {
        Serial.println(dataReceived);
        newData = false;
    }
}

I think that it true to say that any data type is acceptable but for sanity they must be the same to both ends of the link

Great info - thank you, Bob!

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