Using nRF24L01 modules to send data only at certain times

Hello. I am trying to use nRF24L01 modules to send data from an arduino uno to an arduino nano, and get the nano to start carrying out a function. To do this, I am trying to say that if a variable equals a certain value, then the board should write something to the other board, but if it is not that value, then it should not send anything. However, I am having problems. I am trying to make it so that data is only sent once by changing the variable, but it is being printed on the receiver's serial monitor over and over. Does anybody know what might be wrong with my code?

Transmitter:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8); // CE, CSN

const byte address[6] = "00001";
int num = 1;
int num2 = 2;

void setup() {
  radio.begin();
  Serial.begin(9600);
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

void loop() {
  writing();
  delay(1000);
}

void writing() {
  if (num=1) {
    radio.write(&num2, sizeof(num2));
    num+=1;
  }
  else {
    return;
  }
}


Receiver:


#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8); // CE, CSN

const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    int num = "";
    radio.read(&num, sizeof(num));
    if (num=1) {
      Serial.println("Function");
    }
    else {
      return;
    }
  }
}


Thanks in advance

if (num==1)

You have made this error in RX and TX

1 Like

Hi. This doesn't seem to be working. Now nothing is being printed to the monitor. Here is the new code for both the receiver and transmitter.

Transmitter:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8); // CE, CSN

const byte address[6] = "00001";
int num = 1;
int num2 = 2;

void setup() {
  radio.begin();
  Serial.begin(9600);
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

void loop() {
  writing();
  delay(1000);
}

void writing() {
  if (num==1) {
    radio.write(&num2, sizeof(num2));
    num+=1;
  }
  else {
    return;
  }
}


Receiver:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8); // CE, CSN

const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    int num = "";
    radio.read(&num, sizeof(num));
    if (num==2) {
      Serial.println("Function");
    }
    else {
      return;
    }
  }
}


I just realized that in the receiver I should have made num a string, not an integer. I changed int num = ""; to char num ="";, and now the code is working. Thanks for your answer, I think that was also part of the problem.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.