Reading RF24 signal with new library

Hi guys,

i m having a little problem using this sketch with new library for nrf24l01!!!

this is the code that i use and works fine

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

const int chipSelectNotPin = 53;
const int chipEnablePin = 9;
const int addressSize = 2;
const int packetLength = 28 - addressSize;
const int sensorPin =  2;      // the number of the LED pin

int pulseState = LOW;          // pulseState used to set off the sensor
long previousMillis = 0;       // will store last time pulse signal was changed

long pulseIntervalOn = 1;
long pulseIntervalOff = 1000;

void setup(){
  Serial.begin(115200);

  // initalize the  data ready and chip select pins:
  pinMode(chipEnablePin, OUTPUT);
  pinMode(chipSelectNotPin, OUTPUT);  
  pinMode(sensorPin, OUTPUT);

  // start the SPI library:
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_2XCLOCK_MASK);
  SPI.begin();

  Serial.println("Configuring nRF...");
  // 1Mbps
  byte lv_rf_setup = 0x0;
  write_reg(RF_SETUP,&lv_rf_setup,1);
  
  //Freq channel
  byte lv_rf_ch = 73;
  write_reg(RF_CH, &lv_rf_ch, 1);
  
  //set address length
  byte lv_setup_aw = 0b00;
  switch (addressSize){
    case 2:
      lv_setup_aw = 0b00;
      break;
    case 3:
      lv_setup_aw = 0b01;
      break;
    case 4:
      lv_setup_aw = 0b10;
      break;
    case 5:
      lv_setup_aw = 0b11;
      break;
  }
  write_reg(SETUP_AW, &lv_setup_aw, 1);
  
  //Disable autoretransmit
  //byte lv_setup_retr = 0x0;
 // write_reg(SETUP_RETR, &lv_setup_retr, 1);
  
  //Disable Enchanced ShockBurst
  byte lv_en_aa = 0x0;
  write_reg(EN_AA, &lv_en_aa, 1);
  
  //Disable Pipe 1;
 // byte lv_rx_pw_p1 = 0x0;
  //write_reg(RX_PW_P1, &lv_rx_pw_p1, 1);
  
  //Set data length in pipe 0;
  byte lv_rx_pw_p0 = packetLength;
  write_reg(RX_PW_P0, &lv_rx_pw_p0, 1);
  
  //Set address
  byte lv_addr[5];
  lv_addr[1] = 0xC6;
  lv_addr[0] = 0xA1;
//  lv_addr[4] = 0xC6;
//  lv_addr[3] = 0xA1;
//  lv_addr[2] = 0x01;
//  lv_addr[1] = 0xF8;
//  lv_addr[0] = 0x11;
  write_reg(RX_ADDR_P0, &lv_addr[0], addressSize);
  
  //Flush RX buffer
  flush_rx();
  
  //Enable 16bit CRC and power up in RX mode
  byte lv_config = (1<<PWR_UP) | (1<<PRIM_RX) | (1<<EN_CRC) | (1<<CRCO);
  write_reg(CONFIG, &lv_config, 1);

  delay(50);
  
  //Enable nRF
  CE_HIGH();
  delay(10);

}

