Please help with nrf24 code

I’m new to Arduino, this is my 1st project.

I’m bulding wireless timer using:
receiver:
1.uno r3
2. dfrobot lcdkeypad
3.nrf24l01

transmitter:
1.pro mini 3.3v
2.nrf24l01
3.piezo elment / vibra sensor

I have been searching on the internet on how to use nrf24l01, but so little came up. Basically I wanted to stop the timer on receiver if vibration are detected on the transmitter side using nrf24l01.

Here are my code for:
transmitter:

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

int msg[1];

RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;

const int ledPin = 5; 
const int knockSensor = A0; 
const int treshold = 100; 

int sensorReading = 0; 
int ledState = LOW; 
unsigned long lastPeakTime = 0; 
int currentPeak = 0;
int blinkCounter = 0;

void setup() {
  radio.begin();
  radio.openWritingPipe(pipe);
  
  pinMode(ledPin, OUTPUT);

}

void loop() {
  sensorReading = analogRead(knockSensor);
  if (sensorReading > currentPeak) {
    currentPeak = sensorReading;
  }

  if (sensorReading >= treshold) {
    while(blinkCounter <= 3)
        {
    msg[0] = 111;
    radio.write(msg, 1);

        Serial.println(blinkCounter);
        digitalWrite(ledPin, HIGH);
        delay(250);
        digitalWrite(ledPin, LOW);
        delay(250);
        blinkCounter++;
        }
        blinkCounter = 0; 
      delay(50);  
  }
}

receiver:

#include <SPI.h>
#include <RF24.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int msg[1];

RF24 radio(2,3);

const uint64_t pipe = 0xE8E8F0F0E1LL;

// variables used on more than 1 function need to be declared here
unsigned long start, finished, elapsed;
boolean r = false;
// Variables for button debounce time
long lastButtonPressTime = 0; // the last time the button was pressed
long debounceDelay = 50; // the debounce time; keep this as low as possible

void setup()
{
  //set buzzer pin 10 to output
 pinMode(10, OUTPUT);
  
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
  
lcd.begin(16, 2); // initialize the lcd (16 chars, 2 lines)

// a little introduction :)
lcd.setCursor(1, 0); // set the cursor to first character on line 1 - NOT needed (it sets automatically on lcd.begin()
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(" Press X for Start & Stop");
lcd.setCursor(2, 1); // set the cursor to 3rd character on line 2
lcd.print("Start & Stop");
}

void loop()
{
CheckStartStop();
StopWifi();
DisplayResult();
}

void beep(unsigned char delayms){
  analogWrite(10, 20);  
  delay(delayms);       
  analogWrite(10, 0);   
  delay(delayms);
}

void StopWifi(){
     if (msg[0] == 111){
     CheckStartStop();
     }
     }

void CheckStartStop()
{
int x = analogRead (0); // assign 'x' to the Arduino's AnalogueInputs (Shield's buttons)
if (x < 800 && x > 600 ) // if the button is SELECT
{
if ((millis() - lastButtonPressTime) > debounceDelay)
{

if (r == false)
{
  delay(2000);
  beep(100);
lcd.clear();
lcd.setCursor(2, 0); // needed
lcd.print("Runner Time");
start = millis(); // saves start time to calculate the elapsed time
}
else if (r == true)
{
lcd.setCursor(2, 0); // needed
lcd.print(" Final Time ");
}
r = !r;
}
lastButtonPressTime = millis();
}
}

void DisplayResult()
{
if (r == true)
{
finished = millis(); // saves stop time to calculate the elapsed time
// declare variables
float h, m, s, ms;
unsigned long over;

// MATH time!!!
elapsed = finished - start;

h = int(elapsed / 3600000);
over = elapsed % 3600000;
m = int(over / 60000);
over = over % 60000;
s = int(over / 1000);
ms = over % 1000;
// display the results
lcd.setCursor(1, 1);
lcd.print(h, 0); // display variable 'h' - the 0 after it is the number of algorithms after a comma (ex: lcd.print(h, 2); would print 0,00
lcd.print("h "); // and the letter 'h' after it
lcd.print(m, 0);
lcd.print("m ");
lcd.print(s, 0);
lcd.print("s ");
if (h < 10)
{
lcd.print(ms, 0);
lcd.print("ms ");
}
}
}

