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