Hey everyone! I have a question regarding the nrf24 transceiver module. I am trying to setup a two way communication system using ACK payloads between two modules. The Primary TX reads an anlaog value from pin 7 and transmits it to the primary receiver. The primary receiver then also reads the analog value from pin 7 and transmits it’s value back to the transmitter. I have tried using th e library’s examples, but they are quite complicated and have a lot of code which I don’t need for my purpose. Only problem is that The values aren’t sending or being received properly. It would be great if someone could see if it is obvious what I am doing wrong. Thanks!
Primary Transmitter code
#include <SPI.h>
#include "RF24.h";
RF24 radio(7,8);
struct package
{
int remotePotSlider = 0;
int boardBatVoltage = 0;
};
byte adresses[][6] ={"0"};
typedef struct package Package;
Package data;
void setup() {
Serial.begin(115200);
radio.begin();
radio.setRetries(15, 15);
radio.setChannel(115);
radio.setPALevel(RF24_PA_LOW);
radio.setDataRate(RF24_250KBPS);
radio.enableAckPayload();
radio.enableDynamicPayloads();
radio.openWritingPipe(adresses[1]);
radio.openReadingPipe(1,adresses[0]);
delay(1000);
}
void loop() {
data.remotePotSlider = analogRead(7);
delay(20);
if(radio.write(&data, sizeof(data)))
{
Serial.println(" transmitted successfully");
if(!radio.available()){ // If nothing in the buffer, we got an ack but it is blank
Serial.println("Got blank response");
}
else{
while(radio.available() ){ // If an ack with payload was received
radio.read(&data, sizeof(data));
Serial.print("Ack payload is: ");
Serial.print(data.boardBatVoltage);
}
}
}
else {
Serial.println("Failed Sending");
}
delay(50);
}
Primary Receiver code
#include <SPI.h>
#include "RF24.h";
RF24 radio(7,8);
struct package
{
int remotePotSlider = 0;
int boardBatVoltage = 0;
};
byte adresses[][6] ={"0"};
typedef struct package Package;
Package data;
void setup() {
Serial.begin(15200);
radio.begin();
radio.setRetries(15, 15);
radio.setChannel(115);
radio.setPALevel(RF24_PA_LOW);
radio.setDataRate(RF24_250KBPS);
radio.enableAckPayload();
radio.enableDynamicPayloads();
radio.openWritingPipe(adresses[0]);
radio.openReadingPipe(1,adresses[1]);
radio.startListening();
delay(1000);
}
void loop() {
data.boardBatVoltage = analogRead(7);
delay(20);
while( radio.available()){
radio.read(&data,sizeof(data));
Serial.println("Received potslider:");
Serial.print(data.remotePotSlider);
radio.writeAckPayload(1,&data,sizeof(data));
}
delay(50);
}