Code for time sense radio received

Hello!

I need som ideas how to solve my issue.

Im sending data from one arduino to another with 2 nRF24L01, displaying it on a tft display. It working just fine but I want another thing.

I want to have a value that thells me if a uppdated value hav ben receved. In the end i want a green marker on the TFT to turn red if no new data have ben receved for say 10 sec. the TFT thin is no problem but maby you guys can give me som ideas how to fix my code to get the “time since latset received vallues”

here is my receiving arduino code: (the sending arduino is sending updated values every 5 sec.

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

RF24 radio(7, 8); // CE, CSN

const byte address[6] = "00001";

char copy[50];

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  Serial.println("startad");
}

void loop() {


  //Radio
  if (radio.available()) {
    radio.read(&copy, sizeof(copy));

  }


  Serial.println(copy);


  delay (1000);


}

Any good ideas is welcome! tnx!

Make the receiver send an acknowledge back to the sender. The sender looks for the ack and times out if it is not received in time.

Tnx man! :slight_smile: yes maby that is the simplest way.

I was more loking fore a way to just count the time from a message received.

void loop()
{
  static unsigned long timeOfLastReceive = 0;
  
  //Radio
  if (radio.available())
  {
    radio.read(&copy, sizeof(copy));
    timeOfLastReceive = millis();
  }


  unsigned long millisecondsSinceLastReceive = millis() - timeOfLastReceive;
  Serial.println(millisecondsSinceLastReceive / 1000.0, 3); // Show time in seconds
  Serial.println(copy);
  
  delay (1000);
}

SL42:
Tnx man! :slight_smile: yes maby that is the simplest way.

I was more loking fore a way to just count the time from a message received.

From receive up to what?

SL42:
Tnx man! :slight_smile: yes maby that is the simplest way.

I was more loking fore a way to just count the time from a message received.

Capture the value of millis() when you receive it. The elapsed time will always be the current value of millis(), subtracting that saved value. Code is in reply #3.

With the delay() statement in the loop() of the receiver part, you risk losing the transmissions which occur during that delay.

Tnx allot out there:) now it works just fine.