This is my Master's code.
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
RF24 radio(7, 8);
const byte address[6] = "00001";
char text[20] = "";
int flag;
char *p;
int i;
void print();
void read();
void rfwrite();
void setup() {
Serial.begin(9600);
radio.begin();
radio.setDataRate(RF24_1MBPS);
radio.setChannel(1);
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
flag = 0;
p = text;
}
void loop() {
if (Serial.available()) {
read();
}
if (flag) {
print();
rfwrite();
}
}
void print() {
Serial.println(text);
flag = 0;
}
void read() {
p = &text[0];
for (i = 0; Serial.available(); p++, i++) {
*p = Serial.read();
}
text[i + 1] = '\0';
flag = 1;
}
void rfwrite() {
radio.startWrite(text, strlen(text)+1);
}
Slave code:
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
RF24 radio(7, 8); //ce,csn
const byte address[6] = "00001";
char text[20] = "";
int i;
void write_to_array();
void print();
void setup() {
Serial.begin(9600);
radio.begin();
radio.setDataRate(RF24_1MBPS);
radio.setChannel(1);
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
while (radio.available()) {
write_to_array();
print();
}
}
void write_to_array() {
radio.read(text, strlen(text)+1);
}
void print() {
Serial.println(text);
}
I edited the code and made it with an array like @Kwasmich said. But now some end characters are not getting printed. When I type 1234567890-=
into the transmitter, only 1234567890-
gets printed. When I type a new set of characters following that, the missing =
gets printed. Also, this happens only on the second transmission after I upload the code to the Arduino. The first set of characters are received and printed correctly. How do I fix this? Will flushing the buffer help(I doubt this because the first transmission is received correctly)?
char text = ""
can only hold a single character, not a string. You either need a char array or a char pointer. Likewisesizeof(text)
is 1 because this is always a single character.