MisterBotBreak
Published

How to Make a RC Controller #2

This project will show you how to create a RC Controller.

BeginnerFull instructions provided3 hours1,597
How to Make a RC Controller #2

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
Analog Joysticks
×2
TFT screen 2.8
×1
nrf24L01
×1
nRF24l01 adapter
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Schematics

arduinomega_support_rc_36BkDuD1gi.stl

main_body_rc_ZEKAGtBblp.stl

tft_screen_support_rc_kBnDQytnlv.stl

Code

Remote Control #2

Arduino
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#include <SPI.h>                                                     
#include <RF24.h>
#include <Elegoo_GFX.h>
#include <Elegoo_TFTLCD.h>
#include <TouchScreen.h>


#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4


#define  BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define ORANGE   0xFA60
#define LIME     0x07FF
#define ORANGE1   ((255 / 8) << 11) | ((165 / 4) << 5) | (0 / 8) 
#define ORANGE2   ((255 / 8) << 11) | ((187 / 4) << 5) | (61 / 8) 
#define ORANGE3   ((255 / 8) << 11) | ((201 / 4) << 5) | (102 / 8) 
#define BLUE1   ((2 / 8) << 11) | ((9 / 4) << 5) | (219 / 8)
#define BLUE2   ((36 / 8) << 11) | ((43 / 4) << 5) | (253 / 8)
#define BLUE3   ((103 / 8) << 11) | ((108 / 4) << 5) | (254 / 8)
#define DARKBLUE1   ((2 / 8) << 11) | ((8 / 4) << 5) | (185 / 8)
#define DARKBLUE2   ((1 / 8) << 11) | ((5 / 4) << 5) | (118 / 8)
#define RED1    ((255 / 8) << 11) | ((0 / 4) << 5) | (0 / 8)
#define RED2    ((255 / 8) << 11) | ((61 / 4) << 5) | (61 / 8)
#define RED3    ((255 / 8) << 11) | ((122 / 4) << 5) | (122 / 8)
#define CACTUS1 ((196 / 8) << 11) | ((235 / 4) << 5) | (20 / 8)
#define CACTUS2 ((217 / 8) << 11) | ((242 / 4) << 5) | (103 / 8)
#define CACTUS3 ((231 / 8) << 11) | ((247 / 4) << 5) | (158 / 8)
#define YELLOW1 ((235 / 8) << 11) | ((254 / 4) << 5) | (1 / 8)
#define YELLOW2 ((239 / 8) << 11) | ((254 / 4) << 5) | (62 / 8)
#define YELLOW3 ((244 / 8) << 11) | ((254 / 4) << 5) | (123 / 8)
#define MAGENTA1 ((245 / 8) << 11) | ((0 / 4) << 5) | (245 / 8)
#define MAGENTA2 ((255 / 8) << 11) | ((82 / 4) << 5) | (255 / 8)
#define MAGENTA3 ((255 / 8) << 11) | ((184 / 4) << 5) | (255 / 8)
#define CYAN1    ((0 / 8) << 11) | ((255 / 4) << 5) | (255 / 8)
#define CYAN2    ((122 / 8) << 11) | ((255 / 4) << 5) | (255 / 8)
#define CYAN3    ((204 / 8) << 11) | ((255 / 4) << 5) | (255 / 8)    
#define PINK1   ((230 / 8) << 11) | ((7 / 4) << 5) | (92 / 8)
#define PINK2   ((249 / 8) << 11) | ((60 / 4) << 5) | (132 / 8)
#define PINK3   ((251 / 8) << 11) | ((131 / 4) << 5) | (177 / 8)
#define GREEN1   ((15 / 8) << 11) | ((186 / 4) << 5) | (15 / 8)
#define GREEN2   ((36 / 8) << 11) | ((238 / 4) << 5) | (36 / 8)
#define GREEN3   ((103 / 8) << 11) | ((243 / 4) << 5) | (103 / 8)
#define GREY1   ((128 / 8) << 11) | ((128 / 4) << 5) | (128 / 8)
#define GREY2   ((168 / 8) << 11) | ((168 / 4) << 5) | (168 / 8)
#define GREY3   ((200 / 8) << 11) | ((200 / 4) << 5) | (200 / 8)
#define PURPLE1   ((92 / 8) << 11) | ((0 / 4) << 5) | (92 / 8)
#define PURPLE2   ((133 / 8) << 11) | ((0 / 4) << 5) | (133 / 8)
#define PURPLE3   ((173 / 8) << 11) | ((0 / 4) << 5) | (173 / 8)
#define BROWN1   ((41 / 8) << 11) | ((10 / 4) << 5) | (10 / 8)
#define BROWN2   ((73 / 8) << 11) | ((19 / 4) << 5) | (19 / 8)
#define BROWN3   ((106 / 8) << 11) | ((27 / 4) << 5) | (27 / 8)
#define BROWN4   ((138 / 8) << 11) | ((35 / 4) << 5) | (35 / 8)
#define BROWN5   ((171 / 8) << 11) | ((43 / 4) << 5) | (43 / 8)
#define BROWN6   ((203 / 8) << 11) | ((52 / 4) << 5) | (52 / 8)
#define BROWN7   ((212 / 8) << 11) | ((84 / 4) << 5) | (84 / 8)
#define BEIGE1   ((221 / 8) << 11) | ((221 / 4) << 5) | (136 / 8)
#define BEIGE2   ((230 / 8) << 11) | ((230 / 4) << 5) | (168 / 8)
#define BEIGE3   ((235 / 8) << 11) | ((235 / 4) << 5) | (184 / 8)


#define YP A2  
#define XM A3 
#define YM 8   
#define XP 9   

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 400);
Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

TSPoint p;
    
int data[6];  
int infos[6];

int xjoy1;
int yjoy1;
int xjoy2;
int yjoy2;
float angley1 = 0;
float angley2 = 0;

int menu = 0;
int startingmenuvariable = 0;
int menulaunch = 0;

int timer = 0;
int delaybutton4;
int delaybutton5;

void setup(){       
Serial.begin(9600);   

tft.reset();
tft.begin(0x9341);
tft.fillScreen(BLACK);
tft.setRotation(3);

Mirf.cePin = 10; 
  Mirf.csnPin = 11; 
  Mirf.spi = &MirfHardwareSpi; 
  Mirf.init(); 

  Mirf.channel = 1; 
  Mirf.payload = sizeof(int) * 6; 
  Mirf.config(); 

  Mirf.setTADDR((byte *) "nrf02"); 
  Mirf.setRADDR((byte *) "nrf01"); 

data[4] = 0;
  data[5] = 0;



}


void menuAstatis(){
tft.fillCircle(40, 0, 100, MAGENTA);
tft.fillCircle(280, 0, 25, YELLOW);
tft.fillRect(21, 21, 79, 80, BLACK);
tft.fillRect(121, 21, 79, 80, BLACK);

tft.drawCircle(65, 170, 40, WHITE);
tft.drawCircle(175, 170, 40, WHITE);
tft.fillRect(10, 110, 200, 30, BLACK);
tft.fillRect(10, 200, 200, 40, BLACK); 
  tft.fillCircle(100, 240, 30, CYAN);

tft.drawRect(20, 20, 80, 81, WHITE);
tft.drawRect(120, 20, 80, 81, WHITE);
tft.setCursor(220, 35);
tft.setTextColor(RED);
tft.setTextSize(1);
tft.println("PWM5 :");
tft.setCursor(220, 55);
tft.println("PWM6 :");
tft.setCursor(220, 75);
tft.println("PWM9 :");
tft.setCursor(215, 95);
tft.println("PWM10 :");

tft.setTextColor(GREEN3);
tft.setCursor(45, 150);
tft.println("Angle X");
tft.setCursor(155, 150);
tft.println("Angle Y");

tft.fillRoundRect(245, 180, 55, 40, 3, RED);
tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
tft.setTextSize(1);
tft.setCursor(263, 195);
tft.setTextColor(CYAN);
tft.println("Back "); 
}

void menuA(){


if(infos[0]>179){
  infos[0] = 179;
}
 if(infos[1]>179){
  infos[1] = 179;
} 
if(infos[2]>179){
  infos[2] = 179;
}
 if(infos[3]>179){
  infos[3] = 179;
} 
  tft.fillCircle(xjoy1, yjoy1, 2, BLACK);
tft.fillCircle(xjoy2, yjoy2, 2, BLACK); 

 tft.fillRect(54, 170, 40, 10, BLACK);
 tft.fillRect(164, 170, 40, 10, BLACK);


tft.fillRect(259, 34, 24, 71, BLACK);

 xjoy1 = map(data[1], 0, 1024, 31, 89);
 yjoy1 = map(data[0], 1024, 0, 31, 89);
tft.fillCircle(xjoy1, yjoy1, 2, BLUE);
 xjoy2 = map(data[3], 0, 1024, 131, 189);
 yjoy2 = map(data[2], 1024, 0, 31, 89);
tft.fillCircle(xjoy2, yjoy2, 2, BLUE);  


tft.setTextColor(GREEN1);
tft.setCursor(60, 170);
tft.println(infos[4]);
tft.setCursor(170, 170);
tft.println(infos[5]);

tft.setCursor(260, 35);
tft.setTextColor(RED);
tft.setTextSize(1);
tft.println(infos[0]);
tft.setCursor(260, 55);
tft.println(infos[1]);
tft.setCursor(260, 75);
tft.println(infos[2]);
tft.setCursor(260, 95);
tft.println(infos[3]);
}


