nRF24 losing packets when connected to USB

Hello,

I am noticing a strange behaviour of my nRF connection between two Nano boards. One sends between one and six packets consisting of 2 floats every second. Every time a Hall-Sensor is pulled.

The speed of the motor is constant!

If I connect just the sender to the PC i get almost a constant reading of 385 rpm.

If above stays connected to the PC and the receiver is powered by an external source I get a reading 319-339 rpm.

If the sender is powered by an external source and the reciever is powered by an external source I get a reading of 319-326 rpm.

If at last I connect the receiver to the PC and try to get the reading on the serial.print some packets will be lost (about one third of all packets sent) and the reading fluctuates massively between 319-385 rpm.

So I have two main questions:

  1. Is it possible for "serial.print" to take up so much time that the nano loses a packet?

  2. Why is the rpm dropping when the receiver is connected?

I tried the reading of the sender without transmitting data: this will result in the same rpm as if the sender tries to send the data and the receiver is connected to an external source. Which is confusing to me. If the nano tries to transmit data and no receiver listens it gets faster than not trying to transmit data or transmitting data and a receiver listens.

Do you realise we can't see your code?

Receiver

#include <SPI.h>
#include <nRF24L01.h>               //RADIO Bibliothek
#include <RF24.h>                   //RADIO Bibliothek

#include <Wire.h>                   //Wire Bibliothek einbinden
#include <LiquidCrystal_I2C.h>      //Vorher hinzugefügte LiquidCrystal_I2C Bibliothek einbinden
LiquidCrystal_I2C lcd(0x27, 16, 2); //Hier wird festgelegt um was für einen Display es sich handelt. In diesem Fall eines mit 16 Zeichen in 2 Zeilen und der HEX-Adresse 0x27. Für ein vierzeiliges I2C-LCD verwendet man den Code "LiquidCrystal_I2C lcd(0x27, 20, 4)" 

float cadence;                      //cadence deklarieren
float durchschnitt;                 //durchschnitt deklarieren
float kraft;                        //kraft deklarieren
float drehmoment;                   //drehmoment deklarieren
float leistung;                     //leistung deklarieren
//int zaehler;
int i=0;


                                    //Radio
RF24 radio(7, 8);                   //CE, CSN auf PIN 7 und 8
const byte address[6] = "00001";    

void setup() {
  Serial.begin(115200);
                                    //Radio-Setup
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();

                                    //LCD-Setup
  lcd.init();                       //Im Setup wird der LCD gestartet 
  lcd.backlight();                  //Hintergrundbeleuchtung einschalten (lcd.noBacklight(); schaltet die Beleuchtung aus). 
}


                                    //Dann beginnt der loop
void loop() {
  if (radio.available()) {
                                    //die message ist in zwei Teile geteilt, da wenn beide einzeln gesendet werden der Empfänger die Variablen nicht zuordnen kann.
    
    
    float message[2] = {0,0};
    radio.read(&message, sizeof(message));
                                    //hier wird die Aufteilung wieder zugeordnet
    durchschnitt = float (message[0]);     
    cadence = float (message[1]);
    //zaehler = int (message[2]);
    
    kraft = durchschnitt*9.81;      //Berechnung der Kraft aus der gemessenen Masse, das kann auch weggelassen werden, wenn die Kalibrierung auf der Senderseite durch bekannte Kräfte erfolgt [mN]
    drehmoment = kraft*0.175;       //Drehmomentberechnung durch Annahme des Hebels-/Kurbellänge als 175mm [mNm]
    Serial.println(cadence);

    i=i+1;
    lcd.setCursor(10,1);
    lcd.print(i);

lcd.setCursor(0,0); 
    lcd.print("C: ");
    if(cadence <9.5) lcd.print(" ");
    if(cadence <99.5) lcd.print(" ");
    if(cadence <999.5) lcd.print(" ");
    lcd.print(cadence, 0);
//Serial.println(cadence);
}


}

Transmitter:

#include <SPI.h>                   
#include <nRF24L01.h>               //RADIO Bibliothek
#include <RF24.h>                   //RADIO Bibliothek

