NRF24 - Why does my code stop when i radio.write?

#include <SPI.h>                            //this librarys let microcontrollers communicate with each other quickly over short distances(MISO-send data to master from slave)(MOSI-send data from master to peripherals)(SCK-serial clock,clock pulses which synchronize data transmission generated by the master) 
#include <RF24.h>
#include <nRF24L01.h>                         
#include <printf.h>                         //MEGA: MOSI(pin51)MISO(pin50)SCK(pin52)(CE CSN any pin)
#include <RF24_config.h>
#define CE_PIN   8                                          
#define CSN_PIN  53
RF24 radio(CE_PIN, CSN_PIN);               
const uint64_t pipe = 0xE8E8F0F0E1LL;  

int left_sw  = 22;
int right_sw = 24;                               
int lifting_sw   = 44;                    
int throwing_sw  = 45;                           
int climbing_sw  = 43;                          
int hitting_sw   = 14;                          
int STOP_sw      = 41;                      
int levelUP_sw   = 15;
int levelDOWN_sw = 16;
const int sizeOfArray = 9;
int switchModeArray[] = {left_sw, right_sw, lifting_sw, throwing_sw, climbing_sw, hitting_sw, STOP_sw, levelUP_sw, levelDOWN_sw};
int buttonstateArray[sizeOfArray] = {};
         
struct controllerData                               
{                        
  bool levelUP_sw;
  bool levelDOWN_sw;
  bool left_sw;    
  bool right_sw;
  bool lifting_sw;
  bool throwing_sw;
  bool climbing_sw;
  bool hitting_sw;   
  bool STOP_sw;
}; controllerData data;                               

void setup(){
  Serial.begin(115200);
  Serial.println("Starting");
  for (int i=0; i<sizeOfArray; i++){           
    pinMode(switchModeArray[i], INPUT);              //Connect all the switch pins to INPUT
  }                
  for (int i=0; i<sizeOfArray; i++){           
    pinMode(switchModeArray[i], INPUT_PULLUP);       //activate the pullup resistors for each switch
  }       
  radio.begin();                                      //Begin & Link The Radios
  radio.setPALevel(RF24_PA_LOW);                      //set the power consumption
  radio.setDataRate(RF24_250KBPS);                    //set the speed
  radio.setRetries(15, 15);                           //no clue what this does
  radio.openWritingPipe(pipe);                        //opening the pipe for writing only           
         
}


void loop(){
  Serial.println("loop begin");
 for (int i = 0; i < sizeOfArray; i++)                         //read all the switchbuttons
  {           
    buttonstateArray[i] = digitalRead(switchModeArray[i]);        
  } 
  data.left_sw = buttonstateArray[0];                             //put them in the packet so we can send them out
  data.right_sw = buttonstateArray[1];
  data.lifting_sw =  buttonstateArray[2];
  data.throwing_sw = buttonstateArray[3];
  data.climbing_sw = buttonstateArray[4];
  data.hitting_sw =  buttonstateArray[5];    
  data.STOP_sw = buttonstateArray[6];
  data.levelUP_sw = buttonstateArray[7];    
  data.levelDOWN_sw = buttonstateArray[8];
  Serial.print("Buttons Loaded to package");
  radio.write( &data, sizeof(unsigned long) );
  Serial.println("Loop End");
}

the Serial monitor writes
"Starting
loop begin
Buttons Loaded to package"

and thats it, the lopps doest run any more. why is that? It seems that the loop ends when i try to transmit the data to the other NRF24

This line

radio.write( &data, sizeof(unsigned long) );

should be

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

The struct called data is 9 bytes long. An unsigned long is just 4 bytes long

But I don't think that would account for your problem.

You don't say what RF24 library you are using - there are slight differences. I recommend the TMRh20 version

Have a look at my Simple nRF24L01+ Tutorial and see if those examples work for you.

radio.setRetries(15, 15);                           //no clue what this does

You need to study the Nordic datasheet for the nRF24L01+. Those number cause it to wait 4000µsecs between retries and to make 15 retries before giving up - a total of about 60 millisecs.

I believe you will understand the concept of the nRF24 pipes better if you change the name of the variable called pipe in your program to address. That number plays the same role as a house address.

...R

Robin2:
This line

radio.write( &data, sizeof(unsigned long) );

should be

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

The struct called data is 9 bytes long. An unsigned long is just 4 bytes long

But I don't think that would account for your problem.

You don't say what RF24 library you are using - there are slight differences. I recommend the TMRh20 version

Have a look at my Simple nRF24L01+ Tutorial and see if those examples work for you.

radio.setRetries(15, 15);                           //no clue what this does

You need to study the Nordic datasheet for the nRF24L01+. Those number cause it to wait 4000µsecs between retries and to make 15 retries before giving up - a total of about 60 millisecs.

I believe you will understand the concept of the nRF24 pipes better if you change the name of the variable called pipe in your program to address. That number plays the same role as a house address.

...R

it seems like one of the transceiver was the problem, i am using that same library, i have a 5Volt regulator and have 2 arduino Megas. i ran the loop twice on each arduino using transceiver A and transceiver B, transceiver B seemed to stop the loop twice for both arduinos.

gana reorder new ones and see what happens

Have you a 10µF capacitor across Vcc and GND for the nRF24s? It is recommended although I have not needed it with my Uno or Mega. I do need it on my breadboard Atmega328 versions.

...R

Robin2:
Have you a 10µF capacitor across Vcc and GND for the nRF24s? It is recommended although I have not needed it with my Uno or Mega. I do need it on my breadboard Atmega328 versions.

...R

i got it to work but it only works when i have i at MIN. i have a 47 µF capacitor on one of them. it 47 too much?

maydae:
when i have i at MIN.

I don't understand - perhaps there is a typo?

...R

Robin2:
I don't understand - perhaps there is a typo?

...R

hi again so i bought the nrf24 wiht the antenna, i have 6 servos hooked up to the MEGA, i have the MEGA hooked up to a powerbank. When i put the code as "radio.setPALevel(RF24_PA_MIN);" it works fine.

When i put the code as "radio.setPALevel(RF24_PA_MAX);" it constantly loses connection. do i need more power to the arduino? Will connecting a 9 volt AA batteries give the reciever enough power?

If the high-power TX is close to the RX devices its signal can overwhelm them.

You have not posted a diagram showing how you have the high-power device connected. AFAIK it cannot get enough power from the Arduino 3.3v pin - indeed it is almost insufficient for the pcb-antenna versions of the nRF24.

...R

Sounds like you might be having power issues. I've had good results with these modules. They run the high power version of the radios at max power no problem.
http://www.yourduino.com/sunshop/index.php?l=product_detail&p=467

SamIAm93:
Sounds like you might be having power issues. I've had good results with these modules. They run the high power version of the radios at max power no problem.
http://www.yourduino.com/sunshop/index.php?l=product_detail&p=467

ya i believe i was, i got the base moduls and some antenna Nrf24s and they r working fine....for now anyways lol.

thanks everyone