Finalization of the sketch receiver and transmitter for RC cars

Hello, can anyone help in editing and finalizing the sketch? I wrote a sketch for the video lessons on the receiver and the transmitter (for the RC machine), but it doesn’t work, can anyone help to improve it?
the sketch itself

transmitter

#include <SPI.h> // библиотека для работы с шиной SPI
#include “nRF24L01.h” // библиотека радиомодуля
#include “RF24.h” // ещё библиотека радиомодуля

RF24 radio(9, 10);

byte address[6] = {“1Node”, “2Node”, “3Node”, “4Node”, “5Node”, “6Node”}; //возможные номера труб
byte potent = 0;
byte potent1 = 1;
byte button = 3;
byte button1 = 4;
byte button2 = 2;
byte transmit_data[5];

void setup() {
Serial.begin(9600);
radio.begin();
radio.setAutoAck(1);
radio.setRetries(0, 15);
radio.setPayloadSize(32);
radio.openWritingPipe(address[0]);
radio.setChannel(0x60);

radio.setPALevel (RF24_PA_MAX);
radio.setDataRate (RF24_250KBPS);
radio.powerUp();
radio.stopListening();
}

void loop() {

transmit_data[0] = map(analogRead(0), 0, 1023, 1000, 2000); // I hope the reverse will work ???
transmit_data[1] = map(analogRead(1), 0, 1023, 0, 180);
transmit_data[2] = digitalRead(button2);
transmit_data[3] = digitalRead(button);
transmit_data[4] = digitalRead(button1);
radio.write(transmit_data, sizeof(transmit_data));
Serial.println(analogRead(0));

}

receiver:
Receiver

#include <SPI.h>
#include “nRF24L01.h”
#include “RF24.h”
#include <Servo.h>

RF24 radio(9,10); //

byte address[6] = {“1Node”,“2Node”,“3Node”,“4Node”,“5Node”,“6Node”}; //возможные номера труб
byte recieved_data [6];
byte motor = 2;
byte servo = 3;
byte servo1 = 4;
byte light = A1;
byte hightlight = A2;
byte glight = A0;
byte slight = A3;
byte rlight = A4;
Servo rulevaya;
Servo Dvorniki;
Servo Motor;
void setup(){
rulevaya.attach(servo);
Dvorniki.attach(servo1);
Motor.attach(motor);
pinMode (2 , OUTPUT);
pinMode (3 , OUTPUT);
pinMode (4 , OUTPUT);
digitalWrite(2 , HIGH);
Serial.begin(9600); //открываем порт для связи с ПК
radio.begin(); //активировать модуль
radio.setAutoAck(1); //режим подтверждения приёма, 1 вкл 0 выкл
radio.setRetries(0,15); //(время между попыткой достучаться, число попыток)
radio.enableAckPayload(); //разрешить отсылку данных в ответ на входящий сигнал
radio.setPayloadSize(32); //размер пакета, в байтах

radio.openReadingPipe(1,address[0]); //хотим слушать трубу 0
radio.setChannel(0x60); //выбираем канал (в котором нет шумов!)

radio.setPALevel (RF24_PA_MAX); //уровень мощности передатчика. На выбор RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
radio.setDataRate (RF24_250KBPS); //скорость обмена. На выбор RF24_2MBPS, RF24_1MBPS, RF24_250KBPS
//должна быть одинакова на приёмнике и передатчике!
//при самой низкой скорости имеем самую высокую чувствительность и дальность!!

radio.powerUp(); //начать работу
radio.startListening(); //начинаем слушать эфир, мы приёмный модуль
}

void loop() {
byte pipeNo, gotByte;
while( radio.available(&pipeNo)){ // слушаем эфир со всех труб
radio.read( &recieved_data, sizeof(recieved_data) );
}
Motor.write(recieved_data[0]);
rulevaya.write(recieved_data[1]); //steering
{
digitalWrite(servo1, recieved_data[2]); //in theory, this is a wiper
Dvorniki.write(0);
delay(900);
Dvorniki.write(180);
delay(900);
}
digitalWrite(light, recieved_data[3]); // Low beam
digitalWrite(hightlight, recieved_data[4]); // high beam
// Subsequent two lines - rear dimensions (incl. With headlights)
digitalWrite(glight, recieved_data[3]);
digitalWrite(glight, recieved_data[4]);
// there should be stop signals and reverse lights here, but I don’t know how to implement them
}

pins:
Transmitter:
A0 - Potentiometer (gas)
A1 - Potentiometer (steering wheel)
D2 - Switch (wipers)
D3 - Toggle Switch (Dipped)
D4 - Tumbler (same as D3, high beam)
Receiver:
A0 - side lights
A1 - Dipped headlights
A2 - High Beam Headlights
A3 - Stoplight
A4 - Reversing Lights
D2 - Motor (more precisely, the speed controller with reverse …)
D3 - servo on the steering
D4 - servo on the wipers

but it doesn't work

You expect it to do something, but you failed to describe what you expect it to do. The code ACTUALLY does something, but you failed to describe what it actually does.

So, no, we can't help you.

map() and digitalRead() …

transmit_data[0] = map(analogRead(0), 0, 1023, 1000, 2000); //   I hope the reverse will work ???  
transmit_data[1] = map(analogRead(1), 0, 1023, 0, 180);
transmit_data[2] = digitalRead(button2);
transmit_data[3] = digitalRead(button);
transmit_data[4] = digitalRead(button1);

return ints but you’re putting them into a byte array:

byte transmit_data[5];

For transmit_data[1] you might be able to cast the return to a byte like:

transmit_data[1] = (byte)(map(analogRead(1), 0, 1023, 0, 180));

since its return value is <256. Ditto for the digitalReads. But for the mapped value from 1000-2000, you’re going to need to send an array of ints or break it down into two bytes;

int spongebob = map(analogRead(0), 0, 1023, 1000, 2000);
transmit_data[0] = (byte)(spongebob & 0xff);
transmit_data[1] = (byte)((spongebob >> 8) & 0xff);

and then reconstruct it on the receiver.

You haven’t provided much info but from your code it seems like you are trying to teach How to do wireless communication with NRF24L01.

NRF24L01 are bit tricky to configure. First of all, make sure you have connected all the pins correctly and properly. Now it’s better to place a 100uF capacitor between 3.3V & GND. Now upload these simple codes in both of your Arduino.

Transmitting Side:

#include <SPI.h>
    #include "nRF24L01.h"
    #include "RF24.h"
 
    RF24 radio(9,10);
 
    const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
    unsigned long Command = 1;
    void setup()
    {
    Serial.begin(57600);
 
    radio.begin();
    radio.setRetries(15,15);
    radio.openReadingPipe(1,pipes[1]);
    radio.startListening();
    radio.printDetails();
    radio.openWritingPipe(pipes[0]);
    radio.openReadingPipe(1,pipes[1]);
    radio.stopListening();
    }
 
    void loop(void)
    {
    radio.stopListening();
 
    radio.write( &Command, sizeof(unsigned long) );
 
    radio.startListening();
 
    delay(1000);
    }

Receiving Side:

   #include <SPI.h>
    #include "nRF24L01.h"
    #include "RF24.h"
 
    RF24 radio(9,10);
 
    const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
 
    typedef enum { role_ping_out = 1, role_pong_back } role_e;
 
    const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
 
    role_e role = role_pong_back;
 
    void setup(void)
    {
    Serial.begin(57600);
    radio.begin();
    radio.setRetries(15,15);
    radio.openReadingPipe(1,pipes[1]);
    radio.startListening();
    radio.printDetails();
    radio.openWritingPipe(pipes[1]);
    radio.openReadingPipe(1,pipes[0]);
    radio.startListening();
    }
 
    void loop(void)
    {
 
    if ( radio.available() )
    {
    unsigned long data = 0;
    radio.read( &data, sizeof(unsigned long) );
    Serial.println(data);
 
    delay(20);
    }
    }

Open Serial Terminals on both sides and when you write in the transmitting side, it will appear on the receiving side.

PaulS: You expect it to do something, but you failed to describe what you expect it to do. The code ACTUALLY does something, but you failed to describe what it actually does.

