NRF24L01 Two-way communication

Hello All -

I am fairly new to Arduino and have been working on a project for quite some time now. Essentially what I am doing is creating an alarm system that will be activated when someone touches a capacitive toggle switch. A siren will go off and a LCD screen will act as a strobe/display information it has been given.

Each device would have their name to send to other devices when activated and display on it’s own LCD… So if one was activated in room 201, it would send the message “201” to the next closest device, then that one would relay “201” to the next closest device and so on.

I will be using multiple alarms all throughout the building, and they need to communicate through my NRF24L01 modules. I have been successful using the attached code to make the transmitter light up an LED with the push of a button… But I need the same sort of thing happen going both ways.

I have been banging my head against the wall trying to combine this code together somehow but have had no luck. I have searched this forum and across the web but haven’t found any tutorials using this specific code. I think the answer might be within the Pipe section (0xE14BC8F482LL)… But honestly I have no clue.

Would it even be possible to combine the code I am attaching here? I would assume so because they are transceivers. How would I make the other LED light up when the second button is pushed? How would I make this work if I wanted to push the button on one Arduino and relay the message to 20, 30, 100, etc. other devices throughout a building?

Thank you for any help you can provide!

TRANSMITTER CODE:

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

int DataMsg[1];

RF24 radio(9,10);

const uint64_t pipe = 0xE14BC8F482LL;

int Button = 7;

void setup()
{
  pinMode(7, INPUT);
  digitalWrite(7,HIGH);

  Serial.begin(57600);
  Serial.println("NRF24L01 Transmitter");

  radio.begin();
  radio.openWritingPipe(pipe);
}

void loop()
{
  if (digitalRead(Button) == LOW)
  {
    Serial.println("Alarm On!");
    DataMsg[0] = 1;
    radio.write(DataMsg, 1);
  }
  if (digitalRead(Button) == HIGH)
  {
    Serial.println("Alarm Off!");
    DataMsg[0] = 2;
    radio.write(DataMsg, 1);
  }
}

RECEIVER CODE

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

int DataMgs[1];

RF24 radio(9,10);

const uint64_t pipe = 0xE14BC8F482LL;

int LED = 5;

void setup()
{
  pinMode(LED, OUTPUT);
  Serial.begin(57600);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
}

void loop()
{
  if (radio.available())
  {
    bool done = false;    
    while (!done)
    {
      radio.read(DataMgs, 1);
      Serial.print("NRF24L01 Receiver: ");    
      Serial.println(DataMgs[0]);
      
      if (DataMgs[0] == 1)
      {
        delay(10);
        digitalWrite(LED, HIGH);
      }

      if (DataMgs[0] == 2)
      {
        delay(10);digitalWrite(LED, LOW);
      }
      delay(100);
    }
  }
  else
  {
  Serial.println("Waiting for signal...");
  }
}

kylecoogler:
So if one was activated in room 201, it would send the message "201" to the next closest device, then that one would relay "201" to the next closest device and so on.

That sounds like a job well suited to the TMRh20 RF24 Network library - but I have never used it myself.

...R
Simple nRF24L01+ Tutorial

kylecoogler:
I am fairly new to Arduino and have been working on a project for quite some time now.

A kind of contradiction, isn't it?

The "code" is one of the worst I have ever seen, very unsensible,
based on a bad example of the old library, just changed enough to compile.

  bool done = false;   
    while (!done)

The above are all accesses to done.

Robin2:
That sounds like a job well suited to the TMRh20 RF24 Network library - but I have never used it myself.

...R
Simple nRF24L01+ Tutorial

Thanks Robin2! I will make sure to dive into the tutorial ASAP!

Whandall:
A kind of contradiction, isn't it?

The "code" is one of the worst I have ever seen, very unsensible,
based on a bad example of the old library, just changed enough to compile.

  bool done = false;   

while (!done)



The above are **all** accesses to done.

Thanks for the feedback, Whandall! I have been working on an Arduino project for about six months now - so I am not a complete noob but I in no way know it all so I appreciate you being willing to help.

I have tried other versions of the "code" but have been unsuccessful until I found the one I shared in my post to this forum. Was just looking for feedback on how to combine them to work together. If there is another that is similar to this and might work for my purposes I would love to try it out!

Thanks for the help.

kylecoogler:
Was just looking for feedback on how to combine them to work together.

There is nothing worthy in these sketches,
try Robin2's tutoral sketches first to get a feeling what is possible with the NRFs.

For my taste the tutorials are focused too much on polling,
but once you understand what the chips can do,
you can decide yourself if you want to use such an approach.

I would use a rather passive data collector that acknowledges the reports of the nodes,
(which would include a field to identify the sender for collector and peers),
and would make all nodes listen to that report pipe (without ack) also,
besides their personal address with acks (if needed).
All stations report there states regularly if unchanged and additionally on changes.

For the button handling look into the Bounce2 library,
it makes debouncing and state change detection very easy.
Ok it may not of much use for touch buttons,
then you probably have to write the state change detection yourself,
but there is an example in the IDE that covers that.