void menuBstatis(){
  tft.fillCircle(50, 0, 80, PINK1);

tft.fillCircle(240, 20, 130, ORANGE);

tft.fillRect(31, 31, 114, 115, BLACK);
  tft.fillRect(176, 31, 114, 115, BLACK);

tft.drawRect(30, 30, 115, 116, WHITE);
tft.drawRect(175, 30, 115, 116, WHITE);
tft.setCursor(40, 160);
tft.setTextColor(CYAN);
tft.setTextSize(1);
tft.println("X:");
tft.setCursor(40, 175);
tft.println("Y:");
tft.setCursor(160, 160);
tft.println("X:");
tft.setCursor(160, 175);
tft.println("Y:");
tft.fillRoundRect(39, 199, 62, 32, 3, WHITE);
tft.fillRoundRect(139, 199, 62, 32, 3, WHITE);

if(data[4]==1){
  tft.fillRoundRect(41, 201, 58, 28, 3, GREEN);
}
else{
    tft.fillRoundRect(41, 201, 58, 28, 3, RED);
}

if(data[5]==1){
  tft.fillRoundRect(141, 201, 58, 28, 3, GREEN);
}
else{
    tft.fillRoundRect(141, 201, 58, 28, 3, RED);
}
    
tft.fillRoundRect(245, 180, 55, 40, 3, RED);
tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
tft.setTextSize(1);
tft.setCursor(263, 195);
tft.setTextColor(CYAN);
tft.println("Back "); 
}

void menuB(){
tft.drawCircle(xjoy2, yjoy2, 4, BLACK);
tft.drawCircle(xjoy1, yjoy1, 4, BLACK);
tft.fillRect(54, 159, 26, 25, BLACK);
tft.fillRect(174, 159, 26, 25, BLACK);


 xjoy1 = map(data[1], 0, 1024, 41, 134);
 yjoy1 = map(data[0], 1024, 0, 41, 134);
tft.drawCircle(xjoy1, yjoy1, 4, BLUE);
 xjoy2 = map(data[3], 0, 1024, 186, 279);
 yjoy2 = map(data[2], 1024, 0, 41, 134);
tft.drawCircle(xjoy2, yjoy2, 4, BLUE);
tft.setCursor(55, 160);
tft.setTextColor(CYAN);
tft.setTextSize(1);
tft.println(data[1]); 
tft.setCursor(55, 175);
tft.println(data[0]); 
tft.setCursor(175, 160);
tft.println(data[3]); 
tft.setCursor(175, 175);
tft.println(data[2]); 

if(timer<=100){
timer++;
}
if(p.z > ts.pressureThreshhold){
if(p.x>700 && p.y<300 && data[4]==1 && timer>=5){
  data[4] = 0;
  tft.fillRoundRect(41, 201, 58, 28, 3, RED);
  timer = 0;
}else{}
if(p.x>700 && p.y<300 && data[4]==0 && timer>=5){
   data[4] = 1;
   tft.fillRoundRect(41, 201, 58, 28, 3, GREEN);
     timer = 0;
}else{}
if(p.x<700 && p.y<300 && data[5]==1 && timer>=5){
  data[5] = 0;
  tft.fillRoundRect(141, 201, 58, 28, 3, RED);
  timer = 0;
}else{}
if(p.x<700 && p.y<300 && data[5]==0 && timer>=5){
  data[5] = 1;
  tft.fillRoundRect(141, 201, 58, 28, 3, GREEN);
    timer = 0;
}else{}
}

}

void menuCstatis(){
 tft.setCursor(10, 20);
tft.setTextColor(RED);
tft.setTextSize(1);
tft.println("This template must be created on the code."); 
 tft.setCursor(10, 40);
tft.println("For more informations, check up :"); 
tft.setCursor(10, 60);
tft.println("Hackster.io /https://www.hackster.io/MisterBotBreak"); 

tft.fillRoundRect(245, 180, 55, 40, 3, RED);
tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
tft.setTextSize(1);
tft.setCursor(263, 195);
tft.setTextColor(CYAN);
tft.println("Back "); 
}

void menuC(){
}

void menuDstatis(){
  tft.setCursor(10, 20);
tft.setTextColor(RED);
tft.setTextSize(1);
tft.println("This template must be created through the code."); 
 tft.setCursor(10, 40);
tft.println("For more informations, check up :"); 
tft.setCursor(10, 60);
tft.println("Hackster.io /https://www.hackster.io/MisterBotBreak"); 

tft.fillRoundRect(245, 180, 55, 40, 3, RED);
tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
tft.setTextSize(1);
tft.setCursor(263, 195);
tft.setTextColor(CYAN);
tft.println("Back "); 
}

void menuD(){
}



void menuCharactsstatis(){

tft.fillScreen(BLACK);

tft.fillCircle(320, 0, 150, BLUE);
tft.fillCircle(0, 160, 80, GREEN);
tft.fillCircle(140, 240, 50, RED);

tft.setCursor(60, 20);
tft.setTextColor(CYAN);
tft.setTextSize(1);
tft.println("Remote Controller Characteristics"); 
tft.setCursor(10, 50);
tft.setTextColor(WHITE);
tft.println("This controller transmits data using a"); 
tft.setCursor(245, 50);
tft.setTextColor(RED);
tft.println("nRF24l01"); 
tft.setCursor(10, 65);
tft.setTextColor(RED);
tft.println("radio module."); 
tft.setCursor(10, 90);
tft.setTextColor(WHITE);
tft.println("Packet sent : data, array (6 * int) ");
tft.setCursor(10, 105);
tft.println("Packet received : infos, array (6 * int) "); 

tft.setCursor(10, 125);
tft.println("By default, infos[0,1,2,3] are associated with PWMs"); 
tft.setCursor(10, 140);
tft.println("while infos[4,5] are angles (gyroscope)."); 
tft.setCursor(10, 155);
tft.println("Template A is especially for drone driving."); 

 tft.setCursor(10, 175);
tft.println("CE : D10");
tft.setCursor(10, 190);
tft.println("CSN : D11"); 
 tft.setCursor(100, 175);
tft.println("MOSI : D51");
tft.setCursor(100, 190);
tft.println("MISO : D50"); 
tft.setCursor(10, 205);
tft.println("SCK : D52"); 


tft.fillRoundRect(245, 180, 55, 40, 3, RED);
tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
tft.setTextSize(1);
tft.setCursor(263, 195);
tft.setTextColor(CYAN);
tft.println("Back ");  


}


void menuCharacts(){
  
}

void menuXstatis(){
  //put all static elements of your patern here
}

void menuX(){
  //put all dynamic elements of your patern here
}

void startingmenustatis(){
  tft.drawRoundRect(20, 60, 130, 40, 3, WHITE);
  tft.drawRoundRect(170, 60, 130, 40, 3, WHITE);
  tft.drawRoundRect(20, 120, 130, 40, 3, WHITE);
  tft.drawRoundRect(170, 120, 130, 40, 3, WHITE);
  tft.drawRoundRect(20, 180, 205, 40, 3, WHITE);
  tft.setCursor(18, 20);
  tft.setTextColor(CYAN);
  tft.setTextSize(1);
  tft.println("Choose your template for this remote controler !");

  tft.setCursor(51, 67);
  tft.setTextColor(RED);
  tft.println("Template A"); 
  tft.setCursor(53, 85);
  tft.println("(standart)");
  tft.setCursor(203, 75);
  tft.setTextColor(ORANGE);
  tft.println("Template B"); 
  tft.setCursor(51, 135);
  tft.setTextColor(YELLOW);
  tft.println("Template C"); 
  tft.setCursor(203, 135);
  tft.setTextColor(GREEN);
  tft.println("Template D"); 
  tft.setCursor(51, 195);
  tft.setTextColor(MAGENTA);
  tft.println("Technical Characteristics"); 

tft.fillRoundRect(245, 180, 55, 40, 3, RED);
tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
tft.setTextSize(1);
tft.setCursor(263, 195);
tft.setTextColor(CYAN);
tft.println("Back "); 
}


    
void loop(){

 data[0] = analogRead(A8);
  data[1] = analogRead(A9);
  data[2] = analogRead(A10);
  data[3] = analogRead(A11);
    

digitalWrite(13, HIGH);
  p = ts.getPoint();
 digitalWrite(13, LOW);
 pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);


if(startingmenuvariable==0 && menu==0){
  startingmenustatis();
  startingmenuvariable = 1;
}
 if(menu > 0 && p.x < 350 && p.y < 500){
  tft.fillScreen(BLACK);
  startingmenustatis();
  menu = 0;
  menulaunch = 0;
 }
 if(p.z > ts.pressureThreshhold){
  if(menu==0 || menulaunch==0){
  if(p.y>550 && p.x>600){
    menu = 1;
  }
  if(p.y>550 && p.x<600){
    menu = 2;
  }
  if(550>p.y && p.y>340 && p.x>600){
    menu = 3;
  }
  if(550>p.y && p.y>340 && p.x<600){
    menu = 4;
  }
  if(p.y<340 && p.x>400){
    menu = 5;
  }
  }


 }

 Serial.println(data[4]);


 if(menu==1 && menulaunch != 1){
  tft.fillScreen(BLACK);
  menuAstatis();
  menulaunch = 1;
 }
 if(menu==1){
  menuA();
 }
 if(menu==2 && menulaunch != 2){
  tft.fillScreen(BLACK);
  menuBstatis();
  menulaunch = 2;
 }
 if(menu==2){
  menuB();
 }
  if(menu==3 && menulaunch != 3){
  tft.fillScreen(BLACK);
  menuCstatis();
  menulaunch = 3;
 }
 if(menu==3){
  menuC();
 }
  if(menu==4 && menulaunch != 4){
  tft.fillScreen(BLACK);
  menuDstatis();
  menulaunch = 4;
 }
 if(menu==4){
  menuD();
 }
if(menu==5 && menulaunch != 5){
  tft.fillScreen(BLACK);
  menuCharactsstatis();
  menulaunch = 5;
 }
 if(menu==5){
  menuCharacts();
 }


 /*PROGRAM YOUR OWN PATERN HERE : X = the patern' letter / x = the patern' number
  
   if(menu==x && menulaunch != x){
  tft.fillScreen(BLACK);
  menuBstatis();
  menulaunch = x;
 }
 if(menu==x){
  menuX();
 }
  */


 data[0] = analogRead(A8);
  data[1] = analogRead(A9);
  data[2] = analogRead(A10);
  data[3] = analogRead(A11);
 

 if(Mirf.dataReady()){
    Mirf.getData((byte *) &infos); 
    
    Serial.println(infos[0]); 
    Serial.println(infos[1]); 
    Serial.println(infos[2]); 
    Serial.println(infos[3]); 
   Serial.println(infos[4]); 
    Serial.println(infos[5]); 
  
  }


    delay(10);
    
Mirf.send((byte *) &data); 
  while(Mirf.isSending()); 




}

Remote Control #2 (Avoid lag Update + Plane & Submarine Update)

Arduino
Modified to allow usage of submarines and planes.
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#include <SPI.h>
#include <RF24.h>
#include <Elegoo_GFX.h>
#include <Elegoo_TFTLCD.h>
#include <TouchScreen.h>


#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4


#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFA60
#define LIME 0x07FF
#define ORANGE1 ((255 / 8) << 11) | ((165 / 4) << 5) | (0 / 8)
#define ORANGE2 ((255 / 8) << 11) | ((187 / 4) << 5) | (61 / 8)
#define ORANGE3 ((255 / 8) << 11) | ((201 / 4) << 5) | (102 / 8)
#define BLUE1 ((2 / 8) << 11) | ((9 / 4) << 5) | (219 / 8)
#define BLUE2 ((36 / 8) << 11) | ((43 / 4) << 5) | (253 / 8)
#define BLUE3 ((103 / 8) << 11) | ((108 / 4) << 5) | (254 / 8)
#define DARKBLUE1 ((2 / 8) << 11) | ((8 / 4) << 5) | (185 / 8)
#define DARKBLUE2 ((1 / 8) << 11) | ((5 / 4) << 5) | (118 / 8)
#define RED1 ((255 / 8) << 11) | ((0 / 4) << 5) | (0 / 8)
#define RED2 ((255 / 8) << 11) | ((61 / 4) << 5) | (61 / 8)
#define RED3 ((255 / 8) << 11) | ((122 / 4) << 5) | (122 / 8)
#define CACTUS1 ((196 / 8) << 11) | ((235 / 4) << 5) | (20 / 8)
#define CACTUS2 ((217 / 8) << 11) | ((242 / 4) << 5) | (103 / 8)
#define CACTUS3 ((231 / 8) << 11) | ((247 / 4) << 5) | (158 / 8)
#define YELLOW1 ((235 / 8) << 11) | ((254 / 4) << 5) | (1 / 8)
#define YELLOW2 ((239 / 8) << 11) | ((254 / 4) << 5) | (62 / 8)
#define YELLOW3 ((244 / 8) << 11) | ((254 / 4) << 5) | (123 / 8)
#define MAGENTA1 ((245 / 8) << 11) | ((0 / 4) << 5) | (245 / 8)
#define MAGENTA2 ((255 / 8) << 11) | ((82 / 4) << 5) | (255 / 8)
#define MAGENTA3 ((255 / 8) << 11) | ((184 / 4) << 5) | (255 / 8)
#define CYAN1 ((0 / 8) << 11) | ((255 / 4) << 5) | (255 / 8)
#define CYAN2 ((122 / 8) << 11) | ((255 / 4) << 5) | (255 / 8)
#define CYAN3 ((204 / 8) << 11) | ((255 / 4) << 5) | (255 / 8)
#define PINK1 ((230 / 8) << 11) | ((7 / 4) << 5) | (92 / 8)
#define PINK2 ((249 / 8) << 11) | ((60 / 4) << 5) | (132 / 8)
#define PINK3 ((251 / 8) << 11) | ((131 / 4) << 5) | (177 / 8)
#define GREEN1 ((15 / 8) << 11) | ((186 / 4) << 5) | (15 / 8)
#define GREEN2 ((36 / 8) << 11) | ((238 / 4) << 5) | (36 / 8)
#define GREEN3 ((103 / 8) << 11) | ((243 / 4) << 5) | (103 / 8)
#define GREY1 ((128 / 8) << 11) | ((128 / 4) << 5) | (128 / 8)
#define GREY2 ((168 / 8) << 11) | ((168 / 4) << 5) | (168 / 8)
#define GREY3 ((200 / 8) << 11) | ((200 / 4) << 5) | (200 / 8)
#define PURPLE1 ((92 / 8) << 11) | ((0 / 4) << 5) | (92 / 8)
#define PURPLE2 ((133 / 8) << 11) | ((0 / 4) << 5) | (133 / 8)
#define PURPLE3 ((173 / 8) << 11) | ((0 / 4) << 5) | (173 / 8)
#define BROWN1 ((41 / 8) << 11) | ((10 / 4) << 5) | (10 / 8)
#define BROWN2 ((73 / 8) << 11) | ((19 / 4) << 5) | (19 / 8)
#define BROWN3 ((106 / 8) << 11) | ((27 / 4) << 5) | (27 / 8)
#define BROWN4 ((138 / 8) << 11) | ((35 / 4) << 5) | (35 / 8)
#define BROWN5 ((171 / 8) << 11) | ((43 / 4) << 5) | (43 / 8)
#define BROWN6 ((203 / 8) << 11) | ((52 / 4) << 5) | (52 / 8)
#define BROWN7 ((212 / 8) << 11) | ((84 / 4) << 5) | (84 / 8)
#define BEIGE1 ((221 / 8) << 11) | ((221 / 4) << 5) | (136 / 8)
#define BEIGE2 ((230 / 8) << 11) | ((230 / 4) << 5) | (168 / 8)
#define BEIGE3 ((235 / 8) << 11) | ((235 / 4) << 5) | (184 / 8)


#define YP A2
#define XM A3
#define YM 8
#define XP 9

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 400);
Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

TSPoint p;

int data[6];
int infos[6];

int xjoy1, yjoy1, xjoy2, yjoy2;
float angley1 = 0;
float angley2 = 0;

short menu = 0;
short startingmenuvariable = 0;
short menulaunch = 0;

int timer = 0;
int timer2 = 0;
int delaybutton4, delaybutton5;
int avoidlag = 0;

double connecterror = 0;
double turn = 0;
double connectionvalue = 0;

//template C
int flaps = 0;
int pwmthrust = 0;

//template D
int dataOldValues = 0;