#include <HX711_ADC.h>              //Load-cell Bibliothek einbinden https://github.com/olkal/HX711_ADC
#include <Wire.h>                   //Wire Bibliothek einbinden
HX711_ADC LoadCell(4, 5);           //Loadcell Pinbelegung
#define Eingang 2                   //Drehzahl-Eingang: Pin2 / Interrupt 0
unsigned long neueZeit;             //Drehzahl
unsigned long alteZeit;             //Drehzahl
float dauer;                        //Drehzahl
float cadence = 0;                  //Drehzahl

int zaehler = 0;                        //Mittelwertbildung
float sum = 0;                          //Mittelwertbildung
float durchschnitt = 0;                 //Mittelwertbildung


RF24 radio(7, 8); // CE, CSN        //CE, CSN auf PIN 7 und 8
const byte address[6] = "00001";

void setup() {
  Serial.begin(115200);
                                    //Radio-Setup
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();

  pinMode(Eingang, INPUT);          //Drehzahl Eingangspin auf Eingang stellen
  
  LoadCell.begin();                 //start connection to HX711
  LoadCell.start(2000);             //load cells gets 2000ms of time to stabilize
  LoadCell.setCalFactor(889.0);     //calibration factor for load cell => strongly dependent on your individual setup
  
                                    //Interrupt 0 auf Routine readmicros setzen
  attachInterrupt(0, detectMagnet, FALLING);
}


                                    //Dann beginnt der loop
void loop() {
  LoadCell.update();                //retrieves data from the load cell
  float masse = LoadCell.getData(); //get output value

  noInterrupts();
  sum = sum + masse;                //alle Werte zusammenzählen
  interrupts();
  zaehler = zaehler + 1;            //ein Wert mehr addiert
  
  
                                    //Die zu übermittelnden Variablen müssen in eine Nachricht geschrieben werden, 
                                    //da der Empfänger die Zuordnung sonst nicht hinbekommt      
  
  
  //Serial.print("i= ");
  //Serial.print(i);
  //Serial.print(" cadence= ");
  //Serial.print(cadence);
  //Serial.println();
  
}

                                    //hier die funktion die immer aktiviert wird wenn der Magnet den Hallsensor passiert
void detectMagnet() {
  durchschnitt = sum / zaehler;
  
  float message[2] = {float(durchschnitt),float(cadence)};
                                    //dann übermitteln?!?! HIER ODER IM interrupt magnet???!
  radio.write(&message, sizeof(message));

  
  
  cadence = 0;
  neueZeit = micros();
  dauer = neueZeit - alteZeit;
  cadence = 60 / (dauer / 1000000);
  alteZeit = micros();


  //FÜR DIE MITTELWERTBILDUNG

  Serial.print("Durchschnitt= ");
  Serial.print(durchschnitt);
  Serial.print(" Cadence= ");
  Serial.print(cadence);
  Serial.print(" Summe= ");
  Serial.print(sum);
  
  Serial.print(" Zaehler= ");
  Serial.print(zaehler);
  
  Serial.println();
  
  sum = 0;
  zaehler = 0;
  //masse = 0;
  
}

You have far too much code in your ISR function detectMagnet() - and you have code (printing and SPI) within it that relies on interrupts being enabled even though interrupts are disabled within an ISR.

Your ISR should just record the time of the pulse and set a variable to signify that the interrupt has happened. All the rest of the code should be outside the ISR - in functions called from loop(). Have a look at the code in this link.

…R

But still: if i delete most of the code and just focus on the necessary part of the cadence calculation the nano still gives a faster reading while trying to send a message over the nrf than not trying to send that message.

nano24:
But still: if i delete most of the code and just focus on the necessary part of the cadence calculation the nano still gives a faster reading while trying to send a message over the nrf than not trying to send that message.

How can I comment when I can't see your revised program.

And (just in case) please do NOT overwrite the code in Reply #2, put the updated code in your next Reply so that we can compare the versions if necessary.

...R