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