Hi all, glad to enter this awesome forum again. Now I have a question. I had tried many times but I get failed.
CASE:
I've been building a NRF24 Transmitter for my Multiwii 2.3 + NRF24 + BMP180 Drone. Everything looks okay, but when I want to test the NRF24L01 transmitter, I see from Serial Monitor and Mini LCD, It shows 127/128 value for Throttle. Therefore, when I arm the drone and, as soon as I release the left stick (it goes to middle-center position = throttle 127/128), the motors start spin 50% or 127/128.
This is what makes it 127/128:
data.throttle = mapJoystickValues(analogRead(A0), 12, 515, 1024, true );
And, this is to config the mapjoystickValue:
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 );
}
But, I have no clue, how to modify it.
WHAT TO ACHIEVE:
I want the throttle value go to 0% or 0 or 1 or 10 (even though the left stick is at middle-center position.
Here's the NRF24 Drone code Receiver (related to this case):
1. NRF24_RX.cpp
nrf24_rcData[THROTTLE] = map(strData.throttle, 0, 255, 1000, 2000);
nrf24_rcData[ROLL] = map(strData.roll, 0, 255, 1000, 2000);
nrf24_rcData[PITCH] = map(strData.pitch, 0, 255, 1000, 2000);
nrf24_rcData[YAW] = map(strData.yaw, 0, 255, 1000, 2000);
nrf24_rcData[AUX1] = map(strData.AUX1, 0, 1, 1000, 2000);
nrf24_rcData[AUX2] = map(strData.AUX2, 0, 1, 1000, 2000);
2. in RX.cpp file:
#elif defined(NRF24_RX)
if (chan < RC_CHANS) {
data = nrf24_rcData[chan];
} else data = 1500;
FULL CODE for NRF24 TX:
/*
* https://www.youtube.com/@ardujimmy
*/
/******************************************************* Multiwii 2.3 NRF24L01 Transmitter ********************************************************/
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const uint64_t pipeOut = 0xE8E8F0F0E1LL; //IMPORTANT: The same as in the receiver!!!
RF24 radio(4, 3); // CE, CSN
String strAUX1;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int16_t tRoll;
int16_t tPitch;
#define trimRollUpPin 8
#define trimRollDownPin 7
#define trimPitchUpPin 6
#define trimPitchDownPin 5
struct MyData {
byte throttle;
byte yaw;
byte pitch;
byte roll;
byte AUX1;
int16_t rollTrim;
int16_t pitchTrim;
};
MyData data;
void resetData() {
data.throttle = 0;
data.yaw = 127;
data.pitch = 127;
data.roll = 127;
data.AUX1 = 0;
data.rollTrim = 0;
data.pitchTrim = 0;
}
void setup(){
Serial.begin(9600);
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openWritingPipe(pipeOut);
for (byte address = 1; address < 127; address++) {
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0) {
Serial.print("I2C device found at 0x");
Serial.println(address, HEX);
}
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println(F("SSD1306 allocation failed"));
//for (;;)
;
}
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
//Show in LCD
display.clearDisplay();
display.setCursor(0, 0); // Set cursor to top-left corner
display.println("Hallo Ardujimmy");
display.println("-----------------");
display.println("Please wait .....");
display.display();
delay(100);
pinMode(trimRollUpPin, INPUT_PULLUP);
pinMode(trimRollDownPin, INPUT_PULLUP);
pinMode(trimPitchUpPin, INPUT_PULLUP);
pinMode(trimPitchDownPin, INPUT_PULLUP);
resetData();
}
}
void updateTrimValues() {
if (digitalRead(trimRollUpPin) == HIGH) {
tRoll += 5; // Increase roll trim
}
if (digitalRead(trimRollDownPin) == HIGH) {
tRoll -= 5; // Decrease roll trim
}
if (digitalRead(trimPitchUpPin) == HIGH) {
tPitch += 5; // Increase pitch trim
}
if (digitalRead(trimPitchDownPin) == HIGH) {
tPitch -= 5; // Decrease pitch trim
}
// Constrain trim values
tRoll = constrain(tRoll, -250, 250);
tPitch = constrain(tPitch, -250, 250);
}
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(){
updateTrimValues();
//data.throttle = mapJoystickValues(analogRead(A0), 12, 515, 1024, true );
data.throttle = mapJoystickValues(analogRead(A0), 12, 515, 1024, true );
data.yaw = mapJoystickValues(analogRead(A1), 13, 524, 1024, true );
data.pitch = mapJoystickValues(analogRead(A3), 10, 512, 1024, true );
data.roll = mapJoystickValues(analogRead(A2), 1, 524, 1022, true );
data.AUX1 = digitalRead(9);
data.rollTrim = tRoll;
data.pitchTrim = tPitch;
//Send all
radio.write(&data, sizeof(MyData));
//Show in LCD
display.clearDisplay();
display.setCursor(0, 0); // Set cursor to top-left corner
display.println("NRF24 BMP180 QuadX");
if (data.AUX1 == 1){
strAUX1 = "AUX1 ON";
}
else{
strAUX1 = "AUX1 OFF";
}
display.println(strAUX1);
display.println("Throttle: " + String(data.throttle));
display.println("Yaw : " + String(data.yaw));
display.println("Roll : " + String(data.roll));
display.println("Pitch : " + String(data.pitch));
display.println("t_Pitch : " + String(data.pitchTrim));
display.println("t_Roll : " + String(data.rollTrim));
display.display();
// Add delay for readability
delay(100);
}
Perhaps necessary:
Source: https://www.youtube.com/shorts/cfkqHvwNXHw
Analog Joystick I use now: