Error with nRF24L01 and HC-SR04

Hello,

I have written 2 programs for 2 Arduino .

This will send a pulse or send or receive a HIGH signal through 2 nRF24L01 .

Unfortunately this does not work completely .

Therefore, I hope for your help .

Enclosed the programs

Arduino One:

#include <NewPing.h>        //Importiere HC-SR04 Bibliothek
#include <SPI.h>            //Importiere SPI Bibliothek


#include <nRF24L01.h>       //nRF24L01 Bibliotheken
#include <RF24.h>           //nRF24L01 Bibliotheken

//HC-SR04 Zuweisungen
#define TRIGGER_PIN  6   //Arduino Pin 6 --> Trigger-Pin HC-SR04
#define ECHO_PIN     5  //Arduino Pin --> Echo-Pin HC-SR04
#define MAX_DISTANCE 100 //Maximale Distanz in Zentimeter.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

//nRF24L01 Zuweisungen
#define CE_PIN   9
#define CSN_PIN 10


// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t   deviceID = 0xE8E8F0F0E1LL; // Define the ID for this slave

int valChange = 1;

unsigned long aktuelleMillis;                     //Aktuelle Millisekunden
unsigned long vergangeneMillis;                   //Vergangene Millisekunden
unsigned long txIntervalMillis = 100;             //TX Millisekunden Interval
unsigned long Start, Stop, Zeit;
unsigned long HCMillis = 0;                       //Vergange Zeit HC-SR04
unsigned long HCInterval = 30;                    //HC-SR04 Intervall Zeit in Millisekunden

int Aktiv = 0;

RF24 radio(CE_PIN, CSN_PIN);

int dataReceived[2];
int ackData[2] = {12,23};

void displayResult()
{
  float h,m,s,ms;
  unsigned long over;
  Zeit=Stop-Start;
  h=int(Zeit/3600000);
  over=Zeit%3600000;
  m=int(over/60000);
  over=over%60000;
  s=int(over/1000);
  ms=over%1000;
  Serial.print("Vergangene Zeit RAW: ");
  Serial.println(Zeit);
  Serial.print("Vergangene Zeit: ");
  Serial.print(h,0);
  Serial.print("h ");
  Serial.print(m,0);
  Serial.print("m ");
  Serial.print(s,0);
  Serial.print("s ");
  Serial.print(ms,0);
  Serial.println("ms");
  Serial.println();
}

void setup() {
    Serial.begin(9600);
    delay(1000);

    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1,deviceID);
    radio.enableAckPayload();
    radio.writeAckPayload(1, ackData, sizeof(ackData));
    radio.startListening();
        Serial.println("Stoppuhr Empfaenger bereit");
}

void loop() {
  aktuelleMillis = millis();
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).

    if ( radio.available() ) {
        radio.read( dataReceived, sizeof(dataReceived) );
        radio.writeAckPayload(1, ackData, sizeof(ackData));
    }

    if (dataReceived[0] == 1 && Aktiv == 0)
    {
    Start=millis();
    dataReceived[0] = 0;
    Aktiv = 1;
    delay(200);
    Serial.println("Stoppuhr gestartet.");
    Serial.println(Aktiv);
    }
    
    if (Aktiv == 1 && uS / US_ROUNDTRIP_CM <70)
    {
    Stop=millis();
    Aktiv = 0; 
    delay(200);
    displayResult();
    }
 }

Arduino Two:

#include <NewPing.h>        //Importiere HC-SR04 Bibliothek
#include <SPI.h>            //Importiere SPI Bibliothek

#include <nRF24L01.h>       //nRF24L01 Bibliotheken
#include <RF24.h>           //nRF24L01 Bibliotheken

//HC-SR04 Zuweisungen
#define TRIGGER_PIN  6   //Arduino Pin 6 --> Trigger-Pin HC-SR04
#define ECHO_PIN     5  //Arduino Pin --> Echo-Pin HC-SR04
#define MAX_DISTANCE 100 //Maximale Distanz in Zentimeter.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.


//nRF24L01 Zuweisungen
#define CE_PIN   9
#define CSN_PIN 10


// NOTE: the "LL" at the end of the constant is "LongLong" type
// These are the IDs of each of the slaves
const uint64_t slaveID[1] = {0xE8E8F0F0E1LL} ;

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio


int dataToSend[1];
int Sende = 0;

unsigned long aktuelleMillis;                     //Aktuelle Millisekunden
unsigned long vergangeneMillis;                   //Vergangene Millisekunden
unsigned long txIntervalMillis = 1000;             //TX Millisekunden Interval
unsigned long HCMillis = 0;                       //Vergange Zeit HC-SR04
unsigned long HCInterval = 40;                    //HC-SR04 Intervall Zeit in Millisekunden

int txVal = 0;
int ackMessg[6];
byte ackMessgLen = 4; // NB this 4 is the number of bytes in the 2 ints that will be recieved


void setup() {

    Serial.begin(9600);
    Serial.println("Track Control Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.enableAckPayload();
    radio.setRetries(4,15); // delay, count

}

//====================

void loop() {
    aktuelleMillis = millis();


    unsigned int uS = sonar.ping();                           // Send ping, get ping time in microseconds (uS).
           
    if (aktuelleMillis - vergangeneMillis >= txIntervalMillis) 
    {

    radio.openWritingPipe(slaveID[0]);                       // calls the first slave
                                                             // there could be a FOR loop to call several slaves in turn
    }
    
    if (uS / US_ROUNDTRIP_CM <=20){             // Distanz größer als 10cm und kleiner als 70cm
    dataToSend[0] = 1; // 
    bool rslt;
    rslt = radio.write( dataToSend, sizeof(dataToSend) );
    }
    else
    {
    dataToSend[0] = 0; // 
    bool rslt;
    rslt = radio.write( dataToSend, sizeof(dataToSend) );
    }
    if ( radio.isAckPayloadAvailable() ) {
        radio.read(ackMessg,ackMessgLen);
        Serial.print("Acknowledge received: ");
        Serial.print(ackMessg[0]);
    }
    vergangeneMillis = millis();
    Serial.println(dataToSend[0]);
 }

Unfortunately this does not work completely .

The code on each end does something. You left that part out. You expect the code on each end to do something. You left that part out, too.

Therefore, I hope for your help .

We do not know what help you need.

OK,

The Problem is, that the Arduino One doesnt send a Signal when the HC-SR04 gives the value. Or the second Arduino doesnt receive it. But this is my problem, because i doesnt know it.

What do your Serial.print()s tell you is happening? Does Arduino One ever get the data needed to set Aktiv to 1? Does the ping sensor ever get a value of less than 70?

Arduino One doesnt get the data to set Aktiv to 1. But Arduino Two dataToSend[0] will set to 1.

Markus1995: Arduino One doesnt get the data to set Aktiv to 1. But Arduino Two dataToSend[0] will set to 1.

But it sends the data only if it measures a distance of less than 21 centimeters, and the pipe is opened for writing (which happens only if aktuelleMillis - vergangeneMillis >= txIntervalMillis, which seems unlikely, as vergangeneMillis is set to aktuelleMillis at the end of each pass through loop()).

properly my fault is there....

can i open the pipe constantly?

can i open the pipe constantly?

I would think that you would open the pipe for writing when you have something to write.

The pair of programs in this link may give you some ideas.

...R