NRF24L01+ Stops Transmitting After Exactly 33 Seconds

Hi everyone,

This is a real head scratcher for me. I have an Arduino Uno (powered off USB) transmitting to an RC vehicle using NRF24L01+'s with the RF24 Fork library. Everything works, fantastically. But then I hit the 33rd second of transmitting and it suddenly ceases to transmitting. Testing seems to indicate it is definitely on the transmitter side of things.

It is reading a potentiometer. The “failureDetected” method never gets called, it just stops transmitting out of the blue, no errors or anything. It definitely isn’t the potentiometer, even when I set it to transmit a set number constantly, it still fails after 33 seconds.

I did try a 47μF capacitor and a 100μF capacitor on the VCC and GND pins of the NRF24L01+ and the problem remains exactly the same.

Here is my code. Please be kind, I have never had any technical training whatsoever and learned everything I know from google, I am a biology student not a computer science student :slight_smile:

/***********************************************
                CONTROLLER SOFTWARE
***********************************************/

//radio includes
#include <SPI.h>
#include "RF24.h"
#include <printf.h>


const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

RF24 radio(7, 8);

int currentTime = 0;
int nextTime = 0;

const unsigned int measurementInterval = 10; //sampling rate of the potentiometer
float currentThrottle = 0;
float previousThrottles[10];
float averageThrottle = 0;

void setup ()
{
  Serial.begin(57600);
  printf_begin();
  radio.begin();
  radio.setRetries(15, 15);
  radio.setPayloadSize(sizeof(unsigned long));
  radio.setPALevel(RF24_PA_MAX);
  radio.setChannel(16);
  
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1,pipes[1]);
  
  radio.startListening();
  radio.printDetails();
  
  Serial.println("Finished setup...");
  
  nextTime = currentTime + measurementInterval;
}

void loop ()
{
  currentTime = millis();
  
  if (millis() >= nextTime) {
    nextTime = millis() + measurementInterval;
    
    float throttleReading = analogRead(A0);
    
    
    float adjustment = 0.1854;
    
    currentThrottle = adjustment * throttleReading;
    
    
    for (int i = 0; i < 9; i++) {
      previousThrottles[i] = previousThrottles[i + 1];
    }
    
    previousThrottles[9] = currentThrottle;
    
    averageThrottle = 0;
    
    for (int i = 0; i < 10; i++) {
      averageThrottle += previousThrottles[i];
    }
    
    averageThrottle = averageThrottle / 9;
    
    
    if (averageThrottle > 20) {
      radio.stopListening();
      unsigned long yyt = long(averageThrottle);
      bool ok = radio.write(&yyt, sizeof(unsigned long));
      if (!ok) {
        Serial.print("Failed to send throttle: \t");
        Serial.println(yyt);
      } else {
        Serial.println("Success");
        Serial.println(averageThrottle);
    }
      radio.startListening();
    }
    
    if(radio.failureDetected){ 
      radio.begin();     
      Serial.println("FAILURE DETECTED");
      // Attempt to re-configure the radio with defaults
      radio.failureDetected = 0;           // Reset the detection value
      radio.setRetries(15, 15);
      radio.setPayloadSize(sizeof(unsigned long));
      radio.setPALevel(RF24_PA_MIN);
      radio.setChannel(16);
      radio.openWritingPipe(pipes[0]);
      radio.openReadingPipe(1,pipes[1]);
    }
  }
}

My first suggestion would be to take a closer look at the datatypes used in Arduino, specifically int vs unsigned int or unsigned long.

Then try out the following code, and note the changes at the very end.

/***********************************************
                CONTROLLER SOFTWARE
***********************************************/

//radio includes
#include <SPI.h>
#include "RF24.h"
#include <printf.h>


const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

RF24 radio(7, 8);

int currentTime = 0;
int nextTime = 0;

const unsigned int measurementInterval = 10; //sampling rate of the potentiometer
float currentThrottle = 0;
float previousThrottles[10];
float averageThrottle = 0;

void setup ()
{
  Serial.begin(57600);
  printf_begin();
  radio.begin();
  radio.setRetries(15, 15);
  radio.setPayloadSize(sizeof(unsigned long));
  radio.setPALevel(RF24_PA_MAX);
  radio.setChannel(16);
  
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1,pipes[1]);
  
  radio.startListening();
  radio.printDetails();
  
  Serial.println("Finished setup...");
  
  nextTime = currentTime + measurementInterval;
}

void loop ()
{
  currentTime = millis();
  
  if (millis() >= nextTime) {
    nextTime = millis() + measurementInterval;
    
    float throttleReading = analogRead(A0);
    
    
    float adjustment = 0.1854;
    
    currentThrottle = adjustment * throttleReading;
    
    
    for (int i = 0; i < 9; i++) {
      previousThrottles[i] = previousThrottles[i + 1];
    }
    
    previousThrottles[9] = currentThrottle;
    
    averageThrottle = 0;
    
    for (int i = 0; i < 10; i++) {
      averageThrottle += previousThrottles[i];
    }
    
    averageThrottle = averageThrottle / 9;
    
    
    if (averageThrottle > 20) {
      radio.stopListening();
      unsigned long yyt = long(averageThrottle);
      bool ok = radio.write(&yyt, sizeof(unsigned long));
      if (!ok) {
        Serial.print("Failed to send throttle: \t");
        Serial.println(yyt);
      } else {
        Serial.println("Success");
        Serial.println(averageThrottle);
    }
      radio.startListening();
    }
    
    if(radio.failureDetected){ 
      radio.begin();     
      Serial.println("FAILURE DETECTED");
      // Attempt to re-configure the radio with defaults
      radio.failureDetected = 0;           // Reset the detection value
      radio.setRetries(15, 15);
      radio.setPayloadSize(sizeof(unsigned long));
      radio.setPALevel(RF24_PA_MIN);
      radio.setChannel(16);
      radio.openWritingPipe(pipes[0]);
      radio.openReadingPipe(1,pipes[1]);
    }
  }else{
    Serial.print("m: ");
    Serial.println( millis());
    Serial.print("n: ");
    Serial.println(nextTime);
    delay(500);
  }
}

I hope this will give you enough information to solve the problem on your own, and an idea of how to begin troubleshooting such a problem, but if you are still confused, I can post the explanation.

int currentTime = 0;
int nextTime = 0;

You will overflow the currentTime variable where (currentTime = millis();) very quickly and like TMRh20 suggests you you should take a look to data types. These two variables should be of type long.

That was so obvious. WOW. How did I not notice that? I knew it was some obvious mistake in my code that I was not catching but I wish it had been something a little less easy. lol

Thank you both so much :D