void setup() {
  Serial.begin(9600);

  tft.reset();
  tft.begin(0x9341);
  tft.fillScreen(BLACK);
  tft.setRotation(3);

  Mirf.cePin = 10;
  Mirf.csnPin = 11;
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();

  Mirf.channel = 1;
  Mirf.payload = sizeof(int) * 6;
  Mirf.config();

  Mirf.setTADDR((byte *)"nrf02");
  Mirf.setRADDR((byte *)"nrf01");

  data[4] = 0;
  data[5] = 0;
}


void menuAstatis() {
  tft.fillCircle(40, 0, 100, MAGENTA);
  tft.fillCircle(280, 0, 25, YELLOW);
  tft.fillRect(21, 21, 79, 80, BLACK);
  tft.fillRect(121, 21, 79, 80, BLACK);

  tft.drawCircle(65, 170, 40, WHITE);
  tft.drawCircle(175, 170, 40, WHITE);
  tft.fillRect(10, 110, 200, 30, BLACK);
  tft.fillRect(10, 200, 200, 40, BLACK);
  tft.fillCircle(100, 240, 30, CYAN);

  tft.drawRect(20, 20, 80, 81, WHITE);
  tft.drawRect(120, 20, 80, 81, WHITE);
  tft.setCursor(220, 35);
  tft.setTextColor(RED);
  tft.setTextSize(1);
  tft.println("PWM5 :");
  tft.setCursor(220, 55);
  tft.println("PWM6 :");
  tft.setCursor(220, 75);
  tft.println("PWM9 :");
  tft.setCursor(215, 95);
  tft.println("PWM10 :");

  tft.setTextColor(GREEN3);
  tft.setCursor(45, 150);
  tft.println("Angle X");
  tft.setCursor(155, 150);
  tft.println("Angle Y");

  if (avoidlag == 0) {
    tft.fillRoundRect(245, 120, 55, 40, 3, RED);
    tft.fillRoundRect(248, 123, 49, 34, 3, BLACK);
  } else {
    tft.fillRoundRect(245, 120, 55, 40, 3, GREEN);
    tft.fillRoundRect(248, 123, 49, 34, 3, BLACK);
  }

  tft.fillRoundRect(245, 180, 55, 40, 3, RED);
  tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
  tft.setTextSize(1);
  tft.setCursor(263, 195);
  tft.setTextColor(CYAN);
  tft.println("Back ");
}

void menuA() {


  if (infos[0] > 179) {
    infos[0] = 179;
  }
  if (infos[1] > 179) {
    infos[1] = 179;
  }
  if (infos[2] > 179) {
    infos[2] = 179;
  }
  if (infos[3] > 179) {
    infos[3] = 179;
  }
  tft.fillCircle(xjoy1, yjoy1, 2, BLACK);
  tft.fillCircle(xjoy2, yjoy2, 2, BLACK);

  tft.fillRect(54, 170, 20, 10, BLACK);
  tft.fillRect(164, 170, 20, 10, BLACK);
  tft.fillRect(259, 34, 24, 71, BLACK);

  xjoy1 = map(data[1], 0, 1024, 31, 89);
  yjoy1 = map(data[0], 1024, 0, 31, 89);
  tft.fillCircle(xjoy1, yjoy1, 2, BLUE);
  xjoy2 = map(data[3], 0, 1024, 131, 189);
  yjoy2 = map(data[2], 1024, 0, 31, 89);
  tft.fillCircle(xjoy2, yjoy2, 2, BLUE);

  timer2++;
  if (timer2 > 30) {
    timer2 = 20;
  }

  if (p.z > ts.pressureThreshhold && p.x < 350 && p.y > 300) {
    if (avoidlag == 1 && timer2 >= 20) {
      avoidlag = 0;
      tft.fillRect(49, 119, 140, 10, BLACK);
      tft.fillRoundRect(245, 120, 55, 40, 3, RED);
      tft.fillRoundRect(248, 123, 49, 34, 3, BLACK);
      timer2 = 0;
    }
    if (avoidlag == 0 && timer2 >= 20) {
      avoidlag = 1;
      tft.setTextSize(1);
      tft.setCursor(50, 120);
      tft.setTextColor(RED3);
      tft.println("[Avoid lag] activated !");
      tft.fillRoundRect(245, 120, 55, 40, 3, GREEN);
      tft.fillRoundRect(248, 123, 49, 34, 3, BLACK);
      timer2 = 0;
    }
  }

  if (avoidlag == 0) {
    tft.setTextColor(GREEN1);
    tft.setCursor(60, 170);
    tft.println(infos[4]);
    tft.setCursor(170, 170);
    tft.println(infos[5]);

    tft.setCursor(260, 35);
    tft.setTextColor(RED);
    tft.setTextSize(1);
    tft.println(infos[0]);
    tft.setCursor(260, 55);
    tft.println(infos[1]);
    tft.setCursor(260, 75);
    tft.println(infos[2]);
    tft.setCursor(260, 95);
    tft.println(infos[3]);
  } else if (avoidlag == 1) {
  }
}


void menuBstatis() {
  tft.fillCircle(50, 0, 80, PINK1);

  tft.fillCircle(240, 20, 130, ORANGE);

  tft.fillRect(31, 31, 114, 115, BLACK);
  tft.fillRect(176, 31, 114, 115, BLACK);

  tft.drawRect(30, 30, 115, 116, WHITE);
  tft.drawRect(175, 30, 115, 116, WHITE);
  tft.setCursor(40, 160);
  tft.setTextColor(CYAN);
  tft.setTextSize(1);
  tft.println("X:");
  tft.setCursor(40, 175);
  tft.println("Y:");
  tft.setCursor(160, 160);
  tft.println("X:");
  tft.setCursor(160, 175);
  tft.println("Y:");
  tft.fillRoundRect(39, 199, 62, 32, 3, WHITE);
  tft.fillRoundRect(139, 199, 62, 32, 3, WHITE);

  if (data[4] == 1) {
    tft.fillRoundRect(41, 201, 58, 28, 3, GREEN);
  } else {
    tft.fillRoundRect(41, 201, 58, 28, 3, RED);
  }

  if (data[5] == 1) {
    tft.fillRoundRect(141, 201, 58, 28, 3, GREEN);
  } else {
    tft.fillRoundRect(141, 201, 58, 28, 3, RED);
  }

  tft.fillRoundRect(245, 180, 55, 40, 3, RED);
  tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
  tft.setTextSize(1);
  tft.setCursor(263, 195);
  tft.setTextColor(CYAN);
  tft.println("Back ");
}

void menuB() {
  tft.drawCircle(xjoy2, yjoy2, 4, BLACK);
  tft.drawCircle(xjoy1, yjoy1, 4, BLACK);
  tft.fillRect(54, 159, 26, 25, BLACK);
  tft.fillRect(174, 159, 26, 25, BLACK);


  xjoy1 = map(data[1], 0, 1024, 41, 134);
  yjoy1 = map(data[0], 1024, 0, 41, 134);
  tft.drawCircle(xjoy1, yjoy1, 4, BLUE);
  xjoy2 = map(data[3], 0, 1024, 186, 279);
  yjoy2 = map(data[2], 1024, 0, 41, 134);
  tft.drawCircle(xjoy2, yjoy2, 4, BLUE);
  tft.setCursor(55, 160);
  tft.setTextColor(CYAN);
  tft.setTextSize(1);
  tft.println(data[1]);
  tft.setCursor(55, 175);
  tft.println(data[0]);
  tft.setCursor(175, 160);
  tft.println(data[3]);
  tft.setCursor(175, 175);
  tft.println(data[2]);


  if (timer <= 100) {
    timer++;
  }
  if (p.z > ts.pressureThreshhold) {
    if (p.x > 700 && p.y < 300 && data[4] == 1 && timer >= 5) {
      data[4] = 0;
      tft.fillRoundRect(41, 201, 58, 28, 3, RED);
      timer = 0;
    } else {
    }
    if (p.x > 700 && p.y < 300 && data[4] == 0 && timer >= 5) {
      data[4] = 1;
      tft.fillRoundRect(41, 201, 58, 28, 3, GREEN);
      timer = 0;
    } else {
    }
    if (p.x < 700 && p.y < 300 && data[5] == 1 && timer >= 5) {
      data[5] = 0;
      tft.fillRoundRect(141, 201, 58, 28, 3, RED);
      timer = 0;
    } else {
    }
    if (p.x < 700 && p.y < 300 && data[5] == 0 && timer >= 5) {
      data[5] = 1;
      tft.fillRoundRect(141, 201, 58, 28, 3, GREEN);
      timer = 0;
    } else {
    }
  }
}

