Problem with NRF24L01 chip communication freezes after a while!!!

You should study the datasheet of the NRF to get an idea of the NRF's inner workings.

What is the output of the programs?

ddmdavid01:
Note: in the part that shoots it is supposed to receive nothing and the 3 sec. delay is important and it did not give to me any problem.

Messages can be ignored if really needed, there is no reason to switch of the receiver.
Don't fiddle with the hardware if there is no reason to do so.

If you insist in using delay, I'm out.

Basically this is the output

0,0,0,0,505,4

0,0,0,0,505,4

0,0,0,0,505,4

0,0,0,0,505,4

0,0,1792,2248,24,0

0,0,0,0,505,3

0,0,0,0,505,3

0,0,0,0,505,3

0,0,0,0,505,3

0,0,0,0,505,4

0,0,0,0,505,4

0,0,0,0,505,3

0,0,0,0,505,4

0,0,0,0,505,4

0,0,0,0,506,3

0,0,0,0,506,4

0,0,0,0,505,3

0,0,0,0,505,4

0,0,0,0,506,3

0,0,0,0,506,3

0,0,0,0,506,3

0,0,0,0,505,3

0,0,0,0,505,3

0,0,0,0,505,4

0,0,0,0,506,3

0,0,0,0,505,4

0,0,0,0,505,4

0,0,2,0,0,0

0,0,0,0,506,3

0,0,0,0,505,4

0,0,0,0,505,4

0,0,0,8704,0,0

And then it freezes in the last one.

It is possible to notice that there are previous irregular values ,I think the acknowlege message would fix that

You make it very hard to help you.

Why did you post that stuff outside code tags and did not tell which belongs where?

But anyway, both contain lines that are not formed by digital/analog read.

0,0,1792,2248,24,0

0,0,0,8704,0,0

I think we are having problem communicating with each other. I think it has to do with the fact I lack in knowledge related to programming and specially to the NRF24L01 chip. When something happen I fail on describing the full problem. In case you didn't understand that is the message the car receives. I saw the signal produced by the command and the one received by the car (the one I have posted). There are no lines with errors in the one created with the command and there are in the ones received by the car.

Note: If you think it is easier to describe the problem by talking we can video chat.

ddmdavid01:
I think we are having problem communicating with each other. I think it has to do with the fact I lack in knowledge related to programming and specially to the NRF24L01 chip.

Yes, you do not answer questions. (like 'What is the output of the programs?') :wink:
Programming can only be learned by doing.
You should study the datasheet of the NRF, probably more than once.

ddmdavid01:
When something happen I fail on describing the full problem. In case you didn't understand that is the message the car receives.

How could I, having asked for the output of both sides.

ddmdavid01:
I saw the signal produced by the command and the one received by the car (the one I have posted). There are no lines with errors in the one created with the command and there are in the ones received by the car.

If you had posted both outputs, I would not have to believe you... and I'm a very bad believer.

You should create a small receive only sketch that does not mess with the other hardware,
there is something going on, but it is probably not the NRF, if it signals a packet it was received without errors.
Errors on the SPI bus would show up in the receive only sketch too.

After you have the communication running, you should start adding the hardware control.

Did I mention that the hardware control should be non-blocking?
You should raise your programming skills by replacing all delay() calls by non-blocking timing.

Ok so here is the the message received by the car until it freezes:
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,4
0,0,16,0,0,0
0,0,0,0,325,3
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,3
0,0,0,0,325,3
0,0,0,0,326,3
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,0,0

And here is the message send by the command meanwhile:

0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,3
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,3
0,0,0,0,325,3
0,0,0,0,325,3
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,3
0,0,0,0,325,3
0,0,0,0,326,3
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,3
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4
0,0,0,0,325,4

By the way check the message I sent you

I decided to comment the part of the code which envolve the hardware and only leave the communication and here is part of the result:

From the command:

