nRF24 project help

I have a simple tamper alarm using an RF nano with 4 switches as a transmitter and another RF nano with an LCD screen as receiver. As the switches are triggered this info is sent to the receiver and displayed on the screen, thus the screen acts as a sort of memory because it displays all the tampering that has taken place, in order. Unfortunately if there's radio interference then the receiver doesn't get that piece of info and the log is incomplete making the system unreliable.

I'm guessing either the sketch needs some sort of feedback loop so that the transmitter keeps sending until the receiver confirms that it has got the latest change and displayed it correctly but i'm already at the very limits of my coding knowledge so some idiot proof pointers would be appreciated.
Ideally using all the existing hardware as it's glued in a case and probably won't come out without destroying everything.

Transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(10, 9); // CE, CSN
const byte address[6] = "00001";



const byte inputs[] = {2, 3, 4, 5, 6};
const byte NUMBER_OF_INPUTS = sizeof(inputs) / sizeof(inputs[0]);  //calculate number of elements in the array
const char * messages[][2] =
{
  {"1" , "-1" },
  {"2" , "-2" },
  {"3" , "-3" },
  {"4" , "-4" },
  {"5" , "-5" },
};

byte previousStates[NUMBER_OF_INPUTS];
byte action;

void setup()
{

  Serial.begin(9600);
  //
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  pinMode(9,OUTPUT);
  digitalWrite(9,LOW);
  radio.begin();
  radio.openWritingPipe(address);
  radio.stopListening();
  //
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
        for (int switchPin = 0; switchPin < NUMBER_OF_INPUTS; switchPin++)
      previousStates[switchPin] = digitalRead(inputs[switchPin]);
}

void loop()
{ 
  const char text[32];
  for (int switchPin = 0;switchPin < NUMBER_OF_INPUTS; switchPin++)
  {
    byte currentState = digitalRead(inputs[switchPin]);
    if (currentState != previousStates[switchPin])
    {
      if (currentState == HIGH)
      {  
        action = 1;
        // mySwitch.send(messages[switchPin][action]);
      }
      else
      { 
        action = 0;
        // mySwitch.send(messages[switchPin][action]);
      }


        Serial.write(messages[switchPin][action], sizeof(messages[switchPin][action]));
        radio.write(messages[switchPin][action], sizeof(messages[switchPin][action]));
      // mySwitch.send(messages[switchPin][action]);
      delay(400);
    }
    previousStates[switchPin] = currentState;
  }
  delay(2000);
  }

Receiver

#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <SPI.h>
#define OLED_RESET -1

#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(10,9); // CE, CSN
const byte address[6] = "00001";
Adafruit_SSD1306 display(OLED_RESET);

void setup() {
  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setCursor (0,5); // position the cursor
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.display();
  
  pinMode(9,OUTPUT);
  digitalWrite(9,LOW);
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.startListening();
}
void loop() {
  if (radio.available()) {
    char text[32] = "";
    radio.read(&text, sizeof(text));
    Serial.write (text);
    Serial.println(text);
    
      display.print(text);
      display.display();
  }
}

See the setretries() function in the RF24 Class Reference.

void RF24::setRetries ( uint8_t delay, uint8_t count)

Set the number and delay of retries upon failed submit.

Parameters:

delay: How long to wait between each retry, in multiples of 250us, max is 15. 0 means 250us, 15 means 4000us.

count: How many retries before giving up, max 15

I don't see (or understand how to) set up the 2 way protocol that i assume that command would need

Check this library:

https://github.com/RalphBacon/nRF24L01-transceiver/

nRF24L01 Transceiver Use your Arduino to transmit (and receive) data reliably with this easy to use module. See the accompanying video #73 here: https://www.youtube.com/c/RalphBacon

Thanks for the pointer - but that is a terrible video; i think there's 10 mins of great info somewhere in it buried amongst the un-related stories and loop-backs to correct errors he's made earlier in the video & he's inconsistent about terms and names he uses which is really scrambling my brain.

It also goes into the channels and data pipes side of nRF24 that i have completely failed to get a handle on in the last year or so.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.