• John L
  • Junior Member
  • Posts 1
  • Reputation 0
  • Member Since
  • Last Active
  • Name John L
    This user has not added any photos yet.
All Posts Topics Started Likes
nRF24L01 transceiver
Goal: to transmit a command to a receiver and have it confirm back to the transmitter.

Hardware/Software: 2 x nRF24L01, 2 x Sainsmart UNO, I.D.E 1.8.0

Progress: I successfully completed the brainy-bits nRF24L01 tutorial, the only modification was to substitute the LED stick with a single LED. I successfully completed the "Getting Started" example in the RF24 library. I tried to educate myself with the write/read RF24 library command structure by TMRh20.

The Problem: Using the knowledge gained above I combined parts of the above into Transmit & Receive sketches such that when a button is pressed on the transmitter, a light will indicate on the receiver at which time the receiver should send back a confirmation back to the transmitter and light an LED. While I can get the receiver LED to indicate, I just cannot seem to come up with a set of commands that will send and receive the desired confirmation. The code functions are just not that hard, so I am puzzled as to what I am missing. Note that in the Transmit sketch below there are a few lines commented in the void loop...for some reason, uncommented they keep the receiver LED ON when the button is pressed.

Thanks for any help/suggestions...John

// Transmitter Sketch
#include <SPI.h>
#include <RF24.h>

#define LED_PIN 6 // led to confirm receiver receipt of command
#define SwitchPin 8 // push switch connected to Pin 8

int SentMessage[1] = {000}; // Used to store value before being sent through the nRF24L01
int ReceivedMessage[1] = {000}; // Used to store confirmed value received by the NRF24L01

bool radioNumber=1;
RF24 radio(9,10);
byte addresses[][6] = {"1Node","2Node"};
bool role = 1; //Control transmit 1/receive 0

void setup() {
pinMode(SwitchPin, INPUT_PULLUP); // Define the push switch UNO pin as an Input using Internal Pullups
digitalWrite(SwitchPin,HIGH); // Set Pin to HIGH at beginning
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600); // Get ready to send data back for debugging purposes
radio.begin(); // Get the transmitter ready

radio.openWritingPipe(addresses[1]); // Where we send data out
radio.openReadingPipe(1,addresses[0]);// Where we receive data back
}
void loop() {
if (digitalRead(SwitchPin) == LOW){ // when push switch is activated
SentMessage[0] = 111;
radio.write(SentMessage, 1); // Send value through nRF24L01
//radio.startListening(); // Listen to see if confirmation received
//if (radio.available()){
//radio.read(ReceivedMessage, 1);} // Read information from the NRF24L01
//if (ReceivedMessage[0] == 333) { // Indicates switch was pressed
//digitalWrite(LED_PIN,HIGH);
//radio.stopListening();
//}
}
else {
SentMessage[0] = 000;
radio.write(SentMessage, 1);
}
}
*********************************************************
// Receiver Sketch
#include <SPI.h>
#include <RF24.h>

#define LED_PIN 14 // to indicate when command received

int ReceivedMessage[1] = {000}; // Used to store value received by the nRF24L01
int SentMessage[1] = {000}; // Used to store value before sending confirmation from the nRF24L01

bool radioNumber=0;
RF24 radio(9,10);
byte addresses[][6] = {"1Node","2Node"};
bool role = 0; //Control transmit/receive

void setup() {
radio.begin(); // initialize nRF24L01 receiver
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
radio.startListening();
pinMode(LED_PIN, OUTPUT); // Set UNO pin to an OUTPUT
}

void loop() {
while (radio.available())
{
radio.read(ReceivedMessage, 1); // Read information from the NRF24L01

if (ReceivedMessage[0] == 111) // Indicates switch is pressed
{
digitalWrite(LED_PIN,HIGH);
radio.stopListening(); // enter transmit mode
SentMessage[0] = 333;
radio.write(SentMessage, 1); // Send confirmation from nRF24L01
radio.startListening();
}
else
{
digitalWrite(LED_PIN, LOW);
}
delay(10);
}
}
0 0
count post selected

Add a Website Forum to your website.