So, no, we can't help you.

Oh Well, yes, I forgot, I expect that this will be a radio-controlled system on the NRF24 in which, in addition to controlling the accelerator with reverse and steering, there must be control of lighting equipment, namely, switching of the dipped and main beam (along with the overall rear red lights) (from the toggle switch switching between modes, one more toggle switch is connected to a common minus for general switching on / off headlights), turning on the brake lights when the accelerator is released, and accordingly turning on the reverse lights, and another function is to control e wiper (in one mode on / off)

jackthom41:
You haven’t provided much info but from your code it seems like you are trying to teach How to do wireless communication with NRF24L01.

NRF24L01 are bit tricky to configure. First of all, make sure you have connected all the pins correctly and properly. Now it’s better to place a 100uF capacitor between 3.3V & GND. Now upload these simple codes in both of your Arduino.

Transmitting Side:

#include <SPI.h>

#include “nRF24L01.h”
   #include “RF24.h”

RF24 radio(9,10);

const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
   unsigned long Command = 1;
   void setup()
   {
   Serial.begin(57600);

radio.begin();
   radio.setRetries(15,15);
   radio.openReadingPipe(1,pipes[1]);
   radio.startListening();
   radio.printDetails();
   radio.openWritingPipe(pipes[0]);
   radio.openReadingPipe(1,pipes[1]);
   radio.stopListening();
   }

void loop(void)
   {
   radio.stopListening();

radio.write( &Command, sizeof(unsigned long) );

radio.startListening();

delay(1000);
   }




**Receiving Side:**



#include <SPI.h>
   #include “nRF24L01.h”
   #include “RF24.h”

RF24 radio(9,10);

const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

typedef enum { role_ping_out = 1, role_pong_back } role_e;

const char* role_friendly_name = { “invalid”, “Ping out”, “Pong back”};

role_e role = role_pong_back;

void setup(void)
   {
   Serial.begin(57600);
   radio.begin();
   radio.setRetries(15,15);
   radio.openReadingPipe(1,pipes[1]);
   radio.startListening();
   radio.printDetails();
   radio.openWritingPipe(pipes[1]);
   radio.openReadingPipe(1,pipes[0]);
   radio.startListening();
   }

void loop(void)
   {

if ( radio.available() )
   {
   unsigned long data = 0;
   radio.read( &data, sizeof(unsigned long) );
   Serial.println(data);

delay(20);
   }
   }




Open Serial Terminals on both sides and when you write in the transmitting side, it will appear on the receiving side.

No, I connected the NRF24 itself correctly (I checked it several times) in the firmware itself

Berkut_24: Oh Well, yes, I forgot, I expect that this will be a radio-controlled system on the NRF24 in which, in addition to controlling the accelerator with reverse and steering, there must be control of lighting equipment, namely, switching of the dipped and main beam (along with the overall rear red lights) (from the toggle switch switching between modes, one more toggle switch is connected to a common minus for general switching on / off headlights), turning on the brake lights when the accelerator is released, and accordingly turning on the reverse lights, and another function is to control e wiper (in one mode on / off)

O.k. reasonable start to a description of what you want it to do. Now, how much of that does the code you have do? And what does it do that's different from what you wanted?

You have to tell us these things because most of us don't have the exact same hardware you have so we can't really test it for you.

Steve

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

...R

Blackfin: map() and digitalRead() ... return ints but you're putting them into a byte array:

The fact that the type of variable that digitalRead() returns is int is meaningless with regard to where the value is stored. The possible values that are contained in the bit stream are HIGH or LOW (1 or 0). Both values fit easily in a byte.

The mystery is why digitalRead() returns a 16 bit, signed, value when the range of possible values is 0 to 1.

Assuming that the input is in the from range (which is not an unreasonable assumption), the output from the map() function will be in the to range.

You are correct in that the first call to map() will produce a result that won't fit in a byte (element of an array), but the result of the second call to map() will fit.

Dividing the result of the first call, on the sending end, by 10, and multiplying by 10 on the receiving end, would result in only minimal loss of precision.