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();
}
}