Wireless Communication via NRF24L01

Hey I'm using 2 transceivers (NRF24L01) to communicate between my 2 Arduinos - I want the Transmitter to send the signal to the Receiver (piezo beeping and the vibration motors to vibrate) when I push the standard push button. I also have an LED which i wish to flick on an off on the transmitter when the button is pushed.

Would someone be able to go over my code and tell me where I have gone wrong? :astonished:

Transmitter

    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    #define led 6
    RF24 radio(7, 8); // CNS, CE
    const byte addresses[][6] = {"00001", "00002"};
    boolean buttonState = 0;
    //
    int buttonPin = 1;
    int ledPin = 6;
    //
    void setup() 
    {
      pinMode(12, OUTPUT);
      radio.begin();
      radio.openWritingPipe(addresses[1]); // 00001
      radio.openReadingPipe(1, addresses[0]); // 00002
      radio.setPALevel(RF24_PA_MIN);
      //
      pinMode(ledPin, OUTPUT);
      pinMode(buttonPin, INPUT); 
      pinMode(8, OUTPUT);
    }
    void loop()
      {
   //   radio.stopListening();
   //   radio.startListening();
   //   while (!radio.available());
      radio.read(&buttonState, sizeof(buttonState));
      if (buttonState == HIGH) 
        {
        digitalWrite(ledPin, LOW);
        }
      }

Reciever

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define button 1
RF24 radio(7, 8); // CNS, CE
const byte addresses[][6] = {"00001", "00002"};
const int motorPin = 10;

void setup() {
  pinMode(button, INPUT);
  radio.begin();
  radio.openWritingPipe(addresses[0]); // 00002
  radio.openReadingPipe(1, addresses[1]); // 00001
  radio.setPALevel(RF24_PA_MIN);
  pinMode(motorPin,OUTPUT);
}
void loop() 
{
  delay(5);
  radio.startListening();
  if ( radio.available()) 
  {
    while (radio.available()) 
   {
digitalWrite(motorPin, HIGH);
   }
    delay(5);
    radio.stopListening();
  }
}

I have not yet included the Piezo in the code as I wanted to work on 1 section at a time

Why is your transmitter using radio.read()

Have a look at this Simple nRF24L01+ Tutorial

...R

Thank you for this but I am still a bit unsure of what I should be doing with my code to send the signal. I understand how to communicate between and send text but not other signals. Sorry I'm a designer and have never studied anything relating to this

I am still a bit unsure of what I should be doing with my code to send the signal. I understand how to communicate between and send text but not other signals

One way to proceed is to define and type the data you want to send and receive. Transmitted data can be text string(a null terminated array of chars), a single byte or char, an int, a float, an array of any data type, or a struct with mixed types. It sounds like you want to send commands to turn a buzzer and a vibration motor on and off, and perhaps information about the intensity and duration.

However, you appear to be looking at it in a different way and are trying to code as if the button were on the the receiver, and the receiver code knows what to do when the button becomes pressed. In that case it is possible that you could simply send a 0 or 1 and let the receiver code treat the received data as if it were a button state.

So, to begin, you must specify what information you want to send after a button press is detected on the sending Arduino. It could be as simple as a 0 or 1 to represent a button state, or, for example with multiple vibrators as complicated as vibration motor id, vibration intensity, vibration duration.

If you want to send multi byte data(for example, a float is 4 bytes) the data must be broken down into its component bytes to send and receive.The generic form to send and receive a packet of bytes created from your data is

 radio.write((uint8_t*)&dataToSend, sizeof(dataToSend));
 radio.read((uint8_t*)&dataReceived, sizeof(dataReceived));

You should also study the state change example in the ide. You need to trigger your transmission when a button state transitions from not pressed to pressed.

THiSiSVANiTY:
Thank you for this but I am still a bit unsure of what I should be doing with my code to send the signal.

Take account of what @cattledog has said and apply it to one of my examples - perhaps the first example. If it does not work then post the code that represents your best attempt and also include in your post an example of the sort of data you want to send.

...R