DIY NRF24L01 JOYSTICK TRANSMITTER AND RECEIVER FOR DRONES AND RC CARS ||...




CODE:

TRANSMITTER CODE:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x3F, 16, 2); // Change to (0x27,20,4) for 20x4 LCD.

#define JOYX A0
#define JOYY A2
#define POTA A1
#define POTB A3
#define SWITCHA 4
#define SWITCHB 3


RF24 radio(9, 10); // CE, CSN
 
const byte address[6] = "mkinventions100";
 
void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
  pinMode(JOYX, INPUT);
  pinMode(JOYY, INPUT);
  pinMode(POTA, INPUT);
  pinMode(POTB, INPUT);
  pinMode(SWITCHA, INPUT_PULLUP);
  pinMode(SWITCHB, INPUT_PULLUP);


  lcd.setCursor(3, 0);
  lcd.print("WELCOME TO");
  lcd.setCursor(2, 1);
  lcd.print("MKINVENTIONS");
  
  }

void loop() {
  int ROBOT[6];
 
 ROBOT[0] = analogRead(JOYX);
 ROBOT[1] = analogRead(JOYY);
 ROBOT[2] = analogRead(POTA);
 ROBOT[3] = analogRead(POTB);
 ROBOT[4] = digitalRead(SWITCHA);
 ROBOT[5] = digitalRead(SWITCHB);

 radio.write(&ROBOT, sizeof(ROBOT));
 delay(5);


int POTENTIOMETERA = map(analogRead(POTA), 0, 1023, 0, 180); 
int POTENTIOMETERB = map(analogRead(POTB), 0, 1023, 0, 180); 
int SWTA = digitalRead(SWITCHA);
int SWTB = digitalRead(SWITCHB); 


Serial.print("POTA:");
Serial.println(POTENTIOMETERA);
Serial.print("POTB:");
Serial.println(POTENTIOMETERB);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("POTA:");
  lcd.setCursor(5, 0);
  lcd.print(POTENTIOMETERA);
  lcd.setCursor(8, 0);
  lcd.print("|");  
  lcd.setCursor(0, 1);
  lcd.print("POTB:");
  lcd.setCursor(5, 1);
  lcd.print(POTENTIOMETERB);
  lcd.setCursor(8, 1);
  lcd.print("|");
  
  lcd.setCursor(10, 0);
  lcd.print("SWTA:");
  lcd.setCursor(15, 0);
  lcd.print(SWTA);
  
  lcd.setCursor(10, 1);
  lcd.print("SWTB:");
  lcd.setCursor(15, 1);
  lcd.print(SWTB);


}



RECEIVER CODE:

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


RF24 radio(9, 10); // CE, CSN
 
const byte address[6] = "mkinventions100";

int LMT1 = 5;
int LMT2 = 6;
int RMT1 = 7;
int RMT2 = 8;
int LEDA = A0;
int LEDB = A1;

Servo servo1;
Servo servo2;


 
void setup() {
  Serial.begin(9600);
 pinMode(LMT1, OUTPUT);
 pinMode(LMT2, OUTPUT);
 pinMode(RMT1, OUTPUT);
 pinMode(RMT2, OUTPUT);

 pinMode(LEDA, OUTPUT);
 pinMode(LEDB, OUTPUT);

 servo1.attach(2);
 servo2.attach(3);
  
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();

Serial.println("stop");
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, LOW);

digitalWrite(LEDA, LOW);
digitalWrite(LEDB, LOW);

servo1.write(90);
servo2.write(90);
}


void loop() {
  delay(5);
  if ( radio.available()) {
ROBOT();
  }
}


void ROBOT(){
Serial.println("stop");
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, LOW);


while (radio.available()) {
 int ROBOT[6];
  
radio.read(&ROBOT, sizeof(ROBOT));
int POTA = map(ROBOT[2], 0, 1023, 0, 180);
int POTB = map(ROBOT[3], 0, 1023, 0, 180);





if(ROBOT[0] > 520){
Serial.println("FORWARD");
FORWARD();
  }

else if(ROBOT[0] < 500){
Serial.println("BACKWARD");
BACKWARD();
  }

else if(ROBOT[1] > 600){
Serial.println("LEFT");
LEFT();
}

else if(ROBOT[1] < 500){
Serial.println("RIGHT");
RIGHT();
}

servo1.write(POTA);
servo2.write(POTB);

if(ROBOT[4] == 1){
Serial.println("A LED ON");
  digitalWrite(LEDA, HIGH);
  }

if(ROBOT[4] == 0){
  digitalWrite(LEDA, LOW);
  }

if(ROBOT[5] == 1){
Serial.println("B LED ON");
  digitalWrite(LEDB, HIGH);
  }
 
if(ROBOT[5] == 0){
  digitalWrite(LEDB, LOW);
  }
}
}



void LEFT(){
  digitalWrite(LMT1, LOW);
  digitalWrite(LMT2, HIGH);
  digitalWrite(RMT1, HIGH);
  digitalWrite(RMT2, LOW);
}

void RIGHT(){
  digitalWrite(LMT1, HIGH);
  digitalWrite(LMT2, LOW);
  digitalWrite(RMT1, LOW);
  digitalWrite(RMT2, HIGH);
}

void BACKWARD(){
  digitalWrite(LMT1, HIGH);
  digitalWrite(LMT2, LOW);
  digitalWrite(RMT1, HIGH);
  digitalWrite(RMT2, LOW);
}

void FORWARD(){
  digitalWrite(LMT1, LOW);
  digitalWrite(LMT2, HIGH);
  digitalWrite(RMT1, LOW);
  digitalWrite(RMT2, HIGH); 
}

Post a Comment

0 Comments