I am using this code now
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // select CE,CSN pin
const byte address[6] = "00001";
struct Signal {
byte throttle;
byte pitch;
byte roll;
byte yaw;
byte aux1;
byte aux2;
};
Signal data;
void ResetData()
{
data.throttle = 127; // Default position of motors
data.pitch = 127;
data.roll = 127;
data.yaw = 127;
data.aux1 = 127;
data.aux2 = 127;
}
void setup()
{
Serial.begin(9600);
//Start everything up
radio.begin();
radio.openWritingPipe(address);
radio.stopListening(); //start the radio comunication for Transmitter
}
// Joystick center and its borders
int mapJoystickValues(int val, int lower, int middle, int upper, bool reverse)
{
val = constrain(val, lower, upper);
if ( val < middle )
val = map(val, lower, middle, 0, 128);
else
val = map(val, middle, upper, 128, 255);
return ( reverse ? 255 - val : val );
}
void loop()
{
ResetData();
// Control Stick Calibration
// Setting may be required for the correct values of the control levers.
data.throttle = mapJoystickValues( analogRead(A0), 12, 524, 1020, true ); // "true" or "false" for signal direction
data.roll = mapJoystickValues( analogRead(A3), 12, 524, 1020, true ); // "true" or "false" for servo direction
data.pitch = mapJoystickValues( analogRead(A2), 12, 524, 1020, true ); // "true" or "false" for servo direction
data.yaw = mapJoystickValues( analogRead(A1), 12, 524, 1020, true ); // "true" or "false" for servo direction
data.aux1 = mapJoystickValues( analogRead(A4), 12, 524, 1020, true ); // "true" or "false" for servo direction
data.aux2 = mapJoystickValues( analogRead(A5), 12, 524, 1020, true );// "true" or "false" for servo direction
Serial.print(data.throttle);
Serial.print('\t');
Serial.print(data.roll);
Serial.print('\t');
Serial.print(data.pitch);
Serial.print('\t');
Serial.print(data.yaw);
Serial.print('\t');
Serial.print(data.aux1);
Serial.print('\t');
Serial.print(data.aux2);
Serial.println('\t');
radio.write(&data, sizeof(Signal));
}
for the transmitter
And this one
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
int ch_width_1 = 0;
int ch_width_2 = 0;
int ch_width_3 = 0;
int ch_width_4 = 0;
int ch_width_5 = 0;
int ch_width_6 = 0;
Servo ch1;
Servo ch2;
Servo ch3;
Servo ch4;
Servo ch5;
Servo ch6;
struct Signal {
byte throttle;
byte pitch;
byte roll;
byte yaw;
byte aux1;
byte aux2;
};
Signal data;
RF24 radio(7, 8);
const byte address[6] = "00001";
void ResetData()
{
// Define the inicial value of each data input.
// The middle position for Potenciometers. (254/2=127)
data.roll = 127;
data.pitch = 127;
data.throttle = 127;
data.yaw = 127;
data.aux1 = 127;
data.aux2 = 127;
}
void setup()
{
Serial.begin(9600);
//Set the pins for each PWM signal
ch1.attach(2);
ch2.attach(3);
ch3.attach(4);
ch4.attach(5);
ch5.attach(6);
//Configure the NRF24 module
ResetData();
radio.begin();
radio.openReadingPipe(0, address);
radio.startListening(); //start the radio comunication for receiver
}
unsigned long lastRecvTime = 0;
void recvData()
{
if ( radio.available() ) {
radio.read(&data, sizeof(Signal));
lastRecvTime = millis(); // receive the data | data alınıyor
}
}
void loop()
{
recvData();
unsigned long now = millis();
if ( now - lastRecvTime > 1000 ) {
ResetData(); // Signal lost.. Reset data
}
ch_width_4 = map(data.yaw, 0, 255, 1000, 2000); // pin D5 (PWM signal)
ch_width_2 = map(data.pitch, 0, 255, 1000, 2000); // pin D3 (PWM signal)
ch_width_3 = map(data.throttle, 0, 255, 1000, 2000); // pin D4 (PWM signal)
ch_width_1 = map(data.roll, 0, 255, 1000, 2000); // pin D2 (PWM signal)
ch_width_5 = map(data.aux1, 0, 255, 1000, 2000); // pin D6 (PWM signal)
ch_width_6 = map(data.aux2, 0, 255, 1000, 2000); // pin D7 (PWM signal)
Serial.print(ch_width_1+2);
Serial.print('\t');
Serial.print(ch_width_2);
Serial.print('\t');
Serial.print(ch_width_3);
Serial.print('\t');
Serial.print(ch_width_4);
Serial.print('\t');
Serial.print(ch_width_5);
Serial.print('\t');
Serial.println(ch_width_6);
// Write the PWM signal
ch1.writeMicroseconds(ch_width_1);
ch2.writeMicroseconds(ch_width_2);
ch3.writeMicroseconds(ch_width_3);
ch4.writeMicroseconds(ch_width_4);
ch5.writeMicroseconds(ch_width_5);
ch6.writeMicroseconds(ch_width_6);
}
for the receiver.
The problem is that while it works in the serial monitor(i am able to see the values change accordingly), it doesnt work as well when i connect a servo motor(only got those, no escs
)
the servo motor just keeps on moving until it reaches the end and then stops. After some time, the servo gets heated up. Opened the servo's top to find out that the motor just keeps on moving without stopping, even though I am changing the joystick and everything. Even tried using different codes, but this stays the same. In the serial monitor, i am able to get the output, but while using it, no dice