two way wireless communication between two arduino UNO with NRF2L01+

I have a project whereby i need to send the sensor value from one arduino to another to sound a buzzer. Then I have to stop the buzzer by pushing a button. But the problem is the value keep on sending, Do I have to transmit another signal to stop it from continuing sending the sensor value? I have read the Getting Started on two way wireless communication but I am still new on arduino. Is it a must to combine the coding rather than one code each for each arduino? If combining the code, how do we upload the code into two arduino? Someone please guide me on my project. Really thanks a lot...

https://www.youtube.com/watch?v=hI4JGDB7WtU

https://www.youtube.com/watch?v=YpasceYkimI

http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo

Check out the How To guide posted here http://yourduino.com/sunshop2/index.php?l=product_detail&p=188

Your code can send a "start" message when it sees the button pressed, and a "stop" message when it see it stopped. Or it can send "pressed" every 1/10th second or something while the button is pressed.

The receiver will need matching code, responding to the start/stop, or start up when it receives a pressed message, and when it detects that pressed is no longer being received, it stops itself. The receiver can be commanded to stop the buzzer and ignore messages when a button is pressed, until it see another start message, or until it sees that pressed has stopped coming in for some duration. Lots of options.

The NRF24L01+ transceivers provide two-way comms capabilities. If your solution needs you to pass data in both directions then you will need to use those capabilities. You don't say how you're driving the transceivers but if you're using ManiacBug's RF24 library then that comes with plenty of examples showing how to achieve that.

click the MODIFY button in the upper right of the post window. Highlight all you code. click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button. click SAVE (at the bottom). When you post code on the forum, please make a habit of using the code tags "#" button.

Sorry I am still new in posting in forum…So this is the modified code…

//Transmitter
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 7;
 
void setup(void){
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(pipe);}
 