void loop(){
  byte nrf_status = get_nfr_status();
  if ((nrf_status & (1<<RX_DR)) != 0) {
    write_reg(STATUS, &nrf_status, (1<<RX_DR));

    //1. Read payload
    byte ff_status = read_reg(FIFO_STATUS);
    while (~ff_status & (1<<RX_EMPTY)){
      byte buf[packetLength];
      memset(buf,0,sizeof(buf)); 
      
      read_rx((byte*)(&buf),packetLength);
      
      //8th byte - current HR
      Serial.print("HR:"); 
      if (buf[5] < 100) Serial.print(" ");
      Serial.print(buf[7-addressSize], DEC);

      // Last byte - Avarage HR
      Serial.print("  AvgHR:"); 
      if (buf[packetLength-1] < 100) Serial.print(" ");
      Serial.print(buf[packetLength-1], DEC);
      
      //Raw packet
      Serial.print("    Raw data: "); 
      
      for (int i=0; i<packetLength; i++){
        if (buf[i] < 0x10) Serial.print("0");  
        Serial.print(buf[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
      
      ff_status = read_reg(FIFO_STATUS);
    }
  }

  //Send pulse to trigger sensor 
  unsigned long currentMillis = millis();   
  if ((pulseState == LOW) && (currentMillis - previousMillis > pulseIntervalOff)) {
    previousMillis = currentMillis;   
    pulseState = HIGH;
    digitalWrite(sensorPin, pulseState);
  }
  if ((pulseState == HIGH) && (currentMillis - previousMillis > pulseIntervalOn)) {
    previousMillis = currentMillis;
    pulseState = LOW;
    digitalWrite(sensorPin, pulseState);
  }
}

byte read_reg(byte reg){
  byte lv_reg;
  CSN_LOW();
  SPI.transfer(R_REGISTER | (REGISTER_MASK & reg));
  lv_reg = SPI.transfer(NOP);
  CSN_HIGH(); 
  return lv_reg;
}

void write_reg(byte reg, byte* value, byte len){
  CSN_LOW();
  SPI.transfer(W_REGISTER | (REGISTER_MASK & reg));
  for (int i = 0; i < len; i++){
    SPI.transfer(value[i]);
  }
  CSN_HIGH(); 
}

byte get_nfr_status(){
  byte lv_status = 0x0;
  CSN_LOW();
  lv_status = SPI.transfer(NOP);
  CSN_HIGH();  
  return lv_status;
}

void CE_HIGH(){
  digitalWrite(chipEnablePin, HIGH);
}

void CE_LOW(){
  digitalWrite(chipEnablePin, LOW);
}

void CSN_HIGH(){
  digitalWrite(chipSelectNotPin, HIGH);
}

void CSN_LOW(){
  digitalWrite(chipSelectNotPin, LOW);
}


void read_rx(byte *rx_buf, int rx_size){
  CSN_LOW();
  SPI.transfer(R_RX_PAYLOAD);
  for (int i=0; i<rx_size; i++){
    rx_buf[i] = SPI.transfer(NOP);
  }
  CSN_HIGH(); 
}

void flush_rx(){
  CSN_LOW();
  SPI.transfer(FLUSH_RX);
  CSN_HIGH(); 
}

but i would like to integrate this sketch to my program that reads from other sensor using pipes

i first tryed something like this but sometimes works and sometimes not and i can t understand why

anyone can help me?

/*
 Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 version 2 as published by the Free Software Foundation.
 */

/**
 * Example for Getting Started with nRF24L01+ radios. 
 *
 * This is an example of how to use the RF24 class.  Write this sketch to two 
 * different nodes.  Put one of the nodes into 'transmit' mode by connecting 
 * with the serial monitor and sending a 'T'.  The ping node sends the current 
 * time to the pong node, which responds by sending the value back.  The ping 
 * node can then see how long the whole cycle took.
 */

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

//
// Hardware configuration
//

// Set up nRF24L01 radio on SPI bus plus pins 9 & 10 

RF24 radio(9,53);
const int chipSelectNotPin = 53;
//add this for compatibility to Teensy3 and printf
#if defined(__arm__) && defined(CORE_TEENSY)
#define printf radio.kprintf
#endif
//end

//
// Topology
//

// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xC6A1, 0xF0F0F0F0D2LL };

//
// Role management
//
// Set up role.  This sketch uses the same software for all the nodes
// in this system.  Doing so greatly simplifies testing.  
//

// The various roles supported by this sketch
typedef enum { role_ping_out = 1, role_pong_back } role_e;

// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};

// The role of the current running sketch
role_e role = role_pong_back;
const int addressSize = 2;
const int packetLength = 28 - addressSize;

void setup(void)
{
  //
  // Print preamble
  //

  Serial.begin(115200);
  printf_begin();
  printf("\n\rRF24/examples/GettingStarted/\n\r");
  printf("ROLE: %s\n\r",role_friendly_name[role]);
  printf("*** PRESS 'T' to begin transmitting to the other node\n\r");
  pinMode(chipSelectNotPin, OUTPUT);  

  //
  // Setup and configure rf radio
  //

  radio.begin();

  
  radio.setChannel(73);
  // optionally, increase the delay between retries & # of retries
  radio.setRetries(15,15);

  // optionally, reduce the payload size.  seems to
  // improve reliability
  //radio.setPayloadSize(8);

  //
  // Open pipes to other nodes for communication
  //

  // This simple sketch opens two pipes for these two nodes to communicate
  // back and forth.
  // Open 'our' pipe for writing
  // Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)

  //if ( role == role_ping_out )
  {
    //radio.openWritingPipe(pipes[0]);
    radio.openReadingPipe(1,pipes[1]);
  }
  //else
  {
    //radio.openWritingPipe(pipes[1]);
    //radio.openReadingPipe(1,pipes[0]);
  }

  //
  // Start listening
  //

  radio.startListening();

  //
  // Dump the configuration of the rf unit for debugging
  //

  radio.printDetails();
}

void loop(void)
{
  //
  // Ping out role.  Repeatedly send the current time
  //

  if (role == role_ping_out)
  {
    // First, stop listening so we can talk.
    radio.stopListening();

    // Take the time, and send it.  This will block until complete
    unsigned long time = millis();
    printf("Now sending %lu...",time);
    bool ok = radio.write( &time, sizeof(unsigned long) );
    
    if (ok)
      printf("ok...");
    else
      printf("failed.\n\r");

    // Now, continue listening
    radio.startListening();

    // Wait here until we get a response, or timeout (250ms)
    unsigned long started_waiting_at = millis();
    bool timeout = false;
    while ( ! radio.available() && ! timeout )
      if (millis() - started_waiting_at > 200 )
        timeout = true;

    // Describe the results
    if ( timeout )
    {
      printf("Failed, response timed out.\n\r");
    }
    else
    {
      // Grab the response, compare, and send to debugging spew
      unsigned long got_time;
      radio.read( &got_time, sizeof(unsigned long) );

      // Spew it
      printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
    }

    // Try again 1s later
    delay(1000);
  }

  //
  // Pong back role.  Receive each packet, dump it out, and send it back
  //

  if ( role == role_pong_back )
  {
    // if there is data ready
    if ( radio.available() )
    {
      // Dump the payloads until we've gotten everything
      unsigned long got_time;
      bool done = false;
      while (!done)
      {
        // Fetch the payload, and see if this was the last one.
        done = radio.read( &got_time, sizeof(unsigned long) );

        // Spew it
        printf("Got payload %lu...",got_time);
 //Raw packet
       byte buf[packetLength];
       memset(buf,0,sizeof(buf)); 
      //8th byte - current HR
     read_rx((byte*)(&buf),packetLength);

      // Last byte - Avarage HR
      Serial.print("  AvgHR:"); 
      if (buf[packetLength-1] < 100) Serial.print(" ");
      Serial.print(buf[packetLength-1], DEC);
      	// Delay just a little bit to let the other unit
	// make the transition to receiver
	delay(20);
      }

      // First, stop listening so we can talk
      radio.stopListening();

      // Send the final one back.
      radio.write( &got_time, sizeof(unsigned long) );
      printf("Sent response.\n\r");

      // Now, resume listening so we catch the next packets.
      radio.startListening();
    }
  }

  //
  // Change roles
  //

  if ( Serial.available() )
  {
    char c = toupper(Serial.read());
    if ( c == 'T' && role == role_pong_back )
    {
      printf("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n\r");

      // Become the primary transmitter (ping out)
      role = role_ping_out;
      radio.openWritingPipe(pipes[0]);
      radio.openReadingPipe(1,pipes[1]);
    }
    else if ( c == 'R' && role == role_ping_out )
    {
      printf("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r");
      
      // Become the primary receiver (pong back)
      role = role_pong_back;
      radio.openWritingPipe(pipes[1]);
      radio.openReadingPipe(1,pipes[0]);
    }
  }
}
void read_rx(byte *rx_buf, int rx_size){
  CSN_LOW();

  SPI.transfer(R_RX_PAYLOAD);
  for (int i=0; i<rx_size; i++){
    rx_buf[i] = SPI.transfer(NOP);
  }
    CSN_HIGH(); 

}
void CSN_HIGH(){
  digitalWrite(chipSelectNotPin, HIGH);
}

void CSN_LOW(){
  digitalWrite(chipSelectNotPin, LOW);
}

// vim:cin:ai:sts=2 sw=2 ft=cpp

one more info I'm using the code that this guy made to check heart rate from Polar sensor

I tryed to make it more simple than possible and I'm at this point now

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
RF24 radio(9,53);
const uint64_t pipes[3] = { 0xF0F0F0C6A1LL, 0xF0F0F00C6A2LL, 0xF0F0F00C6A3LL };

const int chipSelectNotPin = 53;
const int chipEnablePin = 9;
const int addressSize = 2;
const int packetLength = 28 - addressSize;

void setup(){
  Serial.begin(115200);
  printf_begin();
  // initalize the  data ready and chip select pins:
  pinMode(chipEnablePin, OUTPUT);
  pinMode(chipSelectNotPin, OUTPUT);  

  // start the SPI library:
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_2XCLOCK_MASK);
  SPI.begin();
  radio.begin();
    
  radio.setDataRate(RF24_1MBPS);
  radio.setPALevel(RF24_PA_MIN);
  radio.setChannel(73);
  radio.openWritingPipe(pipes[2]);
  radio.openReadingPipe(0,pipes[0]);
  radio.openReadingPipe(1,pipes[1]);
  radio.startListening();

  //set address length
  byte lv_setup_aw = 0b00;
  switch (addressSize){
    case 2:
      lv_setup_aw = 0b00;
      break;
    case 3:
      lv_setup_aw = 0b01;
      break;
    case 4:
      lv_setup_aw = 0b10;
      break;
    case 5:
      lv_setup_aw = 0b11;
      break;
  }
  write_reg(SETUP_AW, &lv_setup_aw, 1);
  

  //Disable Enchanced ShockBurst
  byte lv_en_aa = 0x0;
  write_reg(EN_AA, &lv_en_aa, 1);
  
  //Set data length in pipe 0;
  byte lv_rx_pw_p0 = packetLength;
  write_reg(RX_PW_P0, &lv_rx_pw_p0, 1);
  
    radio.printDetails();

}

void loop(){
  
    uint8_t len = 0;
    uint8_t pipe = 0;
    // Loop thru the pipes 0 to 5 and check for payloads    
    if ( radio.available( &pipe ) ) {
      bool done = false;
      while (!done)
      {
      Serial.println("  Radio On"); 
      byte buf[packetLength];
      memset(buf,0,sizeof(buf)); 
      read_rx((byte*)(&buf),packetLength);

      Serial.print(buf[packetLength-1], DEC);

  }        
        
        radio.startListening();
        
        // Increase pipe and reset to 0 if more than 5
        pipe++;
        if ( pipe > 5 ) pipe = 0;
      }

    
  


}

byte read_reg(byte reg){
  byte lv_reg;
  CSN_LOW();
  SPI.transfer(R_REGISTER | (REGISTER_MASK & reg));
  lv_reg = SPI.transfer(NOP);
  CSN_HIGH(); 
  return lv_reg;
}

void write_reg(byte reg, byte* value, byte len){
  CSN_LOW();
  SPI.transfer(W_REGISTER | (REGISTER_MASK & reg));
  for (int i = 0; i < len; i++){
    SPI.transfer(value[i]);
  }
  CSN_HIGH(); 
}



void CE_HIGH(){
  digitalWrite(chipEnablePin, HIGH);
}

void CE_LOW(){
  digitalWrite(chipEnablePin, LOW);
}

void CSN_HIGH(){
  digitalWrite(chipSelectNotPin, HIGH);
}

void CSN_LOW(){
  digitalWrite(chipSelectNotPin, LOW);
}


void read_rx(byte *rx_buf, int rx_size){
  CSN_LOW();
  SPI.transfer(R_RX_PAYLOAD);
  for (int i=0; i<rx_size; i++){
    rx_buf[i] = SPI.transfer(NOP);
  }
  CSN_HIGH(); 
}