code for stable connection - nrf24l01 with antenna

for my wireless car and transmitter i bought 2 nrf24l01 modules ... but i have already tried one and the other and i am not getting a stable connection between the mega on the car and the nano in the transmitter, does anyone have a code for a stable connection.
I need to be able to forward multiple values ...
6 potentiometer values and 5 switch values

thank you in advance,
IsaaK

(deleted)

For someone with 4 years experience, you ought to be used to supplying helpful information.

So we are down to guessing what the problem your having actually is, I will guess your problem is due to the transmitter and receiver being too far apart and you need to use a different, longer range module.

For multiple datas sending you can use struct with multiple datas that are sent as one packet to receiver :slight_smile: I think for faster seding you can also delete that delays from code: delay(10 * sizeof(dataPacket));
I think it will work without them too.

At both ends there are set LOW TX POWER in nRF.setPALevel() function... If you have adapter YL-105 (that have on-board 3V3 regulator), you can use HIGH TX power in your Arduino sketch and power it via 5V Arduino pin. Do not use MAX TX POWER, because it will burn your Arduino regulator...

Try it and let me know.

And also, at the side of receiver, there can be implemented fail-safe mechanism like.. if no datas arrived for 5000ms, STOP engine at car or so on and wait for new datas..

Transmitter code:

// nRF24L01 TRANSMITTER
//Author: Martin Chlebovec (martinius96)
//Web: https://arduino.php5.sk?lang=en
//Donate: https://paypal.me/chlebovec

#include <SPI.h>
#include "RF24.h"
#define CE 4
#define CS 3
RF24 nRF(CE, CS);
int analogPin1 = A0;
int analogPin2 = A1;
int analogPin3 = A2;
int analogPin4 = A3;
int analogPin5 = A4;
int analogPin6 = A5;

int digitalPin1 = 5;
int digitalPin2 = 6;
int digitalPin3 = 7;
int digitalPin4 = 8;
int digitalPin5 = 9;

typedef struct {
  int potentiometer1;
  int potentiometer2;
  int potentiometer3;
  int potentiometer4;
  int potentiometer5;
  int potentiometer6;
  int switch1;
  int switch2;
  int switch3;
  int switch4;
  int switch5;
} dataPacket;

byte adresaPrijimac[] = "prijimac00";
byte adresaVysilac[] = "vysilac00";
void setup() {
  Serial.begin(115200);
  nRF.begin();
  nRF.setDataRate( RF24_250KBPS );
  nRF.setPALevel(RF24_PA_LOW);
  nRF.openWritingPipe(adresaVysilac);
  nRF.openReadingPipe(1, adresaPrijimac);
  nRF.startListening();
}

void loop() {
  dataPacket packet;
  packet.potentiometer1 = analogRead(analogPin1);
  packet.potentiometer2 = analogRead(analogPin2);
  packet.potentiometer3 = analogRead(analogPin3);
  packet.potentiometer4 = analogRead(analogPin4);
  packet.potentiometer5 = analogRead(analogPin5);
  packet.potentiometer6 = analogRead(analogPin6);

  packet.switch1 = digitalRead(digitalPin1);
  packet.switch2 = digitalRead(digitalPin2);
  packet.switch3 = digitalRead(digitalPin3);
  packet.switch4 = digitalRead(digitalPin4);
  packet.switch5 = digitalRead(digitalPin5);
  Serial.println("SENDING THESE DATAS:");
  Serial.print("POT1: ");
  Serial.println(packet.potentiometer1);
  Serial.print("POT2: ");
  Serial.println(packet.potentiometer2);
  Serial.print("POT3: ");
  Serial.println(packet.potentiometer3);
  Serial.print("POT4: ");
  Serial.println(packet.potentiometer4);
  Serial.print("POT5: ");
  Serial.println(packet.potentiometer5);
  Serial.print("POT6: ");
  Serial.println(packet.potentiometer6);
  Serial.print("SWITCH1: ");
  Serial.println(packet.switch1);
  Serial.print("SWITCH2: ");
  Serial.println(packet.switch2);
  Serial.print("SWITCH3: ");
  Serial.println(packet.switch3);
  Serial.print("SWITCH4: ");
  Serial.println(packet.switch4);
  Serial.print("SWITCH5: ");
  Serial.println(packet.switch5);
  nRF.stopListening();
  nRF.write(&packet, sizeof(dataPacket));
  //better comment that --> fix, transmitter dont get reply from receiver after datas arrived  
  //delay(10 * sizeof(dataPacket));
  nRF.startListening();
  while (nRF.available()) {
    nRF.read(&packet, sizeof(dataPacket));
    Serial.println("RECEIVER REPLY: ARRIVED THESE DATAS");
    Serial.print("POT1: ");
    Serial.println(packet.potentiometer1);
    Serial.print("POT2: ");
    Serial.println(packet.potentiometer2);
    Serial.print("POT3: ");
    Serial.println(packet.potentiometer3);
    Serial.print("POT4: ");
    Serial.println(packet.potentiometer4);
    Serial.print("POT5: ");
    Serial.println(packet.potentiometer5);
    Serial.print("POT6: ");
    Serial.println(packet.potentiometer6);
    Serial.print("SWITCH1: ");
    Serial.println(packet.switch1);
    Serial.print("SWITCH2: ");
    Serial.println(packet.switch2);
    Serial.print("SWITCH3: ");
    Serial.println(packet.switch3);
    Serial.print("SWITCH4: ");
    Serial.println(packet.switch4);
    Serial.print("SWITCH5: ");
    Serial.println(packet.switch5);
  }
  delay(50);
}

