<aside> ⚠️ This page is a work in progress, some information is subject to change
</aside>
coming soon, available on request
coming soon, available on request
<aside> ⚠️ Code is not finalized. This code will be tested, debugged & notated in the coming days
</aside>
// transmiter V2
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
// Define the radio communication
RF24 radio(8, 9); // nRF24L01 (CE, CSN)
const byte address[6] = "00001"; // Address
struct Data_Package {
byte PotX;
byte PotY;
byte Button;
};
Data_Package data;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
data.PotX = 127; // Values from 0 to 255. When Joystick is in resting position, the value is in the middle, or 127. We actually map the pot value from 0 to 1023 to 0 to 255 because that's one BYTE value
data.PotY = 127;
}
void loop() {
// put your main code here, to run repeatedly:
data.PotX = map(analogRead(A1), 0, 1023, 0, 255); // Convert the analog read value from 0 to 1023 into a BYTE value from 0 to 255
data.PotY = map(analogRead(A0), 0, 1023, 0, 255);
data.Button = digitalRead(1);
// Serial.write("\n");Serial.print(analogRead(A0));Serial.write("\n");Serial.print(analogRead(A1));Serial.write("\n");
// Serial.write("\n");Serial.print(data.PotY);Serial.write("\n");Serial.print(data.PotX);Serial.write("\n");
Serial.print(data.PotX);
Serial.print(",");
Serial.print(data.PotY);
Serial.print(",");
Serial.print("\n");
radio.write(&data, sizeof(Data_Package));
}
// Reciver V2
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
#define enA 5
#define in1 7
#define in2 6
RF24 radio(8, 9); // nRF24L01 (CE, CSN)
const byte address[6] = "00001";
unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;
Servo steering;
int servo1Value = 0;
int motorSpeedA = 0;
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte j1PotX;
byte j1PotY;
byte j1Button;
};
Data_Package data; //Create a variable with the above structure
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
radio.startListening(); // Set the module as receiver
resetData();
steering.attach(A5);
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
Serial.print("\nservo\n");
for (int i =30; i<=150; i++) {
steering.write(i);
Serial.print(i);
Serial.write("\n");
delay(5);
steering.write(servo1Value);
}
}
void loop() {
// Check whether we keep receving data, or we have a connection between the two modules
currentTime = millis();
if ( currentTime - lastReceiveTime > 1000 ) { // If current time is more then 1 second since we have recived the last data, that means we have lost connection
resetData(); // If connection is lost, reset the data. It prevents unwanted behavior, for example if a drone jas a throttle up, if we lose connection it can keep flying away if we dont reset the function
Serial.write("\nlost connection");
}
// Check whether there is data to be received
if (radio.available()) {
radio.read(&data, sizeof(Data_Package)); // Read the whole data and store it into the 'data' structure
lastReceiveTime = millis();// At this moment we have received the data
Serial.write("\ngood connection");
}
Serial.write("\n");
Serial.print(data.j1PotX);
Serial.write("\n");
Serial.print(data.j1PotY);
Serial.write("\n");
Serial.print(data.j1Button);
// Controlling servos
servo1Value = map(data.j1PotX, 0, 255, 30, 150);
steering.write(servo1Value);
Serial.write("\n");
Serial.print(servo1Value);
//servo test
// Serial.print("\nservo test\n");
// for (int i =30; i<=150; i++) {
// steering.write(i);
// Serial.print(i);
// Serial.write("\n");
// delay(10);
// }
//controll motor w l298n
motorSpeedA = map(data.j1PotY, 0, 255, -240, 240);
if (motorSpeedA > 50){//go forwards with speed relative to input
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, motorSpeedA);
}
if (motorSpeedA < 50){//go backwards with speed relative to input
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, -motorSpeedA);
}
if (50 < motorSpeedA < 50){//stop
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(enA, 0);
}
// // Monitor the battery voltage
// int sensorValue = analogRead(A0);
// float voltage = sensorValue * (5.00 / 1023.00) * 3; // Convert the reading values from 5v to suitable 12V i
// Serial.println(voltage);
// // If voltage is below 11V turn on the LED
// if (voltage < 11) {
// digitalWrite(led, HIGH);
// }
// else {
// digitalWrite(led, LOW);
// }
}
void resetData() {
// Reset the values when there is no radio connection - Set initial default values
data.j1PotX = 127;
data.j1PotY = 127;
data.j1Button = 1;
}