Hi
I've recently started working on a small RC project with NRF modules.
I'm using two nanos and two generic NRF24L01 modules. They are set up correctly and they are indeed communicating.
I have a master controlling a slave by sending simple chars. Every few seconds i need the slave to send back some sensor data to the master.
I'm trying to do this by sending a special character ("S"), that when the slave receives, stops listening and starts transmitting momentarily. But unfortunately that doesn't seem to work .
I'm attaching the small crude sketches. No sensor data in them, just the slave trying to send the char back to the master, and the master trying to acknowledge that.
Transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8);
byte addresses[][6] = {"1Node", "2Node"};
unsigned long prevmillis=0;
void setup()
{
pinMode(A1,INPUT_PULLUP);
pinMode(A2,INPUT_PULLUP);
pinMode(A3,INPUT_PULLUP);
pinMode(A4,INPUT_PULLUP);
Serial.begin(9600);
radio.begin();
radio.setRetries(15, 15);
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
radio.setDataRate(RF24_250KBPS);
radio.stopListening();
}
void loop()
{
int up = digitalRead(A1);
int down = digitalRead(A2);
int left = digitalRead(A3);
int right = digitalRead(A4);
if (up==LOW){
char text[]="u";
radio.write(&text, sizeof(text));
}
else if (down==LOW){
char text[]="d";
radio.write(&text, sizeof(text));
}
else if (right==LOW){
char text[]="r";
radio.write(&text, sizeof(text));
}
else if (left==LOW){
char text[]="l";
radio.write(&text, sizeof(text));
}
else{
char text[]="n";
radio.write(&text, sizeof(text));
}
unsigned long currentmillis=millis();
if (currentmillis-prevmillis >=2000){
prevmillis=currentmillis;
char text[]="S";
radio.write(&text, sizeof(text));
radio.startListening();
if(radio.available()) {
Serial.print("OK");
radio.stopListening();
}
}
}
Receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8);
byte addresses[][6] = {"1Node", "2Node"};
void setup()
{
Serial.begin(9600);
radio.begin();
radio.setRetries(15, 15);
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
radio.setDataRate(RF24_250KBPS);
radio.startListening();
}
void loop()
{
if (radio.available())
{
char text[] = {0};
radio.read(&text, sizeof(text));
Serial.println(text);
if (strcmp(text, "u") == 0) {
// Serial.println(text);
}
else if (strcmp(text, "d") == 0) {
// Serial.println(text);
}
else if (strcmp(text, "l") == 0) {
// Serial.println(text);
}
else if (strcmp(text, "r") == 0) {
// Serial.println(text);
}
else if (strcmp(text, "S") == 0) {
radio.stopListening();
radio.write(&text,sizeof(text));
radio.startListening();
// Serial.println(text);
}
else {
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
// Serial.println(text);
}
}
}