Transmitting X and Y values from joystick using nRF24L01

Hello,

I am a couple of months in to Arduino and making progress on my RC boat. I'm trying to use a joystick on the transmitter side (Uno) to send rudder (X) and throttle (Y) commands to the receiver (ESP32). I've managed to control throttle (via L298N - replacing next week with TB6612FNG based on posts I read here about the L298N being inefficient) but to have control over both, throttle and rudder, I'd like to send X and Y values as an array and that's where I'm having issues. I think I'm not reading the array correctly on the receiver's side (I'm relatively new to Arduino coding). I could use some help here! Thanks!

// Transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001"; // Address for communication

int rudder;
int throttle;
int joystick[2];

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_LOW);
  radio.stopListening();
}

void loop() {
  rudder = analogRead(A0);
  throttle = 1023 - analogRead(A1); // Personal preference to read this way

  joystick[0] = rudder;
  joystick[1] = throttle;
  
  radio.write(&joystick, sizeof(joystick)); // Send motor speed to receiver
 
  Serial.print(rudder);
  Serial.print("|");
  Serial.print(throttle);
  Serial.println();
  delay(50);
}

// Receiver

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <ESP32Servo.h>

RF24 radio(4, 5); // CE, CSN
const byte address[6] = "00001"; // Address for communication

// L298N Motor Driver Pins
const int IN1 = 32;   // Motor direction pin 1
const int IN2 = 33;   // Motor direction pin 2
const int ENA = 25;  // Motor speed control (PWM)
const int servo1_pin = 14; // Servo pin

Servo servo1;

int joystick[2];
int throttle;
int rudder;

void setup() {
  // Serial.begin(9600);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENA, OUTPUT);
  servo1.attach(servo1_pin);
  
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_LOW);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    radio.read(&joystick, sizeof(joystick)); // Receive motor speed
    int rudder = joystick[0];
    int throttle = joystick[1];
    if (throttle > 562) { // Forward; 50 added as deadzone to center position of 512 for stability
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    analogWrite(ENA, map(throttle, 562, 1023, 0, 255));
  } else if (throttle < 462) { // Reverse
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    analogWrite(ENA, map(throttle, 0, 462, 255, 0));
  } else { // Stop
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
  }

servo_angle = map(rudder, 0, 1023, 0, 180);
servo1.write(servo_angle);
    }
}

The size of an int on a Uno is 2 bytes whilst the size of an int on an ESP32 is 4 bytes. Can you see a problem ?

Declare the array as type uint16_t on both sketches to force the variables to be of the same size

2 Likes

@UKHeliBob, of course, thank you very much for that, still getting used to differences in data types and the like.

Works like a charm now. Appreciate you taking the time!

Glad I could help

Good luck with your project

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.