Adding Condition (IF) to an RF24L01 Receiver Module

Hi,

I use the codes below to send a message from Tx to an Rx and it works well. What I want to do (but not successful yet) is to send one of two possible messages from the Tx and to make the Rx respond accordingly regarding the message received.

For example; if I press a button (say green), the transmitter will send the message "Green" and the receiver will understand that the message is "Green" and the green LED will be ON. If the red button is pressed the red LED will be ON.

How should I change the codes to achieve this?

TRANSMITTER CODE

/*

  • Arduino Wireless Communication Tutorial
  • Example 1 - Transmitter Code
  • by Dejan Nedelkovski, www.HowToMechatronics.com
  • Library: TMRh20/RF24, GitHub - nRF24/RF24: OSI Layer 2 driver for nRF24L01 on Arduino & Raspberry Pi/Linux Devices
    */
    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    RF24 radio(7, 8); // CE, CSN
    const byte address[6] = "00001";
    void setup() {
    pinMode(5, OUTPUT);
    pinMode(4, INPUT);
    radio.begin();
    radio.openWritingPipe(address);
    radio.setPALevel(RF24_PA_MIN);
    radio.stopListening();
    }
    void loop() {
    if (digitalRead(4)== HIGH)
    {
    const char text[] = "Hello World";
    radio.write(&text, sizeof(text));
    digitalWrite(5, HIGH);
    delay(100);
    digitalWrite(5, LOW);
    delay(1000);
    }
    }

RECEIVER CODE

/*

  • Arduino Wireless Communication Tutorial
  • Example 1 - Receiver Code
  • by Dejan Nedelkovski, www.HowToMechatronics.com
  • Library: TMRh20/RF24, GitHub - nRF24/RF24: OSI Layer 2 driver for nRF24L01 on Arduino & Raspberry Pi/Linux Devices
    */
    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    RF24 radio(7, 8); // CE, CSN
    const byte address[6] = "00001";
    void setup() {
    pinMode(5, OUTPUT);
    Serial.begin(9600);
    radio.begin();
    radio.openReadingPipe(0, address);
    radio.setPALevel(RF24_PA_MIN);
    radio.startListening();
    }
    void loop() {
    if (radio.available()) {
    char text[32] = "";
    radio.read(&text, sizeof(text));
    digitalWrite(5, HIGH);
    delay(100);
    digitalWrite(5, LOW);
    Serial.println(text);
    }
    }

I suggest that you send single character code such as 'G' or 'R' then you can simply test the received data

if (receivedChar == 'G')
{
  //do green stuff
}
else if (receivedChar == 'R')
{
  //do red stuff
}

If you really want/need to send a string then test its value using the strcmp() function

If you want to send one message when pin 4 is high, and another when pin 6 is high:

if (digitalRead(4)== HIGH)
 {
  const char text[] = "Green";
  radio.write(&text, sizeof(text));
  digitalWrite(5, HIGH);
  delay(100);
  digitalWrite(5, LOW);
  delay(1000);
 }
 if (digitalRead(6)== HIGH)
 {
  const char text[] = "Red";
  radio.write(&text, sizeof(text));
  digitalWrite(5, HIGH);
  delay(100);
  digitalWrite(5, LOW);
  delay(1000);
 }

Then, on the receiver:

 if (radio.available())
  {
    byte payloadSize = radio.getPayloadSize();

    char text[32] = "";
    radio.read(&text, sizeof(text));

    text[payloadSize] = '\0';
    Serial.println(text); // NOW you can properly print text. Before, you could NOT.

    if(strcmp(text, "Green") == 0)
    {
       digitalWrite(5, HIGH);
       delay(100);
       digitalWrite(5, LOW);
    }
    else if(strcmp(text, "Red") == 0)
    {
        // diddle with some other pin
    }
  }

Dear UKHeliBob and PaulS,

Thank you for your very quick and useful replies. I will test the codes by integrating into my project codes and will let you know about the results.