This is the code for the Arduino based RC car with NRF24 conenction. You will need the NRF24 based radio controller as well from the past tutorial. Make sure you install the needed libraries as well. Downlaod the .zip file or copy+paste from below. Read all comments in the code in order to understand more.
/* Test code for the Radio control receiver RC car
* Tutorial: https://www.electronoobs.com/eng_arduino_tut92.php
* Schematic: https://www.electronoobs.com/eng_arduino_tut92_sch1.php
* Video: https://www.youtube.com/watch?v=bb1u_kne-m4
* Install the NRF24 library to your IDE
* Upload this code to the Arduino NANO
* Connect a NRF24 module to it:
Module // Arduino UNO
GND -> GND
Vcc -> 3.3V
CE -> D9
CSN -> D10
CLK -> D13
MOSI -> D11
MISO -> D12
Please, like share and subscribe : https://www.youtube.com/c/ELECTRONOOBS
*/
#include <SPI.h>
#include <nRF24L01.h> //Download it here: https://www.electronoobs.com/eng_arduino_NRF24_lib.php
#include <RF24.h>
#include <Servo.h> //To create PWM signals we need this lybrary
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h> //Downlaod it here: https://www.electronoobs.com/eng_arduino_tut22_code5.php
const uint64_t pipeIn = 0xE8E8F0F0E1LL; //Remember that this code is the same as in the transmitter
RF24 radio(9, 10);
//We could use up to 32 channels
struct MyData {
byte throttle; //We define each byte of data input, in this case just 6 channels
byte yaw;
byte pitch;
byte roll;
byte AUX1;
byte AUX2;
};
MyData data;
void resetData()
{
//We define the inicial value of each data input
//3 potenciometers will be in the middle position so 127 is the middle from 254
data.throttle = 127;
data.yaw = 127;
data.pitch = 127;
data.roll = 127;
data.AUX1 = 0;
data.AUX2 = 0;
}
int mot_A_L = 3;
int mot_A_R = 5;
int mot_B_L = 6;
int mot_B_R = 9;
//Servo channel_1;
//Servo channel_2;
//Servo channel_3;
//Servo channel_4;
//Servo channel_5;
//Servo channel_6;
int PWM_A_L = 0;
int PWM_A_R = 0;
int PWM_B_L = 0;
int PWM_B_R = 0;
bool movement = false;
bool stopped = false;
/**************************************************/
void setup()
{
//Attach the servo signal on pins from D2 to D8
//mot_A_L.attach(3);
//mot_A_R.attach(5);
//mot_B_L.attach(6);
//mot_B_R.attach(9);
//channel_5.attach(6);
//channel_6.attach(7);
pinMode(mot_A_L,OUTPUT);
pinMode(mot_A_R,OUTPUT);
pinMode(mot_B_L,OUTPUT);
pinMode(mot_B_R,OUTPUT);
digitalWrite(mot_A_L,LOW);
digitalWrite(mot_A_R,LOW);
digitalWrite(mot_B_L,LOW);
digitalWrite(mot_B_R,LOW);
Serial.begin(9600);
//DFPlayerSerial.begin(9600);
mp3_set_serial(Serial);
mp3_set_volume(15);
//You should always have the same speed selected in the serial monitor
resetData();
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1,pipeIn);
//we start the radio comunication
radio.startListening();
}
/**************************************************/
unsigned long lastRecvTime = 0;
void recvData()
{
while ( radio.available() ) {
radio.read(&data, sizeof(MyData));
lastRecvTime = millis(); //here we receive the data
}
}
/**************************************************/
void loop()
{
recvData();
unsigned long now = millis();
//Here we check if we've lost signal, if we did we reset the values
if ( now - lastRecvTime > 1000 ) {
// Signal lost?
resetData();
}
/*
Serial.print("Throttle: "); Serial.print(data.throttle); Serial.print(" ");
Serial.print("Yaw: "); Serial.print(data.yaw); Serial.print(" ");
Serial.print("Pitch: "); Serial.print(data.pitch); Serial.print(" ");
Serial.print("Roll: "); Serial.print(data.roll); Serial.print("\n");
*/
if(data.yaw > 124 && data.yaw < 130)
{
if(data.throttle > 124 && data.throttle < 130)
{
if(!stopped)
{
mp3_play(2);
mp3_next();
stopped = true;
}
PWM_A_L = 0;
PWM_A_R = 0;
PWM_B_L = 0;
PWM_B_R = 0;
movement = 0;
}
if(data.throttle > 130)
{
if(!movement)
{
mp3_play(1);
mp3_next();
movement = true;
}
stopped = false;
PWM_A_L = map(data.throttle,130,255,20,255);
PWM_A_R = 0;
PWM_B_L = 0;
PWM_B_R = map(data.throttle,130,255,20,255);
}
if(data.throttle < 124)
{
if(!movement)
{
mp3_play(1);
mp3_next();
movement = true;
}
stopped = false;
PWM_A_L = 0;
PWM_A_R = map(data.throttle,124,0,20,255);
PWM_B_L = map(data.throttle,124,0,20,255);
PWM_B_R = 0;
}
}//end yaw middle
if(data.yaw > 130)
{
if(data.throttle > 124 && data.throttle < 130)
{
if(!movement)
{
mp3_play(1);
mp3_next();
movement = true;
}
stopped = false;
PWM_A_L = 0;
PWM_A_R = map(data.yaw,130,255,20,255);
PWM_B_L = 0;
PWM_B_R = map(data.yaw,130,255,20,255);
}
if(data.throttle > 130)
{
if(!movement)
{
mp3_play(1);
mp3_next();
movement = true;
}
stopped = false;
PWM_A_L = map(data.throttle,130,255,20,255) - map(data.yaw,130,255,20,255);
PWM_A_R = 0;
PWM_B_L = 0;
PWM_B_R = map(data.throttle,130,255,20,255) + map(data.yaw,130,255,20,255);
}
if(data.throttle < 124)
{
if(!movement)
{
mp3_play(1);
mp3_next();
movement = true;
}
stopped = false;
PWM_A_L = 0;
PWM_A_R = map(data.throttle,124,0,20,255) - map(data.yaw,130,255,20,255);
PWM_B_L = map(data.throttle,124,0,20,255) + map(data.yaw,130,255,20,255);
PWM_B_R = 0;
}
}//end yaw right
if(data.yaw < 124)
{
if(data.throttle > 124 && data.throttle < 130)
{
if(!movement)
{
mp3_play(1);
mp3_next();
movement = true;
}
stopped = false;
PWM_A_L = map(data.yaw,124,0,20,255);
PWM_A_R = 0;
PWM_B_L = map(data.yaw,124,0,20,255);
PWM_B_R = 0;
}
if(data.throttle > 130)
{
if(!movement)
{
mp3_play(1);
mp3_next();
movement = true;
}
stopped = false;
PWM_A_L = map(data.throttle,130,255,20,255) + map(data.yaw,124,0,20,255);
PWM_A_R = 0;
PWM_B_L = 0;
PWM_B_R = map(data.throttle,130,255,20,255) - map(data.yaw,124,0,20,255);
}
if(data.throttle < 124)
{
if(!movement)
{
mp3_play(1);
mp3_next();
movement = true;
}
stopped = false;
PWM_A_L = 0;
PWM_A_R = map(data.throttle,124,0,20,255) + map(data.yaw,124,0,20,255);
PWM_B_L = map(data.throttle,124,0,20,255) - map(data.yaw,124,0,20,255);
PWM_B_R = 0;
}
}//end yaw left
PWM_A_L = constrain(PWM_A_L,0,255);
PWM_A_R = constrain(PWM_A_R,0,255);
PWM_B_L = constrain(PWM_B_L,0,255);
PWM_B_R = constrain(PWM_B_R,0,255);
analogWrite(mot_A_L,PWM_A_L);
analogWrite(mot_A_R,PWM_A_R);
analogWrite(mot_B_L,PWM_B_L);
analogWrite(mot_B_R,PWM_B_R);
}
/**************************************************/