Setup constantly loops?

So i have a problems where the setup keeps running every time, i have uploaded a picture of both. before this only the setup kecpt looping meaning it didnt run the loop b4. atm the serial monitor of the reciever is stuck...what is the problem guys?

the MEGA i have: link

UNO i have: link

TRANSMITTER CODE: no strange problem

#include <SPI.h>                        //listing libraries needed to communicate with other arduino
#include <RF24.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24_config.h>

#define Lstick_x    A0          //left joystik, x axis              
#define Rstick_y    A2          //right joystick y axis
                     
#define CE_PIN      9
#define CSN_PIN     53
RF24 radio(CE_PIN, CSN_PIN); 
const uint64_t pipe = 0xE8E8F0F0E1LL;    //communication pipe madeup address
   
struct positionData             //make a sturcture package to b sent to the reciever
{                                  
  byte Laxis_x;
  byte Raxis_y;          
};positionData data;                                   

void setup()
{
  Serial.begin(9600);
  Serial.println("starting setup");
  radio.begin();                            //link the 2 radios, set speed and set if its gana b set for reading or writing for this arduino
  radio.setPALevel(RF24_PA_MAX);               
  radio.setDataRate(RF24_250KBPS);              
  radio.openWritingPipe(pipe);              
}
void loop()
{
  int Laxis_x = analogRead(Lstick_x);  
  int Raxis_y = analogRead(Rstick_y);
  data.Laxis_x = map(Laxis_x,0,1023,0,180);             //remap to servo positions and get it ready to b sent out
  data.Raxis_y = map(Raxis_y,0,1023,0,180);                            
  Serial.print("LXaxis: ");              Serial.print(data.Laxis_x);
  Serial.print("    Ryaxis: ");          Serial.println(data.Raxis_y);
  radio.write( &data, sizeof(unsigned long) );         //send to reciever
}

RECIEVER CODE: (THIS ONE HAVE STRANGE PROBLEM

//reciever
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24_config.h>

#define CE_PIN   7
#define CSN_PIN  8
RF24 radio(CE_PIN, CSN_PIN);  
const uint64_t pipe = 0xE8E8F0F0E1LL;       

struct positionData{                        
  byte Laxis_x;
  byte Raxis_y;        
};positionData data;

void setup(){           
  Serial.begin(115200);
  Serial.println("Starting Setup");                 //it keeps running this
  radio.begin();                                    //Link The Radio
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.openReadingPipe(1,pipe);            
  radio.startListening();                
}


void loop() {
  Serial.println("Starting loop");
  if ( radio.available() )
  {                                    
    radio.read( &data, sizeof(unsigned long) );              
    float right_y = data.Raxis_y;
    float left_x = data.Laxis_x;
    Serial.print("    LeftServo: ");
    Serial.print(left_x);        //will be in a servo value, need to change it since, out motor is 97 to 102 is neutral
    Serial.print("    RightServo: ");
    Serial.print(right_y);        //will be in a servo value, need to change it since, out motor is 97 to 102 is neutral           
  }
  else{Serial.print("no data"); }

See if this stops the problem:
added a spam timer Serial.print() will crash the UNO if it is called too quickly

//reciever
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24_config.h>

#define CE_PIN   7
#define CSN_PIN  8
RF24 radio(CE_PIN, CSN_PIN);  
const uint64_t pipe = 0xE8E8F0F0E1LL;      

struct positionData {
                         
   byte Laxis_x;
   byte Raxis_y;        
}; positionData data;

void setup() {
           
   Serial.begin(115200);
   Serial.println("Starting Setup");                 //it keeps running this
   radio.begin();                                    //Link The Radio
   radio.setPALevel(RF24_PA_MAX);
   radio.setDataRate(RF24_250KBPS);
   radio.openReadingPipe(1, pipe);            
   radio.startListening();                
}


void loop() {
  static unsigned long SpamTimer;
  if ( (unsigned long)(millis() - SpamTimer) >= (100)) {
    SpamTimer = millis();

     Serial.println("Starting loop");
     if ( radio.available() )
        {                                    
              radio.read( &data, sizeof(unsigned long) );              
              float right_y = data.Raxis_y;
              float left_x = data.Laxis_x;
              Serial.print("    LeftServo: ");
              Serial.print(left_x);        //will be in a servo value, need to change it since, out motor is 97 to 102 is neutral
              Serial.print("    RightServo: ");
              Serial.print(right_y);        //will be in a servo value, need to change it since, out motor is 97 to 102 is neutral          
            
         }
       else {
        Serial.print("no data");
      }
  }

Thanks, for some reason it now works with and without ur code. so strange

also can u explain why my receiver isnt getting the code that i transmitted? it doesnt make sense why it wont work.

  1. From rx code. This is wrong
         radio.read( &data, sizeof(unsigned long) );
  1. From tx code. This is wrong
         radio.write( data, sizeof(unsigned long) );
  1. from rx code
   if ( radio.available() ) then read multiple bytes

wrong - you can not just assume that the whole message has been received

  1. How are you syncing the messages?

Mark

holmes4:

  1. From rx code. This is wrong
         radio.read( &data, sizeof(unsigned long) );
  1. From tx code. This is wrong
         radio.write( data, sizeof(unsigned long) );

1 and 2) how are these wrong? im only writing and read the data package

holmes4:
3. from rx code

if ( radio.available() ) then read multiple bytes

wrong - you can not just assume that the whole message has been received

3.) i dont understand what u mean, i did add the "if ( radio.available() ){read the package}"

masterfo:
4. How are you syncing the messages?

4.) what do u mean?

