nRF24l01 not receiving data struct

so i was making a tank project using nRF modules with arduino unos, but the thing is, this data struct is not being received by the module. i have tried various different methods of writing data struct but it just isn't working. the transmitter side writes, but the receiver dosen't listen. this is happening only with my code, as i have used test codes for sending and receiving data and they work perfectly. i even tried editing those same codes according to my needs, but it still dosen't work. at present these are the codes i'm using:-

TRANMITTER

#include <nRF24L01.h> // nrf library 
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

#include <SPI.h>
#include <Servo.h>
#include <Encoder.h>

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

// Define analog input pins for joysticks and flex sensor
const int xAxisPin = A0;
const int yAxisPin = A1;
const int flexSensorPin = A2;
const int encoderPinA = 2;
const int encoderPinB = 3;

Encoder myEncoder(encoderPinA, encoderPinB);

struct SensorData {
  int xAxisValue; 
  int yAxisValue; 
  int steps; 
  int flexSensorValue;
};

void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_LOW);  // Set power level (low for short-range testing)
  radio.stopListening();  // Put the radio in TX mode

  pinMode(xAxisPin, INPUT);
  pinMode(yAxisPin, INPUT);
  pinMode(flexSensorPin, INPUT);
  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);

}

void loop() {
  SensorData sensorReading;
  
  // Simulate sensor data (replace with actual sensor readings)
  sensorReading.xAxisValue = map(analogRead(xAxisPin), 0, 1023, -255, 255); // Joystick X-axis value;
  sensorReading.yAxisValue = map(analogRead(yAxisPin), 0, 1023, -255, 255); // Joystick Y-axis value;
  sensorReading.flexSensorValue = map(analogRead(flexSensorPin), 0, 1023, 0, 180); // Flex sensor value (0-180 degrees)
  sensorReading.steps = myEncoder.read();

  // Send the sensor data as a struct
  radio.write(&sensorReading, sizeof(sensorReading));
  delay(1000);  // Delay between transmissions
}

RECEIVER

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

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

#include <SPI.h>
#include <Wire.h>
#include <Servo.h>
#include <Adafruit_MotorShield.h>

Adafruit_MotorShield AFMS1 = Adafruit_MotorShield(); // First Motor Shield
Adafruit_MotorShield AFMS2 = Adafruit_MotorShield(); // Second Motor Shield

Adafruit_StepperMotor *stepper = AFMS1.getStepper(200, 1);
Adafruit_DCMotor *leftMotor = AFMS1.getMotor(3);
Adafruit_DCMotor *rightMotor = AFMS1.getMotor(4);
Adafruit_DCMotor *turret = AFMS2.getMotor(1);

Servo flexServo;

struct SensorData {
  int xAxisValue;
  int yAxisValue;
  int steps;
  int flexSensorValue;
};

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1, address);
  radio.setPALevel(RF24_PA_LOW);  // Set power level (low for short-range testing)
  radio.startListening();  // Put the radio in RX mode

  AFMS1.begin();
  AFMS2.begin();
  stepper->setSpeed(100); // Adjust the speed as needed based on your motor
  flexServo.attach(10); // Servo motor pin
}

void loop() {
  if (radio.available()) {
    SensorData receivedData;
    radio.read(&receivedData, sizeof(receivedData));
    
    // Process the received sensor data and control motors based on the values
    controlMotors(receivedData.xAxisValue, receivedData.yAxisValue, receivedData.steps, receivedData.flexSensorValue);
  }
}

void controlMotors(int xAxisValue, int yAxisValue, int steps, int flexSensorValue) {
  // Control logic for motors, stepper, and servo goes here.
  leftMotor->setSpeed(abs(yAxisValue));
  rightMotor->setSpeed(abs(yAxisValue));

  leftMotor->run(yAxisValue > 0 ? FORWARD : BACKWARD);
  rightMotor->run(yAxisValue > 0 ? FORWARD : BACKWARD);

  if (xAxisValue > 0) {
    leftMotor->run(FORWARD);
    rightMotor->run(BACKWARD);
  } else if (xAxisValue < 0) {
    leftMotor->run(BACKWARD);
    rightMotor->run(FORWARD);
  } else {
    leftMotor->setSpeed(0);
    rightMotor->setSpeed(0);
  }

  stepper->step(steps, FORWARD, SINGLE);

  flexServo.write(flexSensorValue);

  turret->setSpeed(abs(xAxisValue));
  turret->run(xAxisValue < 0 ? FORWARD : BACKWARD);

  Serial.print("Motor Control - xAxisValue: ");
  Serial.print(xAxisValue);
  Serial.print(", yAxisValue: ");
  Serial.print(yAxisValue);
  Serial.print(", steps: ");
  Serial.println(steps);
  Serial.print(", flexSensorValue: ");
  Serial.println(flexSensorValue);
}

I suspect "sensorReadings" is a pointer to "SensorData", so you are NOT sending the length of what you think. Try using the actual length of "SensorData" and see if that fixes the problem.

can you explain a bit more, i didn't understand that :sweat_smile:

Do you know what a pointer is and it's length? Length should be 2 bytes. IS that what your receiver is getting?

you mean like this??

radio.read(&receivedData, sizeof(receivedData));

Your original post says you are not receiving something. How do you know that is true? Did you serial.Print() it so you know? The problem is you are NOT using the length of the struct in the transmit code. You are using the length of the pointer to the struct.

I did use serial.println, the original code which included only 3 variables was working fine and i was getting the readings in the serial monitor, but after changing the number of variable, it stopped working. also in the original code, those values wee fixed, however now it is taking inputs from sensors

now that i wrote serialprint for the size of sensor reading, im getting 8, so i think you are right about the pointer stuff

Did you check to see what is different in the two programs? Does the new program use a pointer and the old program does not?

no, both the programs use the same pointers. this is the old code btw:-

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

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

struct SensorData {
  float temperature;
  int humidity;
  int lightIntensity;
};

void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_LOW);  // Set power level (low for short-range testing)
  radio.stopListening();  // Put the radio in TX mode
}

void loop() {
  SensorData sensorReading;
  
  // Simulate sensor data (replace with actual sensor readings)
  sensorReading.temperature = 25.5;
  sensorReading.humidity = 60;
  sensorReading.lightIntensity = 500;

  // Send the sensor data as a struct
  radio.write(&sensorReading, sizeof(sensorReading));
  delay(1000);  // Delay between transmissions
}

see the sensorReading part is the same (this is the transmitter btw)

Yes, they are the same.
I guess the next thing is to begin serial.Print() the values you are sending and the values you are receiving and see which ones are different.

thats the thing, im not receiving anything, there is nothing getting printed on the receiver side in my code. either the transmitter side prints successful transmission or it prints transmission failed, but both the times, i don't receive anything

Then fix that problem first.

bruh, that IS THE PROBLEM

The problem is in the things you haven't done.

You haven't shown a schematic, as requested in How to get the best out of this forum. So we have no idea how you are powering the nRF modules. You could be using an adapter. You could be plugging into the Uno's 3.3V pin. You could be using a LDO 3.3V regulator that's properly bypassed. But we don't know, because you haven't shown us.

You haven't checked the return from radio.begin(). So you have no idea if you're even communicating with the module.

You haven't checked the return from radio.write(). So you have no idea if the transmission succeeded or failed.

You never look at the value of radio.failureDetected. The library could be telling you there's a problem, but you'll never know because you don't check for it.

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