What's different here from the previous example is that we need to create two pipes or
addresses for the bi-directional communication.
1.const byte addresses[][6] = {"00001", "00002"};
In the setup section we need to define both pipes, and note that the writing address at the
first Arduino needs to be the reading address at the second Arduino, and vice versa, the
reading address at the first Arduino needs to be the writing address at the second Arduino.
1.// at the Transmitter
2.radio.openWritingPipe(addresses[1]); // 00001
3.radio.openReadingPipe(1, addresses[0]); // 00002
1.// at the Receiver
2.radio.openWritingPipe(addresses[0]); // 00002
3.radio.openReadingPipe(1, addresses[1]); // 00001
In the loop section using the radio.stopListening() function we set the first Arduino as
transmitter, read and map the value of Joystick from 0 to 180, and using the radio.write()
function send the data to the receiver.
1.radio.stopListening();
2.int potValue = analogRead(A0);
3.int angleValue = map(potValue, 0, 1023, 0, 180);
4.radio.write(&angleValue, sizeof(angleValue));
On the other side, using the radio.startListening() function we set the second Arduino as
receiver and we check whether there is available data. While there is data available we will
read it, save it to the "angleV" variable and then use that value to rotate the servo motor.
1.radio.startListening();
2.if ( radio.available()) {
3.while (radio.available()) {
4.int angleV = 0;
5.radio.read(&angleV, sizeof(angleV));
6.myServo.write(angleV);
7.}
Next, at the transmitter, we set the first Arduino as receiver and with an empty "while" loop
we wait for the second Arduino the send data, and that's the data for the state of the push
button whether is pressed or not. If the button is pressed the LED will light up. So these
process constantly repeats and both Arduino boards are constantly sending and receiving data.
Source : http://howtomechatronics.com/tutorials/arduino/arduino-
wireless-communication-nrf24l01-tutorial/
Need help?
Do you have a question about the NRF24L01 and is the answer not in the manual?