0,0,0,0,505,495
0,0,0,0,505,495
0,0,0,0,506,495
0,0,0,374,505,495
0,0,0,642,506,495
0,0,0,1011,506,495
0,0,0,1023,506,495
0,0,0,1023,506,495
0,0,0,1023,505,495
0,0,0,1023,506,495
0,0,0,1023,506,495
0,0,0,1023,506,495
0,0,0,987,506,495
0,0,0,842,506,495
0,0,0,736,505,495
0,0,0,627,505,495
0,0,0,487,506,495
0,0,0,421,505,495
0,0,0,421,505,495
0,0,0,421,506,495
0,0,0,421,506,495
0,0,0,422,506,495
0,0,0,421,505,495
0,0,0,421,505,495
0,0,0,421,506,495
0,0,0,421,506,495
0,0,0,421,505,495
0,0,0,421,506,495
0,0,0,422,505,495
0,0,0,421,506,495
0,0,0,421,506,495
0,0,0,421,506,495
0,0,0,419,505,495
0,0,0,321,505,495
0,0,0,209,505,495
0,0,0,120,505,495
0,0,0,22,505,495
0,0,0,0,505,495
0,0,0,0,505,495
0,0,0,0,505,495
0,0,0,0,505,495
0,0,0,0,506,495
0,0,0,0,505,495
0,0,0,0,505,495
0,0,0,0,506,495
0,0,0,0,505,495

From the car:

0,0,0,0,505,495
0,0,0,0,505,495
0,0,0,0,506,495
0,0,0,374,505,495
0,0,0,642,506,495
0,0,0,1011,506,495
0,0,0,1023,506,495
0,0,0,1023,506,495
0,0,0,1023,505,495
0,0,0,1023,506,495
0,0,0,1023,506,495
0,0,0,1023,506,495
0,0,0,987,506,495
0,0,0,842,506,495
0,0,0,736,505,495
0,0,0,627,505,495
0,0,0,487,506,495
0,0,0,421,505,495
0,0,0,421,505,495
0,0,0,421,506,495
0,0,0,421,506,495
0,0,0,422,506,495
0,0,0,421,505,495
0,0,0,421,505,495
0,0,0,421,506,495
0,0,0,421,506,495
0,0,0,421,505,495
0,0,0,421,506,495
0,0,0,422,505,495
0,0,0,421,506,495
0,0,0,421,506,495
0,0,0,421,506,495
0,0,0,419,505,495
0,0,0,321,505,495
0,0,0,209,505,495
0,0,0,120,505,495
0,0,0,22,505,495
0,0,0,0,505,495
0,0,0,0,505,495
0,0,0,0,505,495
0,0,0,0,505,495
0,0,0,0,506,495
0,0,0,0,505,495
0,0,0,0,505,495
0,0,0,0,506,495
0,0,0,0,505,495

As it is possible to see the signal received is completely equal to the one sent and the previous signals I haven't posted are the exact same case.

I think it has to do with the processing of the signal. Maybe by sending the acknowledge message in order to continue?

Please put the data in code tags like normal code.
You can still change this in the older posts.

So it seems the communication is working now in one direction.

What are your plans for the other direction?

