Help with Ultrasonic Sensor

Okay I have been messing around with this program for a bit and I’m close, but I have a few issues. The program is set up so that two nrf24 boards communicate to each other. One board (transmitter) has an ultrasonic sensor on it and transmits the sensor readings to the receiver. The other board (receiver) receives this data and turns on an led. The boards communicate with one another but the issue lies in the sensor value portion of the program. The program needs to be set up so that the led only turns on once the sensor value has changed by a certain threshold. I am having two issues. First, the ultrasonic sensor is terrible at detecting moving objects. I assume this has to do with the timer set in the program. How should I change the timer to be more responsive so that faster moving objects (10mph) are detected? Second, the portion where the transmitter reports a change in sensor values partially works. It definitely notices a change, but the led flashes randomly. What do I need to do to fix this portion of the program?

Receiver:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define led 4  

RF24 radio(10, 9); // CE, CSN for rf_nano
byte address[6] = "00001";

long diff = 0; 

void setup()
{
   Serial.begin(9600);
   pinMode(led, OUTPUT);
   radio.begin();
   radio.openReadingPipe(0, address);
   radio.setPALevel(RF24_PA_MIN);
   radio.startListening();
}

void loop()
{
   if (radio.available()) 
      radio.read(&diff, sizeof(diff));
      Serial.print("diff = ");
      Serial.println(diff);
      if (diff >= 100)
      {
         digitalWrite(led, HIGH);
         
      }
      else
      {
         digitalWrite(led, LOW);
      }
   
}

Transmitter:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define trigPin 2
#define echoPin 3

RF24 radio(10, 9);
byte address[6] = "00001";

void setup()
{
   Serial.begin(9600);
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
   radio.begin();
   radio.openWritingPipe(address);
   radio.setPALevel(RF24_PA_MIN);
   radio.stopListening();
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 50;
   if (millis() - timer >= interval)
   {
      timer = millis();
      long duration, distance;
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration / 2) / 29.1;
      Serial.println(distance);
       int r = distance;
       int index;
       int diff = r-index;
    Serial.println(diff);
    index = r;

      radio.write(diff, sizeof(diff));
   }
}

any input would be appreciated!

 int index;
       int diff = r-index;

?

to my understanding index is the last recorded value, so difference is the current value minus the last current value. I could be (probably) completely wrong, that's why I'm asking.

To my understanding, the value of index is undefined.

Did you mean to qualify it “static”?

Whatever I need to do to label it as the last recorded value of the ultrasonic sensor. I don't know what index does exactly, I have just seen it used before.

So, static int index;

what does this do?

It stops the value of index being undefined.
Initially it will be zero, and then you keep updating it.

I updated my transmitter program but I am still facing the same issue. The ultrasonic sensor is now responsive but I still can’t get it to record only when a the distance value changes.

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define trigPin 2
#define echoPin 3

RF24 radio(10, 9);
byte address[6] = "00001";

void setup()
{
   Serial.begin(9600);
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
   radio.begin();
   radio.openWritingPipe(address);
   radio.setPALevel(RF24_PA_MIN);
   radio.stopListening();
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 50;
   if (millis() - timer >= interval)
   {
      timer = millis();
      long duration, distance;
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration / 2) / 29.1;
      Serial.println(distance);
      static int index;
       int r = distance;
       int diff = r-index;
    Serial.println(diff);
    index = distance;

      radio.write(diff, sizeof(diff));
   }
}

Fixed the issue, my greater than or less than portion on my receiver was messed up.