4

I'm attempting to transmit very simple data across some NRF24l01 modules for testing purposes, and I've been following a YouTube guide.

Here is my code:

Trasmitter:

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

RF24 Radio (7, 8);
byte address[][6] = {"0"};
struct package{
   int on = 1;
};

typedef struct package Package;
Package data;

void setup() {
  Serial.begin(9600);
  delay(1000);
  Radio.begin();
  Radio.setChannel(118);
  Radio.setPALevel(RF24_PA_LOW);
  Radio.setDataRate(RF24_250KBPS);
  Radio.openWritingPipe(address[0]);
  delay(1000);
}

void loop() {
  Radio.write(&data, sizeof(data));

  Serial.println("Data sent:");
  Serial.println(data.on);
  delay(1000);
  }
}

Receiver:

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

 RF24 Radio (7, 8);
 byte address[][6] = {"0"};
 struct package{
    int on = 1;
 };

 typedef struct package Package;
 Package data;

 void setup() {
   Serial.begin(9600);
   pinMode(2, OUTPUT);
   delay(1000);
   Radio.begin();
   Radio.setChannel(118);
   Radio.setPALevel(RF24_PA_LOW);
   Radio.setDataRate(RF24_250KBPS);
   Radio.openReadingPipe(1, address[0]);
   Radio.startListening();
   delay(1000);
 }

 void loop() {
   while(Radio.available()){
     Radio.read(&data, sizeof(data));
     if(data.on == 1){
       digitalWrite(2, HIGH);
     }
     else if(data.on == 0){
       digitalWrite(2, HIGH);
     }
     else{
       digitalWrite(2, LOW);
     }
     Serial.println(data.on);
   }
   while(!Radio.available()){
     Serial.println("Error");
   }
   delay(1000);
 }

When I run the code on each respective unit, I receive only zeros on the receiver. Since I don't see where data.on could get set to zero in my code, I'm assuming there is a communication issue somehow between the two modules, even though Radio.available() is functioning properly.

Am I missing something obvious?

Wilson
  • 41
  • 3
  • I would try it without the `struct`s. – Gerben May 06 '17 at 18:27
  • Are they both Unos? Could be a packing issue if one is a 32-bit MCU. You realize pin 2 is set whether data.on is 1 or 0? – SoreDakeNoKoto May 06 '17 at 22:48
  • @TisteAndii, Im transmitting with an orginal Uno and receiving with a knock-off Mega2560. Is that a cause for concern? Also, pin 2 is just an led that I was using for debugging. I am reading the serial port for the actual value. – Wilson May 08 '17 at 10:43
  • Well, they're both 8-bit CPUs. So packing shouldn't be an issue. Still print the values of 'sizeof(data)' for each MCU to confirm they're the same. Also I'd advise that you add more characters to your address...remember reading the module has problems with 2 bytes or less. – SoreDakeNoKoto May 08 '17 at 11:01
  • @TisteAndii, sizeof(data) is the same on both ends, and changing the address to a larger number doesn't resolve the issue either. Another interesting note, Radia.available is returning true even when the sending module is not powered on... Sorry for the delayed response, busy life – Wilson Jun 07 '17 at 23:44
  • What new address did you use? Use 3 bytes at least. – SoreDakeNoKoto Jun 08 '17 at 03:48
  • @TisteAndii, I used 888. I would assume that would be 3 bytes, but I don't actually know how you determine byte length of a number. – Wilson Jun 09 '17 at 10:35
  • @Wilson The amt of memory the number occupies depends on its type. It should take up at least 2 bytes assuming you assigned it to an integer type variable. Update your post to show how you used the new address. Or merely use "uint64_t address = 0xdeadbeef" so you get a 4-byte address. – SoreDakeNoKoto Jun 09 '17 at 11:47
  • 1
    @TisteAndii, I figured it out! thank you so much for your help, the issue turned out to be that the SPI pins on the MEGA are assigned differently than the UNO, and the code was actually working the whole time. I cant thank you enough for helping me, even if it turned out to be in vain. – Wilson Jul 07 '17 at 16:30
  • @Wilson please add an answer stating how you fixed this and accept it: this way this is not anymore reported as an "unanswered question" – Roberto Lo Giacco Sep 18 '17 at 14:09

1 Answers1

2

The OP wrote:

The issue turned out to be that the SPI pins on the MEGA are assigned differently than the UNO, and the code was actually working the whole time. I cant thank you enough for helping me, even if it turned out to be in vain.