The RF24L01 - Wireless Joystick for Arduino Car with nRF24L01 PA+LNA

Duy Thuc 3/26/2024
0 likes
rc


RC Tranmistter

code google drive: https://drive.google.com/drive/folders/1W6DFpaZA_hRhlrldt4GyOk2pYwppf45u?usp=sharing

code RC transmitter


#include <SPI.h>

#include <nRF24L01.h>

#include <RF24.h>

RF24 radio(8,9); // CE, CSN

const byte address[6] = "00001";

char xyData[32] = "";

String xAxis, yAxis;

void setup() {

  Serial.begin(9600);

  radio.begin();

  radio.openWritingPipe(address);

  radio.setPALevel(RF24_PA_MIN);

  radio.stopListening();

}

void loop() {

  xAxis = analogRead(A4); 

  yAxis = analogRead(A5); 

  // X value

  xAxis.toCharArray(xyData, 5); 

  radio.write(&xyData, sizeof(xyData)); 

  

  // Y value

  yAxis.toCharArray(xyData, 5);

  radio.write(&xyData, sizeof(xyData));

  delay(20);

}


code RC Receiver



#include <SPI.h>

#include <nRF24L01.h>

#include <RF24.h>

#define enA 2  

#define in1 3

#define in2 4

#define enB 7   

#define in3 5

#define in4 6

RF24 radio(8, 9); 

const byte address[6] = "00001";

char receivedData[32] = "";

int  xAxis, yAxis;

int motorSpeedA = 0;

int motorSpeedB = 0;

void setup() {

  pinMode(enA, OUTPUT);

  pinMode(enB, OUTPUT);

  pinMode(in1, OUTPUT);

  pinMode(in2, OUTPUT);

  pinMode(in3, OUTPUT);

  pinMode(in4, OUTPUT);

  Serial.begin(9600);

  radio.begin();

  radio.openReadingPipe(0, address);

  radio.setPALevel(RF24_PA_MIN);

  radio.startListening();

    digitalWrite(in1, LOW);

    digitalWrite(in2, LOW);

    

    digitalWrite(in3, LOW);

    digitalWrite(in4, LOW);

    

}

void loop() {

    

  if (radio.available()) {   

    radio.read(&receivedData, sizeof(receivedData)); 

    xAxis = atoi(&receivedData[0]); 

    delay(10);

    radio.read(&receivedData, sizeof(receivedData));

    yAxis = atoi(&receivedData[0]);

    delay(10);  

  }

  

  

  if (xAxis < 470) {

  

    digitalWrite(in1, HIGH);

    digitalWrite(in2, LOW);

    

    digitalWrite(in3, HIGH);

    digitalWrite(in4, LOW);

  

    motorSpeedA = map(xAxis, 470, 0, 0, 255);

    motorSpeedB = map(xAxis, 470, 0, 0, 255);

  }

  else if (xAxis > 550) {

   

    digitalWrite(in1, LOW);

    digitalWrite(in2, HIGH);

   

    digitalWrite(in3, LOW);

    digitalWrite(in4, HIGH);

    

    motorSpeedA = map(xAxis, 550, 1023, 0, 255);

    motorSpeedB = map(xAxis, 550, 1023, 0, 255);

  }

    else {

    motorSpeedA = 0;

    motorSpeedB = 0;

  }

  if (yAxis < 470) {    

    int yMapped = map(yAxis, 470, 0, 0, 255);

    

    motorSpeedA = motorSpeedA + yMapped;

    motorSpeedB = motorSpeedB - yMapped;


    if (motorSpeedA > 255) {

      motorSpeedA= 255;

    }

    if (motorSpeedB < 0) {

      motorSpeedB = 0;

    }

  

  }

  else if (yAxis > 550) {

   

    int yMapped = map(yAxis, 550, 1023, 0, 255);

    

    motorSpeedB = motorSpeedB + yMapped;

    motorSpeedA = motorSpeedA - yMapped;

 

    if (motorSpeedB > 255) {  

      motorSpeedB = 255;

    }

    if (motorSpeedA < 0) {

      motorSpeedA = 0;

    }


  }


  if (motorSpeedA < 70) {

    motorSpeedA = 0;

  }

  if (motorSpeedB < 70) {

    motorSpeedB = 0;

  }

  analogWrite(enA, motorSpeedA);

  analogWrite(enB, motorSpeedB);

  

}


Powered by Froala Editor

;
Comments