So basically what I was planning doing was after I get full control of the robot (which I don't because of the errors) the robot would read an HC SR 04 and a luminosity sensor that I made with an LDR and a resistor and send it back to the command so it could be seen throw the computer. Basically it would read the signal from the command, preform the tasks as I say, read the sensor and send the info so I can see it.

I would regulary measure into variables of a structure and use that as ackpayload.
Ackpayloads are ordinary packets, only the means of delivery is different.

Please post the compiling minimal receiver sketch here,
removing commented out sections, unused variables, ... before.

You can add the ackpayload structure, it's preload in setup and it's reload after a packet reception.

Hint: void writeAckPayload (uint8_t pipe, const void *buf, uint8_t len);

This is the code that only receives the data and prints it. The code for the command is the same as previously used,

//this is for the arduino mega

#include <Servo.h>
#include <nRF24L01.h>
#include <SPI.h>
#include <RF24.h>
#include <RF24_config.h>


//INPUT values
typedef struct data{
  int trigger;
  int reverse;
  int button3;
  int lights;
  int xAxis;
  int yAxis;
}dataSend;

dataSend dataSendFinal;



//defining everything that is required to the RF communication
const uint64_t pipes[2] = {0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL};

RF24 radio(48, 49);


void setup() {
  /***************************/
  Serial.begin(115200);
  //setup of the radio communication
  radio.begin();
  radio.powerUp();
  //definition of the pipes that are going to be used to stablish the communication between both devices NOTE: the one with the 1 has to change in the one with the receiver
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1, pipes[1]);
  //definition of the conection
  //conection speed
  radio.setDataRate(RF24_250KBPS) ;
  //energy setup
  radio.setPALevel( RF24_PA_MAX ) ;
  //set channel to 105
  radio.setChannel(105);
  //other setups
  radio.enableDynamicPayloads();
  radio.enableAckPayload();
  //number of atemps that will use in order to send a message
  radio.setRetries(5, 15);

  radio.setAutoAck( true );

  radio.startListening();

}

void loop() {

  //trying to receive a signal and if so receive and process the info
  if (radio.available()) {
   radio.read(&dataSendFinal, sizeof(struct data));
  // radio.powerUp();
   // radio.stopListening();
    //radio.powerDown();
  
    Serial.print(dataSendFinal.trigger);
    Serial.print(",");
    Serial.print(dataSendFinal.reverse);
    Serial.print(",");
    Serial.print(dataSendFinal.button3);
    Serial.print(",");
    Serial.print(dataSendFinal.lights);
    Serial.print(",");
    Serial.print(dataSendFinal.xAxis);
    Serial.print(",");
    Serial.print(dataSendFinal.yAxis);
    Serial.print("\n");
  }
}

I would regulary measure into variables of a structure and use that as ackpayload.

I didn't get it.

Something like the following, where I count up distance all 300 ms and (p)reload ackpayloads (untested).

//this is for the arduino mega
#include <Servo.h>
#include <nRF24L01.h>
#include <SPI.h>
#include <RF24.h>
#include <RF24_config.h>

//INPUT values
typedef struct InData {
  int trigger;
  int reverse;
  int button3;
  int lights;
  int xAxis;
  int yAxis;
  void print() {
    Serial.print(trigger);
    Serial.write(',');
    Serial.print(reverse);
    Serial.write(',');
    Serial.print(button3);
    Serial.write(',');
    Serial.print(lights);
    Serial.write(',');
    Serial.print(xAxis);
    Serial.write(',');
    Serial.println(yAxis);
  }
} DataIn;

//OUTPUT values
typedef struct OutData {
  int distance;
  int luminosity;
  void print() {
    Serial.print(distance);
    Serial.write(',');
    Serial.println(luminosity);
  }
} DataOut;

DataIn rxPacket;
DataOut txPacket;

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

RF24 radio(48, 49);

void setup() {
  Serial.begin(115200);
  radio.begin();
  radio.setDataRate(RF24_250KBPS) ;
  radio.setPALevel(RF24_PA_MAX) ;
  radio.setChannel(105);
  radio.enableDynamicPayloads();
  radio.enableAckPayload();
  radio.setRetries(5, 15);
  radio.setAutoAck(true);
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1, pipes[1]);
  radio.startListening();
  radio.writeAckPayload(1, &txPacket, sizeof(txPacket));
  Serial.print(F("P: ")); // preload
  txPacket.print();
}

