uable to transmit nrf 2401, 5 int datastruct

I have a code that the transmitter won’t change anymore for this experiment, and the reciever WILL change a lot once I can start getting data transmitted.

I have sent such data successfully before, but not as much, and still don’t understand some things about the successful code, which is copied from examples.

The receiver last received basically zeros, although I only checked one variable. I seem to think it should be working. I don’t really understand the line "byte addresses[10], and did change the number from examples. I don’t see why I need to put “1node, 2node”, and so left it in last time., but don’t understand the line really at all.

I don’t verify the transit, which IS in the example.
here are both codes. again, the second code shouldn’t probably work the data yet, I still haven’t managed to achieve transmission to start playing with the data to see how to use it for my desired experiment. THe experiment is to use buttons to select one of two quadruped legs, and drive the xyz of the foot selected.

Transmit

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

#define joyStick_x A0
#define joyStick_y A1
#define joyStick_z A2
#define leftFrontSelect 4
#define rightFrontSelect 2
const int lf_on = 5;
const int rf_on = 3;

int lfs_button = 0;
int rfs_button = 0;
int lfs_led = 0;
int rfs_led = 0;

RF24 radio (7,8);

byte addresses [][10] = {"1Node","2Node"};

struct dataStruct 
{
  int Xposition;
  int Yposition;
  int Zposition;
  int lf_select;
  int rf_select;
} myData;
void setup() {
  // put your setup code here, to run once:
pinMode(leftFrontSelect, INPUT);
pinMode(rightFrontSelect, INPUT);
pinMode(lf_on, OUTPUT);
pinMode(rf_on, OUTPUT);
digitalWrite(lf_on, LOW);
digitalWrite(rf_on, LOW);
Serial.begin(115200);
radio.begin();
radio.setChannel(108);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
radio.startListening();

}

void loop() {
  // put your main code here, to run repeatedly:
lfs_button = digitalRead(leftFrontSelect);
rfs_button = digitalRead(rightFrontSelect);

if(lfs_button == HIGH)
{
  Serial.println(lfs_button);
  
  if (lfs_led == 0)
  {
    digitalWrite(lf_on, HIGH); 
    digitalWrite(rf_on, LOW);
  lfs_led = 1; 
  rfs_led = 0;
  Serial.println(lfs_led);
  }
  
} 
if(rfs_button == HIGH)
{
  if(rfs_led == 0)
  {
    digitalWrite(rf_on, HIGH);
    digitalWrite(lf_on, LOW);
  rfs_led = 1;
  lfs_led = 0;
  }
  
}
radio.stopListening();
myData.Xposition = analogRead(joyStick_x);
myData.Yposition = analogRead(joyStick_y);
myData.Zposition = analogRead(joyStick_z);
myData.lf_select = lfs_led;
myData.rf_select = rfs_led;
Serial.println(myData.Xposition);

radio.write(&myData, sizeof(myData));

radio.startListening();

}

Receive

#include<Wire.h>
#include<Adafruit_PWMServoDriver.h>
#include<math.h>
#include<SPI.h>
#include<RF24.h>

RF24 radio (7,8);
byte addresses [] [10] = {"1Node", "2Node"};
int xJoystick;
int yJoystick;
int zJoystick;
int lf_button;
int rf_button;
struct dataStruct {

  int Xposition;
  int Yposition;
  int Zposition;
  int lf_select;
  int rf_select;
} myData;

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

int SERVOMIN = 150;
int SERVOMAX = 600;

int coxa = 56;
int femur = 40;
int tibia = 60;
int camber = 10;
float x;
float y;
float z;
float rf_x;
float rf_y;
float rf_z;
float lf_x;
float lf_y;
float lf_z;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  radio.begin();
  radio.setChannel(108);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_LOW);
  radio.openWritingPipe(addresses[1]);
  radio.openReadingPipe(1, addresses[0]);
  radio.startListening();
  
pwm.begin();
Serial.begin(115200);
pwm.setPWMFreq(60);
yield();
}

void loop() {
  // put your main code here, to run repeatedly:
if (radio.available()){
  {

    while (radio.available())
    radio.read(&myData, sizeof(myData));
    
  }
}

xJoystick=myData.Xposition;
yJoystick=myData.Yposition;
zJoystick=myData.Zposition;
lf_button=myData.lf_select;
rf_button=myData.rf_select;
Serial.println(rf_button);
float xchange = map(xJoystick, 0, 1023, -4, 4);
float ychange = map(yJoystick, 0, 1023, -4, 4);
float zchange = map(zJoystick, 0, 1023, -4, 4);

if (lf_button == HIGH && rf_button == LOW) 
{
  x = lf_x; y = lf_y; z = lf_z;
}
if (rf_button == HIGH && lf_button == LOW)
{
  x = rf_x; y = rf_y; z = rf_z;
}
x = x + xchange; y = y + ychange; z = z + zchange;
float G = atan((x-12.5)/(y-41.5));
float D = sqrt (((x-12.5)*(x-12.5)) + ((y-41.5)*(y-41.5)));
float L = sqrt (((D-coxa)*(D-coxa))+(z*z));
float A = acos(((femur*femur)+(tibia*tibia)-(L*L))/(2*femur*tibia));
float B = (acos(((femur*femur)+(L*L)-(tibia*tibia))/(2*femur*L))+acos(((L*L)+(z*z)-((D-coxa)*(D-coxa)))/(2*L*z)));

G = degrees(G);
if (G<= 0) G = 180 + G;

A = degrees(A);
B = degrees(B);

if (lf_button == HIGH)
{
  G = map(G, -40,160,-20,180);
  G = map(G,0,180,150,600);
  A = map(A,180,0,150,600);
  B = map(B, 0, 180, 150, 600);
  pwm.setPWM(0,0,G);
  pwm.setPWM(1,0,B);
  pwm.setPWM(2,0,A);
  lf_x = x; lf_y=y; lf_z = z;
}
if (rf_button == HIGH)
{
  G = map(G, 0, 200, -20, 180);
  G = map(G, 0, 180, 150, 600);
  A = map(A, 0, 180, 150, 600);
  B = map(B, 180, 0, 150, 600);
  pwm.setPWM(3, 0, G);
  pwm.setPWM(4, 0, B);
  pwm.setPWM(5, 0, A);
  rf_x = x; rf_y = y; rf_z = z;
}

}

Disclaimer: no experience with the RF24 library.

Part of the receiver code

  if (radio.available())
  {
    {
      while (radio.available())
        radio.read(&myData, sizeof(myData));
    }
  }

I would start printing on the serial monitor if you receive anything.

  if (radio.available())
  {
    Serial.println("data available (1)");
    {
      while (radio.available())
      {
        Serial, println("data available (2)");
        radio.read(&myData, sizeof(myData));
      }
    }
  }

Further you currently assume that data is received

  if (radio.available())
  {
    ...
    ...
  }

  xJoystick = myData.Xposition;
  yJoystick = myData.Yposition;
  ...
  ...
  if (lf_button == HIGH && rf_button == LOW)
  {
    ...
    ...

I think that this is basically flawed. I suspect that the extra code should actually reside inside the if (radio.available()) block so the myData struct is only processed of data is actually received.

Currently you currently process regardless if you receive anything or not.

Lastly, I would start very simple and just receive raw data and print it; I'm sure that the library comes with a simple receive example.

If you want 2-way communication then technique in the 2nd example in this Simple nRF24L01+ Tutorial will be easier because there is no need for the stopListening/startListening stuff.

If you only want 1-way transmission use the technique in the first example.

...R