Help with the nrf24l01 code

I finally managed to communicate two arduinos using nrf24l01 modules. Now i want to write my own code looking at the codes i used before.

TX CODE

/* TRANSMITTER CODE FOR OVERLOKMAKINESII PROTOv0,01 */


/* WARNING LED INDICATIONS */
////////////////////////////
/*
   fast blinking three times then staying on for a second = problem with nrf module
   Blinking led = problem with transmission
*/

/*OKLED indications*/
/////////////////////
/*
   fast blinking 3 times then staying on for a second = hardware is OK
   Blinking = LED blinks with every few successful payload transmission
*/

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


#define OKLED 5                    // connect a red colore LED to this pin for convenince
#define warningLED 6               // connect green LED to this pin
#define aAnalogPinX A0
#define aAnalogPinY A1
#define bAnalogPinX A2
#define bAnalogPinY A3
RF24 radio(7,8); // CE --> 7  | CSN --> 8
const uint32_t address = 0x1DA1F15LL;


struct payLoad // max 32 bytes so it doesnt exceed nrf24l01's 32bytes buffer limit
{
  byte aX;
  byte aY;
  byte bX;
  byte bY;
  //insert toggle switch command here to control two of the flight modes
  //insert toggle switch command here to control two of the flight modes
  //insert potmeter command here to trim x axis (is this a good idea?)
  //insert potmeter command here to trim y axis (this is really not a good idea) 
};

payLoad payload;


void readValuesFromJoysticks()
{
  payload.aX = map(analogRead(aAnalogPinX), 0 , 1023 , 0 , 255);
  payload.aY = map(analogRead(aAnalogPinY), 0 , 1023 , 0 , 255);
  payload.bX = map(analogRead(bAnalogPinX), 0 , 1023 , 0 , 255);
  payload.bY = map(analogRead(bAnalogPinY), 0 , 1023 , 0 , 255);
}

void transmissionSuccessful()
{
  digitalWrite(OKLED, HIGH);
  delay(1);
  digitalWrite(OKLED,LOW);
}

void transmissionFailed()
{
  digitalWrite(warningLED, HIGH);
  delay(1);
  digitalWrite(warningLED, LOW);
}

void setup()
{
  pinMode(OKLED, OUTPUT);
  pinMode(warningLED, OUTPUT);
//Serial.begin(9600); // NOTE TO SELF: Comment this after debugging and upload again!!!
  if(!radio.begin()) // check to see if radio hardware is responding.
  {
    bool isRadioAvailable = false;
    while(!isRadioAvailable)
    {
      for(int counter = 0; counter < 3; counter++)      // blink the red led to indicate there is problem with hardware
      {
        digitalWrite(warningLED, HIGH);
        delay(100);
        digitalWrite(warningLED, LOW);
        delay(100);
      }
      digitalWrite(warningLED, HIGH);
      delay(1000);
      isRadioAvailable = radio.begin();                // trying to connect to radio again
    }
  }

  
  /*///////////////////////////////////
            HARDWARE IS OK 
  ///////////////////////////////////*/
  digitalWrite(warningLED, LOW);
  for(int counter = 0; counter < 3; counter++)        // blink the green led three time to indicate hardware is ok
  {
    digitalWrite(OKLED, HIGH);
    delay(100);
    digitalWrite(OKLED, LOW);
    delay(100);
  }
  digitalWrite(OKLED, HIGH);
  delay(1000);
  digitalWrite(OKLED, LOW);


  radio.setPALevel(RF24_PA_MAX);                     // set this low if you are going to use your receiver close to the transmitter
  
  radio.setPayloadSize(sizeof(payLoad));             // save on transmission time by setting the radio to only transmit the
                                                     // number of bytes we need to transmit a payload
  radio.setDataRate(RF24_250KBPS);
  radio.openWritingPipe(address);
  radio.stopListening();                             // put radio in TX mode                                                                       
}



void loop()
{
  readValuesFromJoysticks();
  bool report = radio.write(&payload, sizeof(payLoad));
  if(report)
  {
    transmissionSuccessful();
  }
  else
  {
    transmissionFailed();
  }
}

RX CODE

/* RECEIVER CODE FOR OVERLOKMAKINESII PROTOv0.01 */

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



#define OKLED 5                    // connect a red colore LED to this pin for convenince
#define warningLED 6               // connect green LED to this pin



RF24 radio(7,8); // CE --> 7  | CSN --> 8
const uint32_t address = 0x1DA1F15LL;

struct ReceivedPayLoad // max 32 bytes so it doesnt exceed nrf24l01's 32bytes buffer limit
{
  byte aX;
  byte aY;
  byte bX;
  byte bY;
  //insert toggle switch command here to control two of the flight modes
  //insert toggle switch command here to control two of the flight modes
  //insert potmeter command here to trim x axis (is this a good idea?)
  //insert potmeter command here to trim y axis (this is really not a good idea) 
};

ReceivedPayLoad receivedPayload;


void printReceivedData()
{
  Serial.println(receivedPayload.aX);
  Serial.println(receivedPayload.aY);
  Serial.println(receivedPayload.bX);
  Serial.println(receivedPayload.bY);  
}

void setup()
{
  if(!radio.begin()) // check to see if radio hardware is responding.
  {
    bool isRadioAvailable = false;
    while(!isRadioAvailable)
    {
      for(int counter = 0; counter < 3; counter++)      // blink the red led to indicate there is problem with hardware
      {
        digitalWrite(warningLED, HIGH);
        delay(100);
        digitalWrite(warningLED, LOW);
        delay(100);
      }
      digitalWrite(warningLED, HIGH);
      delay(1000);
      isRadioAvailable = radio.begin();                // trying to connect to radio again
    }
  }

  
  /*///////////////////////////////////
            HARDWARE IS OK 
  ///////////////////////////////////*/
  digitalWrite(warningLED, LOW);
  for(int counter = 0; counter < 3; counter++)        // blink the green led three time to indicate hardware is ok
  {
    digitalWrite(OKLED, HIGH);
    delay(100);
    digitalWrite(OKLED, LOW);
    delay(100);
  }
  digitalWrite(OKLED, HIGH);
  delay(1000);
  digitalWrite(OKLED, LOW);
  Serial.begin(9600);
  radio.setPALevel(RF24_PA_MAX);               // set this low if you are going to use your receiver close to the transmitter
  radio.setDataRate(RF24_250KBPS);
  radio.openReadingPipe(1, address);
  radio.startListening();
}

void loop()
{
  if (radio.available()) 
  {
    radio.read(&receivedPayload, sizeof(ReceivedPayLoad));
    printReceivedData(); //requires Serial port to be initialized thorugh Serial.begin(9600) command          
  }
}  

two modules wont communicate while using this code. What is wrong with the code?

a long long would need to be a uint64_t. You probably would want to move to the new API with a 5 byte buffer
uint8_t adresse[] = {0x15, 0x1F, 0xD1, 0x01, 0xCC};

Tried changing it to uint64_t no luck.
about the address array: which address should i use to transmit or receive? last one?

try with uint8_t adresse[] = {0x15, 0x1F, 0xD1, 0x01, 0xCC};
the address is 5 bytes long.

still no luck

For anyone interested i solved this by deleting this line of code

good news although it should not make a difference as If this method is never called, the driver will always transmit the maximum payload size (32 bytes), no matter how much was sent to write().

I actually changed this line too and maybe this solved my problem i dont really know.

new one = radio.openReadingPipe(0, address);

more likely. (but always good to send only the actual payload rather than 32 bytes if you don't need to)

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