how do i fix the code?

holmes4:
wrong - you can not just assume that the whole message has been received

Sorry, but you can and should. The nRF24 only returns available when the whole message has been received. it does not deal in partial messages.

...R

As a general rule, the only reason that setup() will repeat is if something is causing the Arduino to restart.

zhomeslice:
See if this stops the problem:
added a spam timer Serial.print() will crash the UNO if it is called too quickly

It would probably make more sense if the OP adds a suitable interval between transmissions rather than fiddling with the Rx code.

@masterfo I suggest, for testing, that you add delay(200); as the last thing in loop() in the TX program

...R
Simple nRF24L01+ Tutorial

Robin2:
Sorry, but you can and should. The nRF24 only returns available when the whole message has been received. it does not deal in partial messages.

...R

are u saying that i should constantly send data "radio.write( &data, sizeof(unsigned long) );" whenever i add something into the data packet like "data.Raxis_y= analogread(joystickPin);"

how do i ensure that the whole message will be received? like when do i sent out the message

masterfo:

Sorry, but you can and should. The nRF24 only returns available when the whole message has been received. it does not deal in partial messages.

are u saying that i should constantly send data "radio.write( &data, sizeof(unsigned long) );" whenever i add something into the data packet like "data.Raxis_y= analogread(joystickPin);"

how do i ensure that the whole message will be received? like when do i sent out the message

I don't think my comment and your question have anything in common.

All I was saying in my comment is that you can check whether a complete message has been received using

if (radio.available() ) {

But your question is about transmitting. And I don't think I understand everything that was in your mind when you wrote it.

In general you need to decide how frequently you need to update the data in order to have a smooth response to the user input. I suspect it would be sufficient to update the data (i.e. send a new message) about 5 times per second, or maybe 10 times per second.

However for testing the radio link it would probably be a good idea to reduce that to 1 or 2 per second so there is time to print the results and see what is happening. When you know the radio link is working you can comment-out the print statements and increase the frequency of messages.

...R

Robin2:
are u saying that i should constantly send data "radio.write( &data, sizeof(unsigned long) );" whenever i add something into the data packet like "data.Raxis_y= analogread(joystickPin);"

how do i ensure that the whole message will be received? like when do i sent out the message

I don't think my comment and your question have anything in common.

All I was saying in my comment is that you can check whether a complete message has been received using

if (radio.available() ) {

But your question is about transmitting. And I don't think I understand everything that was in your mind when you wrote it.

In general you need to decide how frequently you need to update the data in order to have a smooth response to the user input. I suspect it would be sufficient to update the data (i.e. send a new message) about 5 times per second, or maybe 10 times per second.

However for testing the radio link it would probably be a good idea to reduce that to 1 or 2 per second so there is time to print the results and see what is happening. When you know the radio link is working you can comment-out the print statements and increase the frequency of messages.

...R

srry about hat im new to wireless communication and its driving me nuts. when u said to send a new message 5 times per second, how exactly do i do this? doesnt the loop run the code every milisecond or something? doesnt it sent data in every loop? are u saying to give a delay?

masterfo:
srry about hat im new to wireless communication and its driving me nuts. when u said to send a new message 5 times per second, how exactly do i do this? doesnt the loop run the code every milisecond or something? doesnt it sent data in every loop? are u saying to give a delay?

Don't use delay() for anything other than simple test programs because it blocks the Arduino from doing other stuff.

Look at how the timing is managed using millis() in the examples in the link gave you in Reply #6

By the way the time between iterations of loop() depends on what code has to be performed. It could be much faster than once per millisecond, or much slower. There is no point in sending messages more frequently than they are needed.

...R