void menuCstatis() {

  tft.fillRoundRect(245, 180, 55, 40, 3, RED);
  tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
  tft.setTextSize(1);
  tft.setCursor(263, 195);
  tft.setTextColor(CYAN);
  tft.println("Back ");


  tft.drawRoundRect(245, 30, 55, 30, 7, WHITE);
  tft.drawRoundRect(245, 60, 55, 30, 7, WHITE);
  tft.drawRoundRect(245, 90, 55, 30, 7, WHITE);
  tft.drawRoundRect(245, 120, 55, 30, 7, WHITE);
  tft.fillRect(255, 58, 35, 70, BLACK);
  tft.setTextSize(1);
  tft.setTextColor(WHITE);
  tft.setCursor(270, 42);
  tft.println("0");
  tft.setCursor(270, 72);
  tft.println("1");
  tft.setCursor(270, 102);
  tft.println("2");
  tft.setCursor(270, 132);
  tft.println("3");

  tft.setTextSize(1);
  tft.setTextColor(YELLOW);
  tft.setCursor(260, 10);
  tft.println("Flaps");

  tft.drawRoundRect(160, 30, 60, 40, 5, WHITE);
  tft.setCursor(172, 13);
  tft.println("Thrust");

  tft.drawRoundRect(160, 100, 60, 40, 5, WHITE);
  tft.setCursor(179, 83);
  tft.println("Roll");

  tft.drawRoundRect(160, 170, 60, 40, 5, WHITE);
  tft.setCursor(176, 153);
  tft.println("Pitch");
  
  
  tft.drawRoundRect(30, 180, 60, 40, 5, WHITE);
  tft.setCursor(30, 162);
  tft.println("Connection");

  tft.drawRect(30, 30, 110, 110, WHITE);
}

void menuC() {

  tft.fillCircle(xjoy1, yjoy1, 2, BLACK);
  tft.fillRect(181, 44, 18, 8, BLACK);
  tft.fillRect(181, 114, 18, 8, BLACK);
  tft.fillRect(181, 184, 18, 8, BLACK);
  tft.fillRect(49, 195, 25, 8, BLACK);

  if (p.z > ts.pressureThreshhold) {
    if (p.x < 430 && p.x > 280) {
      if (p.y > 720 && p.y < 830) {
        flaps = 0;
        tft.drawRoundRect(249, 124, 47, 23, 5, BLACK);
        tft.drawRoundRect(249, 94, 47, 23, 5, BLACK);
        tft.drawRoundRect(249, 64, 47, 23, 5, BLACK);
        tft.drawRoundRect(249, 34, 47, 23, 5, BLUE3);
      }
      if (p.y > 610 && p.y < 719) {
        flaps = 1;
        tft.drawRoundRect(249, 124, 47, 23, 5, BLACK);
        tft.drawRoundRect(249, 94, 47, 23, 5, BLACK);
        tft.drawRoundRect(249, 34, 47, 23, 5, BLACK);
        tft.drawRoundRect(249, 64, 47, 23, 5, BLUE3);
      }
      if (p.y > 500 && p.y < 609) {
        flaps = 2;
        tft.drawRoundRect(249, 124, 47, 23, 5, BLACK);
        tft.drawRoundRect(249, 64, 47, 23, 5, BLACK);
        tft.drawRoundRect(249, 34, 47, 23, 5, BLACK);
        tft.drawRoundRect(249, 94, 47, 23, 5, BLUE3);
      }
      if (p.y > 380 && p.y < 499) {
        flaps = 3;
        tft.drawRoundRect(249, 34, 47, 23, 5, BLACK);
        tft.drawRoundRect(249, 64, 47, 23, 5, BLACK);
        tft.drawRoundRect(249, 94, 47, 23, 5, BLACK);
        tft.drawRoundRect(249, 124, 47, 23, 5, BLUE3);
      }
    }
  }

  xjoy1 = map(data[1], 0, 1024, 33, 137);
  yjoy1 = map(data[0], 1024, 0, 33, 136);
  tft.fillCircle(xjoy1, yjoy1, 2, BLUE);

  tft.setTextColor(YELLOW);
  tft.setCursor(182, 45);
  tft.println(infos[3]);  //pwmthrust infos[0]
  tft.setCursor(182, 115);
  tft.println(infos[4]);  //Roll infos[1]
  tft.setCursor(182, 185);
  tft.println(infos[5]);  // Pitch infos[2]
  if(connectionvalue>=0.75){
  tft.setTextColor(GREEN);
  }
  if(connectionvalue<0.75 && connectionvalue>=0.40){
  tft.setTextColor(YELLOW);
  }
  if(connectionvalue<0.40){
  tft.setTextColor(RED);
  }
  tft.setCursor(50, 196);
  tft.println(connectionvalue);  // Pitch infos[2]

  data[3] = 0;
  data[4] = flaps;
  data[5] = 0;
}

void menuDstatis() {
   tft.fillRoundRect(245, 180, 55, 40, 3, RED);
  tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
  tft.setTextSize(1);
  tft.setCursor(263, 195);
  tft.setTextColor(CYAN);
  tft.println("Back ");

tft.drawRoundRect(30, 30, 150, 60, 3, WHITE);
tft.drawRoundRect(30, 110, 150, 20, 3, WHITE);
tft.setTextSize(1);
 
  tft.setTextColor(YELLOW);
  tft.drawRoundRect(30, 170, 60, 40, 5, WHITE);
  tft.setCursor(30, 152);
  tft.println("Connection");

  tft.drawRoundRect(250, 30, 20, 88, 2, WHITE);

 
}

void menuD() {


tft.fillCircle(map(data[1], 0, 1024, 34, 176), 120, 2, BLACK);
tft.fillCircle(map(data[1], 0, 1024, 34, 176), 120, 2, CYAN);

  data[1] = map(data[1], 0, 1024, -5, 5);//horizontal
  data[0] = map(data[0], 0, 1024, -5, 5);//vertical
  data[2] = map(data[2], 0, 1024, 2, -3);
if(dataOldValues!=data[2]){
 switch(dataOldValues){
    case -2 : 
    tft.fillRoundRect(252, 32, 16, 15, 2, BLACK); break;
    case -1 : 
    tft.fillRoundRect(252, 49, 16, 15, 2, BLACK); break;
    case 0 : 
    tft.fillRoundRect(252, 66, 16, 15, 2, BLACK); break;
    case 1 : 
    tft.fillRoundRect(252, 83, 16, 15, 2, BLACK); break;
    case 2 : 
    tft.fillRoundRect(252, 100, 16, 15, 2, BLACK); break;
    default:
    break;
  }

  switch(data[2]){
    case -2 : 
    tft.fillRoundRect(252, 32, 16, 15, 2, CYAN); break;
    case -1 : 
    tft.fillRoundRect(252, 49, 16, 15, 2, CYAN); break;
    case 0 : 
    tft.fillRoundRect(252, 66, 16, 15, 2, CYAN); break;
    case 1 : 
    tft.fillRoundRect(252, 83, 16, 15, 2, CYAN); break;
    case 2 : 
    tft.fillRoundRect(252, 100, 16, 15, 2, CYAN); break;
    default : 
    break;
  }
}






dataOldValues = data[2];
}



void menuCharactsstatis() {

  tft.fillScreen(BLACK);

  tft.fillCircle(320, 0, 150, BLUE);
  tft.fillCircle(0, 160, 80, GREEN);
  tft.fillCircle(140, 240, 50, RED);

  tft.setCursor(60, 20);
  tft.setTextColor(CYAN);
  tft.setTextSize(1);
  tft.println("Remote Controller Characteristics");
  tft.setCursor(10, 50);
  tft.setTextColor(WHITE);
  tft.println("This controller transmits data using a");
  tft.setCursor(245, 50);
  tft.setTextColor(RED);
  tft.println("nRF24l01");
  tft.setCursor(10, 65);
  tft.setTextColor(RED);
  tft.println("radio module.");
  tft.setCursor(10, 90);
  tft.setTextColor(WHITE);
  tft.println("Packet sent : data, array (6 * int) ");
  tft.setCursor(10, 105);
  tft.println("Packet received : infos, array (6 * int) ");

  tft.setCursor(10, 125);
  tft.println("By default, infos[0,1,2,3] are associated with PWMs");
  tft.setCursor(10, 140);
  tft.println("while infos[4,5] are angles (gyroscope).");
  tft.setCursor(10, 155);
  tft.println("Template A is especially for drone driving.");

  tft.setCursor(10, 175);
  tft.println("CE : D10");
  tft.setCursor(10, 190);
  tft.println("CSN : D11");
  tft.setCursor(100, 175);
  tft.println("MOSI : D51");
  tft.setCursor(100, 190);
  tft.println("MISO : D50");
  tft.setCursor(10, 205);
  tft.println("SCK : D52");


  tft.fillRoundRect(245, 180, 55, 40, 3, RED);
  tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
  tft.setTextSize(1);
  tft.setCursor(263, 195);
  tft.setTextColor(CYAN);
  tft.println("Back ");
}


void menuCharacts() {
}

void menuXstatis() {
  //put all static elements of your patern here
}

void menuX() {
  //put all dynamic elements of your patern here
}

void startingmenustatis() {
  tft.drawRoundRect(20, 60, 130, 40, 3, WHITE);
  tft.drawRoundRect(170, 60, 130, 40, 3, WHITE);
  tft.drawRoundRect(20, 120, 130, 40, 3, WHITE);
  tft.drawRoundRect(170, 120, 130, 40, 3, WHITE);
  tft.drawRoundRect(20, 180, 205, 40, 3, WHITE);
  tft.setCursor(18, 20);
  tft.setTextColor(CYAN);
  tft.setTextSize(1);
  tft.println("Choose your template for this remote controler !");

  tft.setCursor(51, 67);
  tft.setTextColor(RED);
  tft.println("Template A");
  tft.setCursor(53, 85);
  tft.println("(standart)");
  tft.setCursor(203, 75);
  tft.setTextColor(ORANGE);
  tft.println("Template B");
  tft.setCursor(51, 135);
  tft.setTextColor(YELLOW);
  tft.println("Template C");
  tft.setCursor(203, 135);
  tft.setTextColor(GREEN);
  tft.println("Template D");
  tft.setCursor(51, 195);
  tft.setTextColor(MAGENTA);
  tft.println("Technical Characteristics");

  tft.fillRoundRect(245, 180, 55, 40, 3, RED);
  tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
  tft.setTextSize(1);
  tft.setCursor(263, 195);
  tft.setTextColor(CYAN);
  tft.println("Back ");
}



void loop() {

  data[0] = analogRead(A8);
  data[1] = analogRead(A9);
  data[2] = analogRead(A10);
  data[3] = analogRead(A11);



  digitalWrite(13, HIGH);
  p = ts.getPoint();
  digitalWrite(13, LOW);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);


  if (startingmenuvariable == 0 && menu == 0) {
    startingmenustatis();
    startingmenuvariable = 1;
  }
  if (menu > 0 && p.x < 350 && p.y < 300) {
    tft.fillScreen(BLACK);
    startingmenustatis();
    menu = 0;
    menulaunch = 0;
  }



  if (p.z > ts.pressureThreshhold) {
    if (menu == 0 || menulaunch == 0) {
      if (p.y > 550 && p.x > 600) {
        menu = 1;
      }
      if (p.y > 550 && p.x < 600) {
        menu = 2;
      }
      if (550 > p.y && p.y > 340 && p.x > 600) {
        menu = 3;
      }
      if (550 > p.y && p.y > 340 && p.x < 600) {
        menu = 4;
      }
      if (p.y < 340 && p.x > 400) {
        menu = 5;
      }
    }
  }


  if (menu == 1 && menulaunch != 1) {
    tft.fillScreen(BLACK);
    menuAstatis();
    menulaunch = 1;
  }
  if (menu == 1) {
    menuA();
  }
  if (menu == 2 && menulaunch != 2) {
    tft.fillScreen(BLACK);
    menuBstatis();
    menulaunch = 2;
  }
  if (menu == 2) {
    menuB();
  }
  if (menu == 3 && menulaunch != 3) {
    tft.fillScreen(BLACK);
    menuCstatis();
    menulaunch = 3;
  }
  if (menu == 3) {
    menuC();
  }
  if (menu == 4 && menulaunch != 4) {
    tft.fillScreen(BLACK);
    menuDstatis();
    menulaunch = 4;
  }
  if (menu == 4) {
    menuD();
  }
  if (menu == 5 && menulaunch != 5) {
    tft.fillScreen(BLACK);
    menuCharactsstatis();
    menulaunch = 5;
  }
  if (menu == 5) {
    menuCharacts();
  }


  /*PROGRAM YOUR OWN PATERN HERE : X = the patern' letter / x = the patern' number
  
   if(menu==x && menulaunch != x){
  tft.fillScreen(BLACK);
  menuBstatis();
  menulaunch = x;
 }
 if(menu==x){
  menuX();
 }
  */




  if (Mirf.dataReady()) {
    Mirf.getData((byte *)&infos);
   /* Serial.println(infos[0]);
    Serial.println(infos[1]);
    Serial.println(infos[2]);
    Serial.println(infos[3]);
    Serial.println(infos[4]);
    Serial.println(infos[5]);*/
  }
  else if(!Mirf.dataReady()){
  connecterror = connecterror + 1;
  }

  turn++;
  if(turn>=20.0){
  connectionvalue = 1-(connecterror/turn);
  turn=0;
  connecterror=0;
  Serial.println(connectionvalue);
  }
  delay(5);

  
  Mirf.send((byte *)&data);
  while (Mirf.isSending());
}

Remote Control #2 (Avoid lag Update)

Arduino
This new code has a button on Template A to delete lags due to text printing on TFT.
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#include <SPI.h>                                                     
#include <RF24.h>
#include <Elegoo_GFX.h>
#include <Elegoo_TFTLCD.h>
#include <TouchScreen.h>


#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4


#define  BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define ORANGE   0xFA60
#define LIME     0x07FF
#define ORANGE1   ((255 / 8) << 11) | ((165 / 4) << 5) | (0 / 8) 
#define ORANGE2   ((255 / 8) << 11) | ((187 / 4) << 5) | (61 / 8) 
#define ORANGE3   ((255 / 8) << 11) | ((201 / 4) << 5) | (102 / 8) 
#define BLUE1   ((2 / 8) << 11) | ((9 / 4) << 5) | (219 / 8)
#define BLUE2   ((36 / 8) << 11) | ((43 / 4) << 5) | (253 / 8)
#define BLUE3   ((103 / 8) << 11) | ((108 / 4) << 5) | (254 / 8)
#define DARKBLUE1   ((2 / 8) << 11) | ((8 / 4) << 5) | (185 / 8)
#define DARKBLUE2   ((1 / 8) << 11) | ((5 / 4) << 5) | (118 / 8)
#define RED1    ((255 / 8) << 11) | ((0 / 4) << 5) | (0 / 8)
#define RED2    ((255 / 8) << 11) | ((61 / 4) << 5) | (61 / 8)
#define RED3    ((255 / 8) << 11) | ((122 / 4) << 5) | (122 / 8)
#define CACTUS1 ((196 / 8) << 11) | ((235 / 4) << 5) | (20 / 8)
#define CACTUS2 ((217 / 8) << 11) | ((242 / 4) << 5) | (103 / 8)
#define CACTUS3 ((231 / 8) << 11) | ((247 / 4) << 5) | (158 / 8)
#define YELLOW1 ((235 / 8) << 11) | ((254 / 4) << 5) | (1 / 8)
#define YELLOW2 ((239 / 8) << 11) | ((254 / 4) << 5) | (62 / 8)
#define YELLOW3 ((244 / 8) << 11) | ((254 / 4) << 5) | (123 / 8)
#define MAGENTA1 ((245 / 8) << 11) | ((0 / 4) << 5) | (245 / 8)
#define MAGENTA2 ((255 / 8) << 11) | ((82 / 4) << 5) | (255 / 8)
#define MAGENTA3 ((255 / 8) << 11) | ((184 / 4) << 5) | (255 / 8)
#define CYAN1    ((0 / 8) << 11) | ((255 / 4) << 5) | (255 / 8)
#define CYAN2    ((122 / 8) << 11) | ((255 / 4) << 5) | (255 / 8)
#define CYAN3    ((204 / 8) << 11) | ((255 / 4) << 5) | (255 / 8)    
#define PINK1   ((230 / 8) << 11) | ((7 / 4) << 5) | (92 / 8)
#define PINK2   ((249 / 8) << 11) | ((60 / 4) << 5) | (132 / 8)
#define PINK3   ((251 / 8) << 11) | ((131 / 4) << 5) | (177 / 8)
#define GREEN1   ((15 / 8) << 11) | ((186 / 4) << 5) | (15 / 8)
#define GREEN2   ((36 / 8) << 11) | ((238 / 4) << 5) | (36 / 8)
#define GREEN3   ((103 / 8) << 11) | ((243 / 4) << 5) | (103 / 8)
#define GREY1   ((128 / 8) << 11) | ((128 / 4) << 5) | (128 / 8)
#define GREY2   ((168 / 8) << 11) | ((168 / 4) << 5) | (168 / 8)
#define GREY3   ((200 / 8) << 11) | ((200 / 4) << 5) | (200 / 8)
#define PURPLE1   ((92 / 8) << 11) | ((0 / 4) << 5) | (92 / 8)
#define PURPLE2   ((133 / 8) << 11) | ((0 / 4) << 5) | (133 / 8)
#define PURPLE3   ((173 / 8) << 11) | ((0 / 4) << 5) | (173 / 8)
#define BROWN1   ((41 / 8) << 11) | ((10 / 4) << 5) | (10 / 8)
#define BROWN2   ((73 / 8) << 11) | ((19 / 4) << 5) | (19 / 8)
#define BROWN3   ((106 / 8) << 11) | ((27 / 4) << 5) | (27 / 8)
#define BROWN4   ((138 / 8) << 11) | ((35 / 4) << 5) | (35 / 8)
#define BROWN5   ((171 / 8) << 11) | ((43 / 4) << 5) | (43 / 8)
#define BROWN6   ((203 / 8) << 11) | ((52 / 4) << 5) | (52 / 8)
#define BROWN7   ((212 / 8) << 11) | ((84 / 4) << 5) | (84 / 8)
#define BEIGE1   ((221 / 8) << 11) | ((221 / 4) << 5) | (136 / 8)
#define BEIGE2   ((230 / 8) << 11) | ((230 / 4) << 5) | (168 / 8)
#define BEIGE3   ((235 / 8) << 11) | ((235 / 4) << 5) | (184 / 8)


#define YP A2  
#define XM A3 
#define YM 8   
#define XP 9   

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 400);
Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

TSPoint p;
    
int data[6];  
int infos[6];

int xjoy1;
int yjoy1;
int xjoy2;
int yjoy2;
float angley1 = 0;
float angley2 = 0;

int menu = 0;
int startingmenuvariable = 0;
int menulaunch = 0;

int timer = 0;
int timer2 = 0;
int delaybutton4;
int delaybutton5;
int avoidlag = 0;

void setup(){       
Serial.begin(9600);   

tft.reset();
tft.begin(0x9341);
tft.fillScreen(BLACK);
tft.setRotation(3);

Mirf.cePin = 10; 
  Mirf.csnPin = 11; 
  Mirf.spi = &MirfHardwareSpi; 
  Mirf.init(); 

  Mirf.channel = 1; 
  Mirf.payload = sizeof(int) * 6; 
  Mirf.config(); 

  Mirf.setTADDR((byte *) "nrf02"); 
  Mirf.setRADDR((byte *) "nrf01"); 

data[4] = 0;
  data[5] = 0;

}


void menuAstatis(){
tft.fillCircle(40, 0, 100, MAGENTA);
tft.fillCircle(280, 0, 25, YELLOW);
tft.fillRect(21, 21, 79, 80, BLACK);
tft.fillRect(121, 21, 79, 80, BLACK);

tft.drawCircle(65, 170, 40, WHITE);
tft.drawCircle(175, 170, 40, WHITE);
tft.fillRect(10, 110, 200, 30, BLACK);
tft.fillRect(10, 200, 200, 40, BLACK); 
  tft.fillCircle(100, 240, 30, CYAN);

tft.drawRect(20, 20, 80, 81, WHITE);
tft.drawRect(120, 20, 80, 81, WHITE);
tft.setCursor(220, 35);
tft.setTextColor(RED);
tft.setTextSize(1);
tft.println("PWM5 :");
tft.setCursor(220, 55);
tft.println("PWM6 :");
tft.setCursor(220, 75);
tft.println("PWM9 :");
tft.setCursor(215, 95);
tft.println("PWM10 :");

tft.setTextColor(GREEN3);
tft.setCursor(45, 150);
tft.println("Angle X");
tft.setCursor(155, 150);
tft.println("Angle Y");

if(avoidlag==0){
tft.fillRoundRect(245, 120, 55, 40, 3, RED); 
tft.fillRoundRect(248, 123, 49, 34, 3, BLACK);
}
else{
tft.fillRoundRect(245, 120, 55, 40, 3, GREEN); 
tft.fillRoundRect(248, 123, 49, 34, 3, BLACK);
}

tft.fillRoundRect(245, 180, 55, 40, 3, RED);
tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
tft.setTextSize(1);
tft.setCursor(263, 195);
tft.setTextColor(CYAN);
tft.println("Back "); 
}

void menuA(){


if(infos[0]>179){
  infos[0] = 179;
}
 if(infos[1]>179){
  infos[1] = 179;
} 
if(infos[2]>179){
  infos[2] = 179;
}
 if(infos[3]>179){
  infos[3] = 179;
} 
  tft.fillCircle(xjoy1, yjoy1, 2, BLACK);
tft.fillCircle(xjoy2, yjoy2, 2, BLACK); 

 tft.fillRect(54, 170, 20, 10, BLACK);
 tft.fillRect(164, 170, 20, 10, BLACK);
tft.fillRect(259, 34, 24, 71, BLACK);

 xjoy1 = map(data[1], 0, 1024, 31, 89);
 yjoy1 = map(data[0], 1024, 0, 31, 89);
tft.fillCircle(xjoy1, yjoy1, 2, BLUE);
 xjoy2 = map(data[3], 0, 1024, 131, 189);
 yjoy2 = map(data[2], 1024, 0, 31, 89);
tft.fillCircle(xjoy2, yjoy2, 2, BLUE);  

timer2++;
if(timer2>30){
  timer2 = 20;
}

if(p.z > ts.pressureThreshhold && p.x < 350 && p.y > 300){
  if(avoidlag==1 && timer2>=20){
  avoidlag = 0;
  tft.fillRect(49, 119, 140, 10, BLACK);
  tft.fillRoundRect(245, 120, 55, 40, 3, RED); 
tft.fillRoundRect(248, 123, 49, 34, 3, BLACK);
  timer2 = 0; 
}
 if(avoidlag==0 && timer2>=20){
  avoidlag = 1;
  tft.setTextSize(1);
  tft.setCursor(50, 120);
  tft.setTextColor(RED3);
  tft.println("[Avoid lag] activated !");
  tft.fillRoundRect(245, 120, 55, 40, 3, GREEN); 
tft.fillRoundRect(248, 123, 49, 34, 3, BLACK);
  timer2 = 0; 
 }
}

if(avoidlag==0){
tft.setTextColor(GREEN1);
tft.setCursor(60, 170);
tft.println(infos[4]);
tft.setCursor(170, 170);
tft.println(infos[5]);

tft.setCursor(260, 35);
tft.setTextColor(RED);
tft.setTextSize(1);
tft.println(infos[0]);
tft.setCursor(260, 55);
tft.println(infos[1]);
tft.setCursor(260, 75);
tft.println(infos[2]);
tft.setCursor(260, 95);
tft.println(infos[3]);
}
else if(avoidlag==1){
}

}


void menuBstatis(){
  tft.fillCircle(50, 0, 80, PINK1);

tft.fillCircle(240, 20, 130, ORANGE);

tft.fillRect(31, 31, 114, 115, BLACK);
  tft.fillRect(176, 31, 114, 115, BLACK);

tft.drawRect(30, 30, 115, 116, WHITE);
tft.drawRect(175, 30, 115, 116, WHITE);
tft.setCursor(40, 160);
tft.setTextColor(CYAN);
tft.setTextSize(1);
tft.println("X:");
tft.setCursor(40, 175);
tft.println("Y:");
tft.setCursor(160, 160);
tft.println("X:");
tft.setCursor(160, 175);
tft.println("Y:");
tft.fillRoundRect(39, 199, 62, 32, 3, WHITE);
tft.fillRoundRect(139, 199, 62, 32, 3, WHITE);

if(data[4]==1){
  tft.fillRoundRect(41, 201, 58, 28, 3, GREEN);
}
else{
    tft.fillRoundRect(41, 201, 58, 28, 3, RED);
}

if(data[5]==1){
  tft.fillRoundRect(141, 201, 58, 28, 3, GREEN);
}
else{
    tft.fillRoundRect(141, 201, 58, 28, 3, RED);
}
    
tft.fillRoundRect(245, 180, 55, 40, 3, RED);
tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
tft.setTextSize(1);
tft.setCursor(263, 195);
tft.setTextColor(CYAN);
tft.println("Back "); 
}

void menuB(){
tft.drawCircle(xjoy2, yjoy2, 4, BLACK);
tft.drawCircle(xjoy1, yjoy1, 4, BLACK);
tft.fillRect(54, 159, 26, 25, BLACK);
tft.fillRect(174, 159, 26, 25, BLACK);


 xjoy1 = map(data[1], 0, 1024, 41, 134);
 yjoy1 = map(data[0], 1024, 0, 41, 134);
tft.drawCircle(xjoy1, yjoy1, 4, BLUE);
 xjoy2 = map(data[3], 0, 1024, 186, 279);
 yjoy2 = map(data[2], 1024, 0, 41, 134);
tft.drawCircle(xjoy2, yjoy2, 4, BLUE);
tft.setCursor(55, 160);
tft.setTextColor(CYAN);
tft.setTextSize(1);
tft.println(data[1]); 
tft.setCursor(55, 175);
tft.println(data[0]); 
tft.setCursor(175, 160);
tft.println(data[3]); 
tft.setCursor(175, 175);
tft.println(data[2]); 


if(timer<=100){
timer++;
}
if(p.z > ts.pressureThreshhold){
if(p.x>700 && p.y<300 && data[4]==1 && timer>=5){
  data[4] = 0;
  tft.fillRoundRect(41, 201, 58, 28, 3, RED);
  timer = 0;
}else{}
if(p.x>700 && p.y<300 && data[4]==0 && timer>=5){
   data[4] = 1;
   tft.fillRoundRect(41, 201, 58, 28, 3, GREEN);
     timer = 0;
}else{}
if(p.x<700 && p.y<300 && data[5]==1 && timer>=5){
  data[5] = 0;
  tft.fillRoundRect(141, 201, 58, 28, 3, RED);
  timer = 0;
}else{}
if(p.x<700 && p.y<300 && data[5]==0 && timer>=5){
  data[5] = 1;
  tft.fillRoundRect(141, 201, 58, 28, 3, GREEN);
    timer = 0;
}else{}
}

}

void menuCstatis(){
 tft.setCursor(10, 20);
tft.setTextColor(RED);
tft.setTextSize(1);
tft.println("This template must be created on the code."); 
 tft.setCursor(10, 40);
tft.println("For more informations, check up :"); 
tft.setCursor(10, 60);
tft.println("Hackster.io /https://www.hackster.io/MisterBotBreak"); 

tft.fillRoundRect(245, 180, 55, 40, 3, RED);
tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
tft.setTextSize(1);
tft.setCursor(263, 195);
tft.setTextColor(CYAN);
tft.println("Back "); 
}

void menuC(){
}

void menuDstatis(){
  tft.setCursor(10, 20);
tft.setTextColor(RED);
tft.setTextSize(1);
tft.println("This template must be created through the code."); 
 tft.setCursor(10, 40);
tft.println("For more informations, check up :"); 
tft.setCursor(10, 60);
tft.println("Hackster.io /https://www.hackster.io/MisterBotBreak"); 

tft.fillRoundRect(245, 180, 55, 40, 3, RED);
tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
tft.setTextSize(1);
tft.setCursor(263, 195);
tft.setTextColor(CYAN);
tft.println("Back "); 
}

void menuD(){
}



void menuCharactsstatis(){

tft.fillScreen(BLACK);

tft.fillCircle(320, 0, 150, BLUE);
tft.fillCircle(0, 160, 80, GREEN);
tft.fillCircle(140, 240, 50, RED);

tft.setCursor(60, 20);
tft.setTextColor(CYAN);
tft.setTextSize(1);
tft.println("Remote Controller Characteristics"); 
tft.setCursor(10, 50);
tft.setTextColor(WHITE);
tft.println("This controller transmits data using a"); 
tft.setCursor(245, 50);
tft.setTextColor(RED);
tft.println("nRF24l01"); 
tft.setCursor(10, 65);
tft.setTextColor(RED);
tft.println("radio module."); 
tft.setCursor(10, 90);
tft.setTextColor(WHITE);
tft.println("Packet sent : data, array (6 * int) ");
tft.setCursor(10, 105);
tft.println("Packet received : infos, array (6 * int) "); 

tft.setCursor(10, 125);
tft.println("By default, infos[0,1,2,3] are associated with PWMs"); 
tft.setCursor(10, 140);
tft.println("while infos[4,5] are angles (gyroscope)."); 
tft.setCursor(10, 155);
tft.println("Template A is especially for drone driving."); 

 tft.setCursor(10, 175);
tft.println("CE : D10");
tft.setCursor(10, 190);
tft.println("CSN : D11"); 
 tft.setCursor(100, 175);
tft.println("MOSI : D51");
tft.setCursor(100, 190);
tft.println("MISO : D50"); 
tft.setCursor(10, 205);
tft.println("SCK : D52"); 


tft.fillRoundRect(245, 180, 55, 40, 3, RED);
tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
tft.setTextSize(1);
tft.setCursor(263, 195);
tft.setTextColor(CYAN);
tft.println("Back ");  


}


void menuCharacts(){
  
}

void menuXstatis(){
  //put all static elements of your patern here
}

void menuX(){
  //put all dynamic elements of your patern here
}

void startingmenustatis(){
  tft.drawRoundRect(20, 60, 130, 40, 3, WHITE);
  tft.drawRoundRect(170, 60, 130, 40, 3, WHITE);
  tft.drawRoundRect(20, 120, 130, 40, 3, WHITE);
  tft.drawRoundRect(170, 120, 130, 40, 3, WHITE);
  tft.drawRoundRect(20, 180, 205, 40, 3, WHITE);
  tft.setCursor(18, 20);
  tft.setTextColor(CYAN);
  tft.setTextSize(1);
  tft.println("Choose your template for this remote controler !");

  tft.setCursor(51, 67);
  tft.setTextColor(RED);
  tft.println("Template A"); 
  tft.setCursor(53, 85);
  tft.println("(standart)");
  tft.setCursor(203, 75);
  tft.setTextColor(ORANGE);
  tft.println("Template B"); 
  tft.setCursor(51, 135);
  tft.setTextColor(YELLOW);
  tft.println("Template C"); 
  tft.setCursor(203, 135);
  tft.setTextColor(GREEN);
  tft.println("Template D"); 
  tft.setCursor(51, 195);
  tft.setTextColor(MAGENTA);
  tft.println("Technical Characteristics"); 

tft.fillRoundRect(245, 180, 55, 40, 3, RED);
tft.fillRoundRect(248, 183, 49, 34, 3, BLACK);
tft.setTextSize(1);
tft.setCursor(263, 195);
tft.setTextColor(CYAN);
tft.println("Back "); 
}


    
void loop(){

 data[0] = analogRead(A8);
  data[1] = analogRead(A9);
  data[2] = analogRead(A10);
  data[3] = analogRead(A11);
    

digitalWrite(13, HIGH);
  p = ts.getPoint();
 digitalWrite(13, LOW);
 pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);


if(startingmenuvariable==0 && menu==0){
  startingmenustatis();
  startingmenuvariable = 1;
}
 if(menu > 0 && p.x < 350 && p.y < 300){
  tft.fillScreen(BLACK);
  startingmenustatis();
  menu = 0;
  menulaunch = 0;
 }


 
 if(p.z > ts.pressureThreshhold){
  if(menu==0 || menulaunch==0){
  if(p.y>550 && p.x>600){
    menu = 1;
  }
  if(p.y>550 && p.x<600){
    menu = 2;
  }
  if(550>p.y && p.y>340 && p.x>600){
    menu = 3;
  }
  if(550>p.y && p.y>340 && p.x<600){
    menu = 4;
  }
  if(p.y<340 && p.x>400){
    menu = 5;
  }
  }
 }


 if(menu==1 && menulaunch != 1){
  tft.fillScreen(BLACK);
  menuAstatis();
  menulaunch = 1;
 }
 if(menu==1){
  menuA();
 }
 if(menu==2 && menulaunch != 2){
  tft.fillScreen(BLACK);
  menuBstatis();
  menulaunch = 2;
 }
 if(menu==2){
  menuB();
 }
  if(menu==3 && menulaunch != 3){
  tft.fillScreen(BLACK);
  menuCstatis();
  menulaunch = 3;
 }
 if(menu==3){
  menuC();
 }
  if(menu==4 && menulaunch != 4){
  tft.fillScreen(BLACK);
  menuDstatis();
  menulaunch = 4;
 }
 if(menu==4){
  menuD();
 }
if(menu==5 && menulaunch != 5){
  tft.fillScreen(BLACK);
  menuCharactsstatis();
  menulaunch = 5;
 }
 if(menu==5){
  menuCharacts();
 }


 /*PROGRAM YOUR OWN PATERN HERE : X = the patern' letter / x = the patern' number
  
   if(menu==x && menulaunch != x){
  tft.fillScreen(BLACK);
  menuBstatis();
  menulaunch = x;
 }
 if(menu==x){
  menuX();
 }
  */


 data[0] = analogRead(A8);
  data[1] = analogRead(A9);
  data[2] = analogRead(A10);
  data[3] = analogRead(A11);


 if(Mirf.dataReady()){
    Mirf.getData((byte *) &infos); 
    
    Serial.println(infos[0]); 
    Serial.println(infos[1]); 
    Serial.println(infos[2]); 
    Serial.println(infos[3]); 
   Serial.println(infos[4]); 
    Serial.println(infos[5]); 
  
  }

    delay(10);
    
Mirf.send((byte *) &data); 
  while(Mirf.isSending()); 

}

Credits

MisterBotBreak

MisterBotBreak

48 projects • 151 followers
I love electronics and cats :D !

Comments