2

I am using RF24 modules with my two Arduinos Leonardo with the RF24 library. My goal is to be able to press a button on the first Arduino which will light up an LED on the second Arduino, and be able to press a button on the second Arduino to light up an LED on the first Arduino. I was easily able to figure this out as a one-way transmission with the first Arduino only transmitting and the second only receiving, but I am unable to make it a two-way transmission, where each Arduino can transmit and receive at the same time.

This is my code so far:

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

int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
const uint64_t pipe2 = 0xF0F0F0F0AA;
void setup(void){
  Serial.begin(57600);
  pinMode(A0, INPUT);
  radio.begin();
  radio.openWritingPipe(pipe);
  radio.openReadingPipe(1,pipe2);
}

void loop(void){
  int charToSend[1];
  charToSend[0] = digitalRead(A0);
  if (!radio.write(charToSend,1)) {Serial.println('didn\'t send');}
  radio.startListening();
  if (radio.available()){
    bool done = false;  
    done = radio.read(msg, 1); 
    Serial.println(msg[0]);
    digitalWrite(13, msg[0]);
  }
  radio.stopListening();
  radio.powerDown(); 
  delay(10);
  radio.powerUp();
}

But the radio.write() is returning false and therefore not sending.

  • Please update your library to github.com/TMRh20/RF24. That way you'll get a faster and more stable library. – Avamander Jan 2 '16 at 9:54
3

The main thing that I can see wrong with your sketch is that the logic of your methodology is backwards.

You are spending most of your time either with the radio asleep, or not in listening mode, so it can not receive anything.

Instead you need to spend all your time in listening mode waiting for messages to arrive, and only when the button changes state do you want to switch off listening mode and send a packet to the other end. As soon as you have done that you then go back to listening.

You can't listen while the radio is powered down, so using powerDown() and powerUp() is just nonsense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.