void loop() {
  unsigned long topLoop = millis();
  static unsigned long lastMeasure;
  if (topLoop - lastMeasure >= 300) {
    lastMeasure = topLoop;
    txPacket.distance += 1;
    if (txPacket.distance > 400) {
      txPacket.distance = 0;
    }
    Serial.print(F("M: ")); // modify
    txPacket.print();
  }
  if (radio.available()) {
    radio.read(&rxPacket, radio.getDynamicPayloadSize());
    radio.writeAckPayload(1, &txPacket, sizeof(txPacket));
    Serial.print(F("R: ")); // received
    rxPacket.print();
    Serial.print(F("L: ")); // reload
    txPacket.print();
  }
}

I see what you did and thanks for the suggestion.
I will try it out and report results.

Untested sender part:

//this is for the arduino uno

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

typedef struct data {
  int trigger;
  int reverse;
  int button3;
  int lights;
  int xAxis;
  int yAxis;
  void print() {
    Serial.print(trigger);
    Serial.write(',');
    Serial.print(reverse);
    Serial.write(',');
    Serial.print(button3);
    Serial.write(',');
    Serial.print(lights);
    Serial.write(',');
    Serial.print(xAxis);
    Serial.write(',');
    Serial.println(yAxis);
  }
} dataSend;

//OUTPUT values
typedef struct AckData {
  int distance;
  int luminosity;
  void print() {
    Serial.print(distance);
    Serial.write(',');
    Serial.println(luminosity);
  }
} DataAck;

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

RF24 radio(7, 8);

DataAck rxData;

void setup() {
  Serial.begin(115200);
  radio.begin();
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1, pipes[0]);
  radio.setDataRate(RF24_250KBPS) ;
  radio.setPALevel( RF24_PA_MAX ) ;
  radio.setChannel(105);
  radio.enableDynamicPayloads();
  radio.enableAckPayload();
  radio.setRetries(5, 15);
  radio.setAutoAck( true );

  //pin for the controler
  pinMode(6, INPUT);
  pinMode(5, INPUT);
  pinMode(4, INPUT);
}

void loop() {
  unsigned long topLoop = millis();
  static unsigned long lastSend;
  //receiving the signal to send
  if (topLoop - lastSend >= 100) {
    dataSend dataSendFinal = { digitalRead(6), digitalRead(5), digitalRead(4), analogRead(A5), analogRead(A0), analogRead(A3)};
    dataSendFinal.print();
    if (radio.write(&dataSendFinal, sizeof(struct data))) {
      if (radio.isAckPayloadAvailable()) {
        radio.read(&rxData, sizeof(rxData));
        rxData.print();
      }
    }
  }
}

I followed what you did and it worked perfectly:D

Here it is the result:

In the command:

Command:
0,0,0,0,506,495
Sensors:
198,0
Command:
0,0,0,0,506,495
Sensors:
199,0
Command:
0,0,0,0,506,495
Sensors:
200,0
Command:
0,0,0,0,506,495
Sensors:
201,0
Command:
0,0,0,0,506,495
Sensors:
202,0

In the car:

Command:
0,0,0,0,505,495
Sensors
199,0
M: 200,0
Command:
0,0,0,0,506,495
Sensors
200,0
M: 201,0
Command:
0,0,0,0,506,495
Sensors
201,0
M: 202,0
Command:
0,0,0,0,506,495
Sensors
202,0
M: 203,0
Command:
0,0,0,0,506,495
Sensors
203,0

I have to say that there was a small delay in terms of the time it took to receive the message.

By the way, I had no idea that it was possible to put a function in a structure, thank you for that one :smiley:

ddmdavid01:
By the way, I had no idea that it was possible to put a function in a structure, thank you for that one :smiley:

Structures are merely classes with default public access, so functions are no problem.

