Getting random values while using int to send data using nRF24L01... NEED HELP!!

Hi,

So I’m writing this code to build an rc transmitter and receiver. So far I have been able to successfully sent byte data to and fro between two nrf24l01 hooked up to an Arduino Uno and Mega (because I only have these two boards). But the problem is when I try to sent integers values using int, I get random values on the receiving end between -32,768 to 32,767 and if I use unsigned int I get value from 0 to 65,535.

This is the code for transmitter

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

RF24 radio(53, 48); 
const uint64_t pipe[2] = {0xE8E8F0F0E1LL, 0xE8E8F0F0E2LL };


struct Data_Package
{
  byte j1PotX;
};

struct Data_Package2
{
  unsigned int in1 = 0;
};

Data_Package data;
Data_Package2 data2;

void setup()
{
  Serial.begin(57600);
  printf_begin();
  radio.begin();
  //radio.openWritingPipe(address);
  radio.openWritingPipe(pipe[0]);
  radio.openReadingPipe(1, pipe[1]);
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_LOW);
  radio.printDetails();
}

void loop()
{
  data.j1PotX = map(analogRead(A1), 0, 1023, 0, 255);
  radio.stopListening();
  radio.write(&data, sizeof(Data_Package));
  radio.startListening();
  unsigned long started_waiting_at = millis();
  bool timeout = false;
  while (!radio.available() && !timeout)
  {
    if ((millis() - started_waiting_at) > 10)
      timeout = true;

    if (timeout)
    {
      Serial.println("Not Receiving mega");
    }
    else
    {
      radio.read(&data2, sizeof(Data_Package2));
      //int i = map(data2.in1,0,255,0,1023);
      Serial.println(data2.in1);
    }
  }

}

This is the code for receiver

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


RF24 radio(9, 10);
const uint64_t pipe[2] = {0xE8E8F0F0E1LL, 0xE8E8F0F0E2LL };


struct Data_Package
{
  byte j1PotX;
};

struct Data_Package2
{
  unsigned int in1;
};

Data_Package data; 
Data_Package2 data2;

void setup()
{
  Serial.begin(57600);
  printf_begin();
  radio.begin();
  //radio.openReadingPipe(0, address);
  radio.openWritingPipe(pipe[1]);
  radio.openReadingPipe(1, pipe[0]);
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_LOW);
  radio.printDetails();
}
void loop()
{

  radio.startListening();
  unsigned long started_waiting_at = millis();
  bool timeout = false;
  while (!radio.available() && !timeout)
  {
    if ((millis() - started_waiting_at) > 10)
      timeout = true;

    if (timeout)
    {
      Serial.println("Not Receiving uno");
    }
    else
    {

      radio.read(&data, sizeof(Data_Package));
      Serial.println(data.j1PotX);
    }
  }
  radio.stopListening();
  data2.in1 = analogRead(A0); //map(analogRead(A0),0,1023,0,255);
  Serial.print("\t");
  Serial.print(data2.in1);
  Serial.print("\t");
  radio.write(&data2, sizeof(Data_Package2));
}

There is a potentiometer connected to the analog of each Arduino.

If you look at transmitter code, you can see that as I read the analog pin I’m also mapping the value to 0-255 and then sending this mapped value using a byte variable, which successfully transmitted and received at the other end.

But in the receiver code, I’m using int instead of byte to send the data. I wanted to send a value ranging from 0 to 1023. If im using byte I can only send value from 0 to 255. When I use int, I’m getting random values at the receiving end. It’s more like the values from 0 to 1023 is converted to 0 to 65,535, but as I turn the potentiometer knob the received value goes from 0 to 65535 and it becomes 0 and then again reaches 65535.
In short when I rotate the knob from one end to the other, the value goes from 0 to 65535 twice.

Please help.
Thanks in Advance.

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

The third example works by switching between listening and talking, though I don't use that system myself. I prefer the simplicity of the ackPayload for 2-way communication.

...R

Robin2:
Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

The third example works by switching between listening and talking, though I don't use that system myself. I prefer the simplicity of the ackPayload for 2-way communication.

...R

I tried to send int variables separately, and this is what I got

what I tired to send what I received
0 0
1 257
10 2570
100 25700
101 25957
102 26214
200 -14136

what does this mean? Any idea?

code_red:
I tried to send int variables separately, and this is what I got

what I tired to send what I received
0 0
1 257
10 2570
100 25700
101 25957
102 26214
200 -14136

what does this mean? Any idea?

I have no idea what it means unless you post the two programs that YOU have uploaded to your Arduinos and which have produced that output.

My very wild guess is that there is a mismatch between the datatypes at the Tx and Rx

...R

Robin2:
I have no idea what it means unless you post the two programs that YOU have uploaded to your Arduinos and which have produced that output.

My very wild guess is that there is a mismatch between the datatypes at the Tx and Rx

...R

I have posted the code in the original post, if you check the beginning of this thread you can find the code.

I think I have solved the issue. It seems there was something wrong with the nrf24l01 library that I was using. Then I tried again after changing to a different library and the code worked.
The library I was using was called RF24-master (or something like that), then I changed to RF24 by TMRH20.

Well, the issue is solved now. and I thank you for your time.
cheers,

Where is the code where you “tried to send the values separately”?

Those values that you list, at the receive end, seem to be the signed integer representation of n*256+n.

That would seem to imply that you somehow tried to send a high byte and low byte, but messed up and either...
a) at the transmitter send the low byte twice
b) at the receiver used the low byte twice to reconstruct the 16-bit signed int