Labeling something a receiver, without including any code to receive anything, hardly makes sense.

There ARE examples that come with the nRF24L01 library. Have you tried them?

Thank you for your fast reply. I forgot to delete the back slash on the code, because i tried to implement sample from rf24 library. I have corrected the code above.

My problem are; how to send code from sender => transmitter and stop the timer. Sample code on rf24 library are too confusing for me.

I used this Tutorial to get my NFR24s working.

Before you do anything else you need to ensure that you can send and receive data between your Arduinos so just use the standard examples until you have achieved that.

If you have problems it will be easier to help if you are using the standard examples.

When you get that working then you can adapt it to your own requirements.

...R

Thank you Robin2 for your reply. Yes i have read and try the tutorial link you posted as a matter of fact that is the fist tutorial i stubled upon 2 weeks ago. And yes both nrf24 can talk send and receive data between them, no problem there. Yes, i have tried every example available in rf24 library.

The problem with my sketch are, i use analog read to read button state and use it to start and stop the timer. But what i dont understand is how to send data/interupt/etc either one is suitable to receiver and use it as a trigger to stop the timer.

Thank you.

Hope someone can pointed me to right direction, or fix my sketch. I have been strugling with this for 2 weeks.

i use analog read to read button state

The state of the knock sensor button?

But what i dont understand is how to send data/interupt/etc either one is suitable to receiver and use it as a trigger to stop the timer.

You have to define some message that means start and some message that means stop.

"1" and "0" seem somehow appropriate.

You need to send the message.

You need to receive the message. You need to respond to the message.

Thank you PaulS

That is exacly what i tought of. I want the timer to start only if i press the select button on lcd keypad. And stop if it detect vibration remotely. If i use this code on transmitter

 msg[0] = 111;
    radio.write(msg, 1);

Is this correct code for receiver?

If radio.read = (msg(0) == 111){
// how to make timer stop ???

Is this correct code for receiver?

No. The radio.read() method takes an address of a buffer that it can write to and the size of the buffer. The first element of the buffer may be 111, when the message is read.

Ok PaulS, it start to make sense to me.

if (radio.available()){
   bool done = false;    
   while (!done){
     done = radio.read(msg, 1);      
     if (msg[0] == 111){
//how to make timer stop???
}
     if (msg[0] == 111){
//how to make timer stop???
}

You need to seriously reorganize your code.

You have:

void DisplayResult()
{
if (r == true)
{
finished = millis(); // saves stop time to calculate the elapsed time

Which makes no sense. finished should be assigned a value somewhere else. DisplayResult() should use the values of finished and start to compute the interval. But it should not determine the finished time.

You need to define what "stop the timer" means. Does it mean that you want to stop accumulating time?

If so, you need an accumulator so that every time you "stop the timer" you accumulate the time that the timer was running. Starting the timer updates start; stopping the timer updates finished AND the accumulator and sets start to 0 (that way you can tell whether timing is happening, so stopping twice doesn't mess up the accumulated time).

PaulS:
You need to define what “stop the timer” means. Does it mean that you want to stop accumulating time?

If so, you need an accumulator so that every time you “stop the timer” you accumulate the time that the timer was running. Starting the timer updates start; stopping the timer updates finished AND the accumulator and sets start to 0 (that way you can tell whether timing is happening, so stopping twice doesn’t mess up the accumulated time).

Yes, that is exacly what i intended to do. Stopping the timer show result on lcd (accumulated time from start to stop). And set the start to 0. Starting timer update start.

I found arduino simple timer from instructables, i copied the code and tried to adapt it to suit my needs. I want to make timer for nonprofit youth community for their upcoming event on january 1st. We cant afford to buy pro timer. Im totally new to this arduino,

Thank you for your time, patient, and help paulS, i will tried again tomorrow, its 3am in here.