I am using the NRF24L01 modules compatible with arduino and following a tutorial on how to communicate using them between two distinct arduinos, a transmitter and a receiver. I have programmed both a transmitter and a receiver as instructed and have all the wiring correct. However, the message "hello world" is not reaching the receiver, either because it isn't being sent or it isn't being seen. These modules are right next to eachother, so connection shouldn't be a problem.
Here is the code for the transmitter:
//Include Libraries
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//create an RF24 object
RF24 radio(9, 8); // CE, CSN
//address through which two modules communicate.
const byte address[6] = "00001";
int led = 50;
int swit = 0;
int t = 0;
void setup()
{
radio.begin();
//set the address
radio.openWritingPipe(address);
//Set module as transmitter
radio.stopListening();
pinMode(led,OUTPUT);
t = millis();
Serial.begin(9600);
}
void loop()
{
//Send message to receiver
flash();
const char text[] = "Hello World";
radio.write(&text, sizeof(text));
t = millis();
Serial.print("Message transmitted: " + String(text)+"\n");
delay(1000);
}
void flash(){
if(swit==0){
digitalWrite(led,HIGH);
swit+=1;
swit%=2;
}else{
digitalWrite(led,LOW);
swit+=1;
swit%=2;
}
}
Here is the code for the reciever:
//Include Libraries
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//create an RF24 object
RF24 radio(9, 8); // CE, CSN
//address through which two modules communicate.
const byte address[6] = "00001";
int t = 0;
int led = 50;
int swit = 0;
void setup()
{
while (!Serial);
Serial.begin(9600);
radio.begin();
pinMode(led,OUTPUT);
//set the address
radio.openReadingPipe(0, address);
//Set module as receiver
radio.startListening();
t=millis();
}
void loop()
{
if(millis()-t>1000){
flash();
t = millis();
}
//Read the data if available in buffer
if (radio.available())
{
char text[32] = {0};
radio.read(&text, sizeof(text));
Serial.println(text);
}
}
void flash(){
if(swit==0){
digitalWrite(led,HIGH);
swit+=1;
swit%=2;
}else{
digitalWrite(led,LOW);
swit+=1;
swit%=2;
}
}