RF24 Can't get this simple sketch to work :(

I'm trying to sent an array with 2 analog values from one Arduino to another using 2 nrf24L01 modules. The hardware works, I've tried the example "getting started" sketch that comes with the RF24 library and that works. I've started from the example sketch to see how this library works but I'm not getting much result. So what am I doing wrong?
This is the transmitter sketch:

/*
RF24 test transmitter
*/

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

int joystick[2];
const uint64_t pipe = 0xE8E8F0F0E1LL;

RF24 radio(8,7);

void setup(void)
{
  Serial.begin(9600);
  radio.begin();
  radio.startListening();
  radio.setRetries(15,15);
  radio.setPayloadSize(sizeof(joystick));
}

void loop(void)
{
  joystick[0] = analogRead(A0);
  joystick[1] = analogRead(A1);
  
  radio.stopListening();
  radio.openWritingPipe(pipe);
  bool ok = radio.write( &joystick, sizeof(joystick) );
  if (ok)
        Serial.println("ok");
      else
        Serial.println("not ok");

}

The serial print gives me a "not ok". So I must be doing something wrong.

This is the receiver sketch:

/*
 RF24 test reciever
*/

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

int joystick[2];

RF24 radio(48,49);
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(void)
{
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  // optionally, increase the delay between retries & # of retries
  radio.setRetries(15,15);

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

  radio.startListening();

}

void loop(void)
{
  if ( radio.available() )
  {
    radio.read( &joystick, sizeof(joystick) );
    Serial.print("joystick[0] = ");
    Serial.println(joystick[0]);
    Serial.print("joystick[1] = ");
    Serial.println(joystick[1]);
  }
  else
  {
    Serial.println("No radio available");
  }
}

Which of course gives me "no radio available".

Go back to the working example and change one thing at a time, then you'll know where you went wrong... Or start by replacing one end of the communication with a known working piece of code (so you can work out whether its your receiving code or transmitting code (or both) that is misbehaving).

Pff, after an hour of fiddling with the code I got it to work.

/*
RF24 test transmitter
*/

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

int joystick[2];

RF24 radio(8,7);

const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(void)
{
  Serial.begin(9600);
  radio.begin();
  
  radio.openWritingPipe(pipe);
  radio.printDetails();

}

void loop(void)
{
  joystick[0] = analogRead(A0);
  joystick[1] = analogRead(A1);
 
  radio.write( joystick, sizeof(joystick) );

}

And the receiver:

/*
 RF24 test receiver
 */

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


int joystick[2];

RF24 radio(48,49);
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(void)
{
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
}

void loop(void)
{
  if ( radio.available() )
  {
    // Dump the payloads until we've gotten everything
    bool done = false;
    while (!done)
    {
      // Fetch the payload, and see if this was the last one.
      done = radio.read( joystick, sizeof(joystick) );
      Serial.println(joystick[0]);
      Serial.println(joystick[1]);
    }
  }
  else
  {
    Serial.println("No radio available");
  }
}

I'm still wrestling with this. I'm now trying to sent one array from a duemilanove to a mega and another array from the mega to the duemilanove.
Serial print gives me "no radio available" all the time on both Arduinos. Grrrrr...
Duemilanove sketch:

/*
http://www.bajdi.com
 Receive and transmit analog values
 */

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

int joystick[2];
float current[4];

RF24 radio(8,7);

const uint64_t pipes[2] = { 
  0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };


void setup(void)
{
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(pipes[0]);  
  radio.openReadingPipe(1,pipes[1]);
  radio.startListening();
}

void loop(void)
{
  joystick[0] = analogRead(A0);
  joystick[1] = analogRead(A1);

  radio.stopListening();
  radio.write( joystick, sizeof(joystick) );
  radio.startListening();

  if ( radio.available() )
  {
    // Dump the payloads until we've gotten everything
    bool done = false;
    while (!done)
    {
      // Fetch the payload, and see if this was the last one.
      done = radio.read( current, sizeof(current) );
      Serial.println(current[0]);
      Serial.println(current[1]);
      Serial.println(current[2]);
      Serial.println(current[3]);
    }
  }
  else
  {
    Serial.println("No radio available");
  }

}

Mega sketch:

/*
 http://www.bajdi.com
 Receive and transmit analog values
 */

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

int joystick[2];
float current[4];   // Array with the 4 motor current values, to sent to remote control

int apin[] = { 
  A0, A1, A2, A3 };  // analog pin array

RF24 radio(48,49);
const uint64_t pipes[2] = { 
  0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

void setup(void)
{
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1,pipes[0]);
  radio.startListening();
}

void loop(void)
{
  if ( radio.available() )
  {
    // Dump the payloads until we've gotten everything
    bool done = false;
    while (!done)
    {
      // Fetch the payload, and see if this was the last one.
      done = radio.read( joystick, sizeof(joystick) );
      Serial.println(joystick[0]);
      Serial.println(joystick[1]);
    }
  }
  else
  {
    Serial.println("No radio available");
  }
  
  radio.stopListening();
  // 4 analog readings from motor controller = motor current
  for (int i=0; i<4; i++)
  {
    current[i] = analogRead( apin[i] )/ 1023.0 * 5.0;
  }
  radio.write( current, sizeof(current) );
  radio.startListening();
}

Bajdi:
Serial print gives me "no radio available" all the time on both Arduinos. Grrrrr...

Please review the documentation for RF24::available. "Test whether there are bytes available to be read. Returns: True if there is a payload available, false if none is." Doesn't make a ton of sense to continually print "nothing available".

Anyway, Mark is exactly right. Go back to the example, which works. Change one line, put the sketch on both nodes, test. Repeat until it stops working, and then you know the one line that is problematic. As it stands, you've got way too many unnecessary changes from the example.

Got it to work.
This is one of the sketches, the other sketch just has the arrays switched.
I hope I'm doing this the right way :blush:

/*
 http://www.bajdi.com
 Receive and transmit analog values
 */

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

int joystick[2];
float current[4];   // Array with the 4 motor current values, to sent to remote control

int apin[] = { 
  A0, A1, A2, A3 };  // analog pin array

RF24 radio(48,49);
const uint64_t pipes[2] = { 
  0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

void setup(void)
{
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1,pipes[0]);
  radio.startListening();
}

void loop(void)
{
  if ( radio.available() )
  {
    // Dump the payloads until we've gotten everything
    bool done = false;
    while (!done)
    {
      // Fetch the payload, and see if this was the last one.
      done = radio.read( &joystick, sizeof(joystick) );
    }
  }

  radio.stopListening();
  
  // 4 analog readings from motor controller = motor current
  for (int i=0; i<4; i++)
  {
    current[i] = analogRead( apin[i] )/ 1023.0 * 5.0;
  }

  bool ok = radio.write( &current, sizeof(current) );

  radio.startListening();
  
  Serial.println(joystick[0]);
  Serial.println(joystick[1]);
}

As long as you got it to work, that's what counts :slight_smile: