Devices & Components
Arduino Uno Rev3
Grove - Thumb Joystick
Arduino Nano
PCB Board Pin Headers
Breadboard Jumper Wire Pack (200mm&100mm)
nRF24L01 tranceiver
Perf Board, 2.5 x 3.5 inches
Software & Tools
Arduino IDE
Project description
Code
Receiver Code
cpp
Code for the receiver.
1#include <SPI.h> 2#include <nRF24L01.h> 3#include <RF24.h> 4#include <Servo.h> 5 6RF24 radio(7, 8); // CE, CSN 7const byte address[6] = "77777"; 8 9struct package 10{ 11 int x1 = 0; // aileron 12 int y1 = 0; // elevator 13 14 int x2 = 0; // rudder 15 int y2 = 0; // thrust 16 17 char b1 = '0'; // eject 18}; 19 20struct package data; 21 22// Servo objects 23Servo motor; 24Servo rudder; 25Servo aileron1; 26Servo aileron2; 27Servo elevator; 28 29// Trim values (adjust as needed) 30int trimElevator = 0; 31int trimRudder = 0; 32int trimAileron1 = 0; 33int trimAileron2 = 0; 34 35void setup() 36{ 37 Serial.begin(9600); 38 39 elevator.attach(3); 40 aileron1.attach(5); 41 aileron2.attach(6); 42 rudder.attach(10); 43 motor.attach(9, 1000, 2000); 44 45 // Initialize control surfaces to trimmed neutral 46 elevator.write(constrain(90 + trimElevator, 0, 180)); 47 aileron1.write(constrain(90 + trimAileron1, 0, 180)); 48 aileron2.write(constrain(90 + trimAileron2, 0, 180)); 49 rudder.write(constrain(90 + trimRudder, 0, 180)); 50 51 radio.begin(); 52 radio.openReadingPipe(0, address); 53 radio.setPALevel(RF24_PA_MIN); 54 radio.setDataRate(RF24_250KBPS); 55 radio.setAutoAck(false); 56 radio.startListening(); 57} 58 59void loop() 60{ 61 if (radio.available()) 62 { 63 radio.read(&data, sizeof(data)); 64 65 // Thrust control 66 data.y2 = constrain(data.y2, 90, 180); 67 motor.write(map(data.y2, 90, 180, 0, 90)); 68 69 // Control surfaces with trim applied 70 rudder.write(constrain(data.x2 + trimRudder, 0, 180)); 71 aileron1.write(constrain(data.x1 + trimAileron1, 0, 180)); 72 aileron2.write(constrain(data.x1 + trimAileron2, 0, 180)); 73 elevator.write(constrain(data.y1 + trimElevator, 0, 180)); 74 75 // Debug info 76 Serial.print("x1: "); Serial.print(data.x1); 77 Serial.print(" y1: "); Serial.print(data.y1); 78 Serial.print(" x2: "); Serial.print(data.x2); 79 Serial.print(" y2: "); Serial.print(data.y2); 80 Serial.print(" button: "); Serial.println(data.b1); 81 } 82}
Transmitter Code
cpp
Code for transmitter.
1#include <SPI.h> 2#include <nRF24L01.h> 3#include <RF24.h> 4 5RF24 radio(7, 8); // CE, CSN 6 7const byte address[6] = "77777"; 8 9#define buttonPin 6 10 11struct package 12{ 13 int x1 = 0; // aileron 14 int y1 = 0; // elevator 15 16 int x2 = 0; // rudder 17 int y2 = 0; // thrust 18 19 char b1 = '0'; // eject 20}; 21 22struct package data; 23 24void setup() 25{ 26 Serial.begin(9600); 27 28 radio.begin(); 29 radio.openWritingPipe(address); 30 radio.setPALevel(RF24_PA_MIN); 31 radio.setDataRate(RF24_250KBPS); 32 radio.setAutoAck(false); 33 radio.stopListening(); 34 35 pinMode(buttonPin, INPUT_PULLUP); 36} 37 38void loop() 39{ 40 data.x1 = map(analogRead(A1), 0, 1023, 0, 180); 41 data.y1 = map(analogRead(A0), 0, 1023, 0, 180); 42 data.x2 = map(analogRead(A3), 0, 1023, 180, 0); 43 data.y2 = map(analogRead(A2), 0, 1023, 180, 0); 44 data.b1 = digitalRead(buttonPin) ? '1' : '0'; 45 46 radio.write(&data, sizeof(data)); 47}
Comments
Only logged in users can leave comments