Here are demo programs using the example code from Robin2's excellent tutorial, simple rf24. Relay state controlled by a push switch. The switch toggles the state of the mode variable using state change detection. If a change of state of the button pin is detected, a payload is sent to the receiver. If the state changes from high to low (active low) the mode flag is toggled. The receiver receives the payload and sets an LED, to represent the relay, per the mode value.
Sender code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const byte CE_PIN = 9;
const byte CSN_PIN = 10;
const byte buttonPin = 4;
const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 100;
void setup()
{
Serial.begin(115200);
Serial.println("Remote relay controller.");
Serial.println(" Push on, push off");
pinMode(buttonPin, INPUT_PULLUP);
radio.begin();
radio.setChannel(76); //76 library default
//RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
radio.setPALevel(RF24_PA_HIGH);
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3, 5); // delay, count
radio.openWritingPipe(slaveAddress);
radio.stopListening();
}
void loop()
{
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis)
{
sendOnButton();
prevMillis = millis();
}
}
//====================
void sendOnButton()
{
static bool mode = 0;
static bool lastButtonState = 1;
bool buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == 0)
{
mode = ! mode; // on/off
Serial.print(" mode now >> ");
Serial.println(mode);
}
lastButtonState = buttonState;
// send on both edges, error detection?
radio.write( &mode, sizeof(mode) );
}
}
Edit: Added radio.stopListening();
to sender. Thanks @Whandall.
Receiver code:
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const byte CE_PIN = 9;
const byte CSN_PIN = 10;
const byte ledPin = 7;
const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN);
bool newData = false;
bool buttonValues = 0;
//===========
void setup()
{
Serial.begin(115200);
Serial.println("Remote relay controller");
pinMode(ledPin, OUTPUT);
radio.begin();
radio.setChannel(76); //76 library default
//RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
radio.setPALevel(RF24_PA_HIGH);
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
}
//=============
void loop()
{
getData();
showData();
actOnData();
}
//==============
void getData()
{
if ( radio.available() )
{
radio.read( &buttonValues, sizeof(buttonValues) );
newData = true;
}
}
void showData()
{
if (newData == true)
{
Serial.print("Data received ^^ ");
Serial.print("mode = ");
Serial.println(buttonValues);
Serial.println();
//newData = false;
}
}
void actOnData()
{
if (newData == true)
{
digitalWrite(ledPin, buttonValues);
newData = false;
}
}
Tested, successfully, on real hardware.