With the communication finished I decided try to control the hardware and the previous problem happened. Here is the output of the car (I don't think that the output of the command is useful):

M: 170,0
Command:
0,0,0,1280,2048,24
Sensors
170,0
M: 171,0
Command:
0,0,0,0,416,4
Sensors
171,0
M: 172,0
Command:
0,0,0,0,416,4
Sensors
172,0
M: 173,0
Command:
0,0,0,0,416,4
Sensors
173,0
M: 174,0
Command:
0,0,0,0,416,4
Sensors
174,0
M: 175,0
Command:
0,0,0,0,416,4
Sensors
175,0
M: 176,0
Command:
0,0,0,0,415,3
Sensors
176,0
M: 177,0
Command:
0,0,0,0,416,4
Sensors
177,0
M: 178,0
Command:
0,0,0,0,416,4
Sensors
178,0
M: 179,0
Command:
0,0,0,0,416,4
Sensors
179,0
M: 180,0
Command:
0,0,0,0,416,4
Sensors
180,0
M: 181,0
Command:
0,0,0,0,416,4
Sensors
181,0
M: 182,0
Command:
0,0,0,0,416,4
Sensors
182,0
M: 183,0
Command:
0,0,0,0,416,4
Sensors
183,0
M: 184,0
Command:
0,0,0,0,416,3
Sensors
184,0
M: 185,0
Command:
0,0,0,0,416,3
Sensors
185,0
M: 186,0
Command:
0,0,0,0,416,4
Sensors
186,0
M: 187,0
Command:
0,0,0,0,416,3
Sensors
187,0
M: 188,0
Command:
0,0,0,0,416,3
Sensors
188,0
M: 189,0
Command:
0,0,0,0,416,4
Sensors
189,0
M: 190,0
Command:
0,0,0,0,416,3
Sensors
190,0
M: 191,0
Command:
0,0,0,0,416,4
Sensors
191,0
M: 192,0
Command:
0,0,0,0,416,4
Sensors
192,0
M: 193,0
Command:
0,0,0,0,416,3
Sensors
193,0
M: 194,0
Command:
0,0,0,0,416,4
Sensors
194,0
M: 195,0
Command:
0,0,0,0,416,3
Sensors
195,0
M: 196,0
Command:
0,0,0,0,416,4
Sensors
196,0
M: 197,0
Command:
0,0,0,0,416,4
Sensors
197,0
M: 198,0
Command:
0,0,0,0,416,3
Sensors
198,0
M: 199,0
Command:
0,0,0,0,416,3
Sensors
199,0
M: 200,0
Command:
0,0,0,0,416,4
Sensors
200,0
M: 201,0
Command:
0,0,0,0,416,3
Sensors
201,0
M: 202,0
Command:
0,0,0,0,416,4
Sensors
202,0
M: 203,0
Command:
0,0,0,0,416,4
Sensors
203,0
M: 204,0
Command:
0,0,0,0,416,4
Sensors
204,0
M: 205,0
Command:
0,0,0,0,416,3
Sensors
205,0
M: 206,0
Command:
0,0,0,0,416,3
Sensors
206,0
M: 207,0
Command:
0,0,0,0,416,4
Sensors
207,0
M: 208,0
Command:
0,0,0,0,416,3
Sensors
208,0
M: 209,0
Command:
0,0,0,0,416,4
Sensors
209,0
M: 210,0
Command:
0,0,0,0,416,4
Sensors
210,0
M: 211,0
Command:
0,0,0,0,416,4
Sensors
211,0
M: 212,0
Command:
0,0,0,0,416,4
Sensors
212,0
M: 213,0
Command:
0,0,0,0,416,4
Sensors
213,0
M: 214,0
Command:
0,0,0,0,416,4
Sensors
214,0
M: 215,0
Command:
0,0,0,0,416,4
Sensors
215,0
M: 216,0
Command:
0,0,0,0,416,4
Sensors
216,0
M: 217,0
Command:
0,0,0,0,416,4
Sensors
217,0
M: 218,0
Command:
0,0,0,0,416,4
Sensors
218,0
M: 219,0
Command:
0,0,0,0,416,4
Sensors
219,0
M: 220,0
Command:
0,0,0,0,416,3
Sensors
220,0
M: 221,0
Command:
0,0,0,0,416,4
Sensors
221,0
M: 222,0
Command:
0,0,0,0,416,4
Sensors
222,0
M: 223,0
Command:
0,0,0,0,416,3
Sensors
223,0
M: 224,0
M: 225,0
M: 226,0
M: 227,0
M: 228,0
M: 229,0
M: 230,0
M: 231,0

I have one idea of what it might be. I remember that I was developing a little project which the objective was to control the color of a RGB LED with a GUI in python from a computer (link of the problem if you are interested can't send string from python to arduino - Stack Overflow) it is already solved and I remember that the problem was that, because I was sending messages at a speed faster than the arduino processing power, the arduino would start gliching and the go off. I an the person who helped me out managed to solve the problem by only sending a message to the arduino when there was a change. Maybe that is the problem here, because when I control the wheels for a long period (only moment when it freezes) the arduino processes and receives a lot of messages at the time. If we can find a way to control the amount of messages when I control the wheels or something similar to that it could fix the problem.

Remove any delays from your code.

Control your tank non-blocking.

When it comes to delays it has no problem because there are no delays in the wheels. if I increase the time it takes to send a message when I control the wheels, would it change something?

PS what do you mean by non-blocking, never stops?

If you increase the distance of the control packets, you will get a lag, you decide whether that is aceptable.

You could remember the time of the last good reception and timeout after half a second with none,
then call stopListening, startListening and don't forget to preload the ackpayload again.

Something like this maybe?

//this is for the arduino mega
#include <Servo.h>
#include <nRF24L01.h>
#include <SPI.h>
#include <RF24.h>
#include <RF24_config.h>

//INPUT values
typedef struct InData {
  int trigger;
  int reverse;
  int button3;
  int lights;
  int xAxis;
  int yAxis;
  void print() {
    Serial.print(trigger);
    Serial.write(',');
    Serial.print(reverse);
    Serial.write(',');
    Serial.print(button3);
    Serial.write(',');
    Serial.print(lights);
    Serial.write(',');
    Serial.print(xAxis);
    Serial.write(',');
    Serial.println(yAxis);
  }
} DataIn;

//OUTPUT values
typedef struct OutData {
  int distance;
  int luminosity;
  void print() {
    Serial.print(distance);
    Serial.write(',');
    Serial.println(luminosity);
  }
} DataOut;

DataIn rxPacket;
DataOut txPacket;

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

RF24 radio(48, 49);

void setup() {
  Serial.begin(115200);
  radio.begin();
  radio.setDataRate(RF24_250KBPS) ;
  radio.setPALevel(RF24_PA_MAX) ;
  radio.setChannel(105);
  radio.enableDynamicPayloads();
  radio.enableAckPayload();
  radio.setRetries(5, 15);
  radio.setAutoAck(true);
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1, pipes[1]);
  radio.startListening();
  radio.writeAckPayload(1, &txPacket, sizeof(txPacket));
  Serial.print(F("P: ")); // preload
  txPacket.print();
}

void loop() {
  unsigned long topLoop = millis();
  static unsigned long lastMeasure;
  static unsigned long lastPacket;
  if (topLoop - lastPacket >= 500) {
    lastPacket = topLoop; // block direct reoccurrence
    radio.stopListening();
    radio.startListening();
    radio.writeAckPayload(1, &txPacket, sizeof(txPacket));
    Serial.print(F("P: ")); // preload
    txPacket.print();
  }
  if (topLoop - lastMeasure >= 300) {
    lastMeasure = topLoop;
    txPacket.distance += 1;
    if (txPacket.distance > 400) {
      txPacket.distance = 0;
    }
    Serial.print(F("M: ")); // modify
    txPacket.print();
  }
  if (radio.available()) {
    lastPacket = topLoop;
    radio.read(&rxPacket, radio.getDynamicPayloadSize());
    radio.writeAckPayload(1, &txPacket, sizeof(txPacket));
    Serial.print(F("R: ")); // received
    rxPacket.print();
    Serial.print(F("L: ")); // reload
    txPacket.print();
  }
}