void loop(void){
  int sensorValue = analogRead(A0);
  if (sensorValue<500){
  msg[0] = 111;
  radio.write(msg, 1);}
//Reciever
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int LED1 = 3;

void setup(void){
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
  pinMode(LED1, OUTPUT);}
 
void loop(void){
  if (radio.available()){
    bool done = false;    
    while (!done){
      done = radio.read(msg, 1);      
      Serial.println(msg[0]);
      if (msg[0] == 111){delay(10);digitalWrite(LED1, HIGH);}
      else {digitalWrite(LED1, LOW);}
      delay(10);}}
  else{Serial.println("No radio available");}}

This code basically detect the sensor value on the transmitter and send the msg to the receiver. The receiver will receive the msg and light an LED. Now I would like to modify the code so that once a button is pressed at the receiver,it will send a msg to transmitter to activate a motor and hence switch off the LED. How do I modify the code to make it to two way communication? Can someone guide me through the project?

If I was doing your project I would lay out my code like this - at the level of loop() both sets of code would be identical And, probably the receive- and send- code would be identical on both devices. The code in "actOnRadioMessage()" would obviously be different on each device.

void loop() {
   receiveRadioMessage();
   sendRadioMessage();
   actOnRadioMessage();
}

The function receiveRadioMessage() would take in the data and store is somewhere for later use by the actOnRadioMessage() function. The function sendRadioMessage() would send data that had already been prepared (probably by actOnRadioMessage())

...R

What do u mean by receive and sent code are identical on both device? and what is the function of act on message?

VincetntCool: How do I modify the code to make it to two way communication?

I see you're using Maniacbug's RF24 library. That comes with examples showing how to achieve two way communication. Look at the examples. Run them. Understand how they work. Apply the same approach to your own project.

VincetntCool: What do u mean by receive and sent code are identical on both device? and what is the function of act on message?

The purpose of the receiving and sending code is just to receive or send stuff. It doesn't need to know anything about what it is receiving or sending.

The actOnMessage() function is the bit that "knows" what to do with whatever has been received and what needs to be made ready for the reply. In one device it will note that the the sensor value has been received and do whatever must be done with that. In the other device it will act on the message to start the motor running.

You probably need another function special to each device. In one case it reads the keyboard and puts the "start Motor" message ready to be sent. In the other case it reads the sensor and puts the sensor value ready to be sent.

So the code for the two devices might look like this ... (I've changed the names of actOnRadioMessage() to better represent what they do

void loop() {
   receiveRadioMessage();
   sendRadioMessage();
   processReceivedSensorValue();
   readKeyboard();
}

void loop() {
   receiveRadioMessage();
   sendRadioMessage();
   manageMotor();
   readSensor();
}

...R

I have modified the maniacbug/RF24 / examples / GettingStarted to do my project but fail to have communication. Can someone saw any mistakes in my code?

This arduino receive a msg (111) from another side of arduino when a sensor value is detected to sound a buzzer, then when different switch is pressed, it will send different content of msg2 back to it for controlling the motor.

//For One Side of Arduino
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
int msg2[1];
RF24 radio(9,10);
const uint64_t pipe[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
int buzer = 3;
int SW1 = 5;//motor stop
int SW2 = 6;// motor clokcwise
int SW3 = 7;//motor anticlockwise
 
void setup(void)
{
  pinMode(3, OUTPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  Serial.begin(9600);
  radio.begin();
radio.openReadingPipe(1,pipe[0]);
radio.openWritingPipe(pipe[1]);
  }
  
void loop(void){
 int stopmotor =digitalRead(SW1);
int clockwise =digitalRead(SW2); 
int anticlcokwise=digitalRead(SW3);

if (clockwise==HIGH){msg2[0] = 222;
  radio.write(msg2, 1);}
else if (anticlockwise==HIGH){msg2[0] = 333;
  radio.write(msg2, 1);}
else if (stopmotor==HIGH){msg2[0] = 444;
  radio.write(msg2, 1);}
else
{
radio.startListening();
if 
(radio.available())
{
    bool done = false;    
    while (!done){
      done = radio.read(msg, 1);      
      Serial.println(msg[0]);
      if (msg[0] == 111)
{
delay(10);
digitalWrite(buzer, HIGH);
}
      else 
{
digitalWrite(buzer, LOW);
}
 delay(10);
}
}
 else
{
Serial.println("No radio available");
}
  radio.stopListening();
}
}

This arduino detects the sensor value value and send the msg (111) to another arduino and ready to accept another msg (msg2) from it to control the motor.

//Another side of arduino
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
int msg2[1];
RF24 radio(9,10);
const uint64_t pipe[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
int x = 0;
int LED1 = 3;
int LED2 = 4;

 
void setup(void)
{
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  Serial.begin(9600);
  radio.begin();
radio.openWritingPipe(pipe[0]);
radio.openReadingPipe(1,pipe[1]);
  }
 
void loop(void){
  int sensorValue = analogRead(A0);
   int maxi =digitalRead(SW1);
    int mini =digitalRead(SW2);
  
    radio.startListening();
   if (radio.available())
{
    bool done = false;    
    while (!done){
      done = radio.read(msg2, 1);      
      Serial.println(msg2[0]);
      if (msg2[0] == 222)
{
delay(10);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
}
    else if (msg2[0] == 333)
{
delay(10);
digitalWrite(LED2, HIGH);
digitalWrite(LED1, LOW);
}
    //can add more function to control motor and stop buzer
      else if	(msg2[0]==444)
{

digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
}
delay(10);
}

  	
 else if (sensorValue<500)
  {
radio.stopListening();
    msg[0] = 111;
  radio.write(msg, 1);

delay(1000);}
}

Please help me. I am almost on deadline of my project.

Did you test the radios using the library example before you starting trying to write you own program ?

Did you test the radios using the library example before you starting trying to write you own program ?

Yes I do. And I am writing accordance to it. Just that I think I cant combine both code whereby both transmit and receive for each arduino are different.

Why have you started a duplicate discussion on this issue? I have been responding to your other Thread here.

...R

Threads merged.

Why have you started a duplicate discussion on this issue? I have been responding to your other Thread here.

...R

So, that is what i have done by modifying the previous code. But I still cant make it a two way communication. Anyone can help please?

I don't have the same wireless transceivers as you so I can't test your code. I have two-way transmission with other 2.4Ghz transceivers.

I am finding it hard to understand what you don't understand.

As far as I can see you know how to get Arduino A to send a message to Arduino B. Getting B to send a message to A will be identical - the only difference will be the content of the message.

It may be easier if you designate one of the Arduinos to be the master - it doesn't matter which one. Then that one can control the process. It can send a message to Arduino B (the slave) and then listen for a reply. You can have a variable that keeps track of whether you are sending or listening. The slave will spend most of its time listening and will only reply after it receives a message.

I suspect that your problem is because you have not thought out the detailed sequence of events in English (or your native language) before thinking about it in computer code.

If you can explain what you don't understand I will try to help more.

...R

VincetntCool:
Yes I do. And I am writing accordance to it. Just that I think I cant combine both code whereby both transmit and receive for each arduino are different.

The RF24 library comes with examples showing how to send messages in both directions. Your own code makes no attempt to do that. The examples show you exactly how to do it. I don’t understand why you don’t simply follow the examples - that’s what they are there for. If your question is: “How do I do it?” then the example is there specifically to show you how. What’s the problem?

hi i want to use 2 nrf for communication and 2 arduino uno. one arduino is connected with pir sensor and nrf and other arduino is with buzzer and nrf. if pir sensor sense any motion send message to other arduino through nrf and buzzer will operate. please help me.

This Simple nRF24L01+ Tutorial should get you started.

...R