Receiver code:

// nRF24L01 RECEIVER
//Author: Martin Chlebovec (martinius96)
//Web: https://arduino.php5.sk?lang=en
//Donate: https://paypal.me/chlebovec

#include <SPI.h>
#include "RF24.h"
#define CE 4
#define CS 3
RF24 nRF(CE, CS);

typedef struct {
  int potentiometer1;
  int potentiometer2;
  int potentiometer3;
  int potentiometer4;
  int potentiometer5;
  int potentiometer6;
  int switch1;
  int switch2;
  int switch3;
  int switch4;
  int switch5;
} dataPacket;

byte adresaPrijimac[] = "prijimac00";
byte adresaVysilac[] = "vysilac00";
void setup() {
  Serial.begin(115200);
  nRF.begin();
  nRF.setDataRate( RF24_250KBPS );
  nRF.setPALevel(RF24_PA_LOW);
  nRF.openWritingPipe(adresaPrijimac);
  nRF.openReadingPipe(1, adresaVysilac);
  nRF.startListening();
}

void loop() {
  dataPacket packet;
  if ( nRF.available()) {
    while (nRF.available()) {
      nRF.read(&packet, sizeof(dataPacket));
      Serial.println("DATAS ARRIVED: ");
      Serial.print("POT1: ");
      Serial.println(packet.potentiometer1);
      Serial.print("POT2: ");
      Serial.println(packet.potentiometer2);
      Serial.print("POT3: ");
      Serial.println(packet.potentiometer3);
      Serial.print("POT4: ");
      Serial.println(packet.potentiometer4);
      Serial.print("POT5: ");
      Serial.println(packet.potentiometer5);
      Serial.print("POT6: ");
      Serial.println(packet.potentiometer6);
      Serial.print("SWITCH1: ");
      Serial.println(packet.switch1);
      Serial.print("SWITCH2: ");
      Serial.println(packet.switch2);
      Serial.print("SWITCH3: ");
      Serial.println(packet.switch3);
      Serial.print("SWITCH4: ");
      Serial.println(packet.switch4);
      Serial.print("SWITCH5: ");
      Serial.println(packet.switch5);
      /*IF YOU WANT TO WORK WITH ONE OF VARIABLE....
        int value = packet.switch5;
        if (value==HIGH){
        Serial.println("VALUE OF SWITCH 5 IS HIGH, TURN ON MOTOR");
        }else{
        Serial.println("VALUE OF SWITCH 5 IS LOW, TURN OFF MOTOR");
        }
        int pot_value = packet.potentiometer1;
        if (pot_value==1023){
        Serial.println("ANALOG VALUE OF POT 1 IS MAX");
        }
      */
    }
    nRF.stopListening();
    nRF.write(&packet, sizeof(dataPacket));
    delay(10 * sizeof(dataPacket));
    nRF.startListening();
  }
}

Schematics for receiver and transmitter - Arduino with nRF24L01:

thankyou for your replies!

srnet:
I will guess your problem is due to the transmitter and receiver being too far apart and you need to use a different, longer range module.

srnet, they are 10 cm apart, and I bought 2 nrf24l01 modules that have 1km range, seems enough!?!

martinius, I will try your code!

Peter-CAD-HST:
try and check the HC-12 Wireless Transceiver Module for your project.

Peter, I already bought the nrf24l01 so ...

Isaak

Yes... one mistake there... if delay(10 * sizeof(dataPacket)); is in transmitter code, it will not receive datas from receiver... Transmitter will be unable to "know" what receiver received...

If that delay removed, output (Transmitter side):

You see, transmitter is sending datas faster, than receiver is replying with them....
There are first datas (at top of screenshot) same as arrived from client, but between these outputs there was one more data packet sent...

Transmitter is sending data packet each 50ms, that means.. 20 times per second.
There is also used slowest data rate speed, that will help to communicate on larger distance..

some update for others?