ESC Map To Joystick Function

Hi,

I am a little confused on the ESC map function function to my joystick.

potValue = map(joyStick[0], 0, 1023, 0, 180);

I'm taking in the value and mapping my ESC from 0 - 1023, and again my joystick from 0 - 180 as they should step up together. It does work, but ideally I would like to have a higher value than 180.

It seems like when I change the code to be

potValue = map(joyStick[0], 0, 1023, 0, 800);

It does not give me my expected results, which would be a slower rev up and the ability to increment slower.

Am I understanding this operation incorrectly?

Here is the function for my ESC attach as well

ESC.attach(5,1000,2000);

Should I instead map the following way?

potValue = map(joyStick[0], 1000, 2000, 0, 1000);

Thanks for any assistance.

Please post your complete sketch. Snippets do not do much good

May be a little profanity for some items in there, frustrating while learning! Also, this is not exactly clean, I have been playing with various things on this code, appreciate any help with any other areas of this code but hoping we can keep any comments to the Map configuration.

// SimpleRx - the slave or the receiver

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

#define CE_PIN   9
#define CSN_PIN 10

#include<Wire.h>
const int MPU=0x68; 
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

Servo ESC;
Servo ESC2;
Servo ESC3;
Servo ESC4;

String newfuck; 

int xAxis;

char receivedData[2] = ""; 

const byte thisSlaveAddress[5] = {'R','x','A','A','A'};

RF24 radio(CE_PIN, CSN_PIN);

int joyFuck[32]; // this must match dataToSend in the TX
int potValue;
bool newData = false;
int Joy;
//Joy.toCharArray(joyFuck, 3);

//===========

void setup() {

    Serial.begin(9600);
ESC.attach(5,1000,2000); // (pin, min pulse width, max pulse width in microseconds) 
ESC2.attach(6,1000,2000); // (pin, min pulse width, max pulse width in microseconds) 
ESC3.attach(3,1000,2000); // (pin, min pulse width, max pulse width in microseconds) 
ESC4.attach(7,1000,2000); // (pin, min pulse width, max pulse width in microseconds) 
    Serial.println("SimpleRx Starting");
    radio.begin();
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.startListening();

  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B); 
  Wire.write(0);    
  Wire.endTransmission(true);

}

//=============

void loop() {
    getData();
    showData();

    Joy == joyFuck[32];

potValue = map(joyFuck[0], 0, 1023, 0, 180);
    Serial.println(joyFuck[0]);
    Serial.println(potValue);
    ESC.write(potValue);
//ESC.write(potValue);
  if (joyFuck[0] > 600) {
    Serial.println("YYYEESSSS");
    ESC.write(potValue);
    ESC2.write(potValue);
    ESC3.write(potValue);
    ESC4.write(potValue);


  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  
  Wire.endTransmission(false);
  Wire.requestFrom(MPU,12,true);  
  AcX=Wire.read()<<8|Wire.read();    
  AcY=Wire.read()<<8|Wire.read();  
  AcZ=Wire.read()<<8|Wire.read();  
  GyX=Wire.read()<<8|Wire.read();  
  GyY=Wire.read()<<8|Wire.read();  
  GyZ=Wire.read()<<8|Wire.read();  
  
  Serial.print("Accelerometer: ");
  Serial.print("X = "); Serial.print(AcX);
  Serial.print(" | Y = "); Serial.print(AcY);
  Serial.print(" | Z = "); Serial.println(AcZ); 
  
  Serial.print("Gyroscope: ");
  Serial.print("X = "); Serial.print(GyX);
  Serial.print(" | Y = "); Serial.print(GyY);
  Serial.print(" | Z = "); Serial.println(GyZ);
  Serial.println(" ");
  delay(333);


    
  }

}

//==============

void getData() {
    if ( radio.available() ) {
        radio.read( &joyFuck, sizeof(joyFuck) );
        newData = true;
    }
}

void showData() {
    if (newData == true) {
     //   Serial.print("Data received");
    //    Serial.println(joyFuck[0]);
    //    Serial.println (xAxis);
        newData = false;
    }
}

I didn’t change anything substantive, but I did address the formatting and tabulation issues you have in your original code:

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

#define CE_PIN   9
#define CSN_PIN 10

const int MPU = 0x68;
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;

Servo ESC;
Servo ESC2;
Servo ESC3;
Servo ESC4;

int xAxis;
char receivedData[2] = "";
const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

int joyFuck[32]; // this must match dataToSend in the TX
int potValue;
bool newData = false;
int Joy;


void setup()
{
  Serial.begin(9600);
  
  ESC.attach(5);
  ESC2.attach(6);
  ESC3.attach(3);
  ESC4.attach(7);
  
  Serial.println("SimpleRx Starting");
  
  radio.begin();
  radio.openReadingPipe(1, thisSlaveAddress);
  radio.startListening();

  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
}


void loop()
{
  getData();
  showData();

  Joy == joyFuck[32];

  potValue = map(joyFuck[0], 0, 1023, 0, 180);
  Serial.println(joyFuck[0]);
  Serial.println(potValue);
  ESC.write(potValue);
  
  if (joyFuck[0] > 600)
  {
    Serial.println("YYYEESSSS");
    
    ESC.write(potValue);
    ESC2.write(potValue);
    ESC3.write(potValue);
    ESC4.write(potValue);
    
    Wire.beginTransmission(MPU);
    Wire.write(0x3B);
    Wire.endTransmission(false);
    Wire.requestFrom(MPU, 12, true);
    
    AcX = Wire.read() << 8 | Wire.read();
    AcY = Wire.read() << 8 | Wire.read();
    AcZ = Wire.read() << 8 | Wire.read();
    GyX = Wire.read() << 8 | Wire.read();
    GyY = Wire.read() << 8 | Wire.read();
    GyZ = Wire.read() << 8 | Wire.read();

    Serial.print("Accelerometer: ");
    Serial.print("X = "); Serial.print(AcX);
    Serial.print(" | Y = "); Serial.print(AcY);
    Serial.print(" | Z = "); Serial.println(AcZ);

    Serial.print("Gyroscope: ");
    Serial.print("X = "); Serial.print(GyX);
    Serial.print(" | Y = "); Serial.print(GyY);
    Serial.print(" | Z = "); Serial.println(GyZ);
    Serial.println(" ");
    
    delay(333);
  }
}


void getData()
{
  if (radio.available())
  {
    radio.read( &joyFuck, sizeof(joyFuck) );
    newData = true;
  }
}


void showData()
{
  if (newData == true)
  {
    //   Serial.print("Data received");
    //    Serial.println(joyFuck[0]);
    //    Serial.println (xAxis);
    newData = false;
  }
}

From what I could tell, your ESC looks like it’s being given a valid signal (assuming you are SURE joyFuck[0] stays within the 0-1023 bounds set in map() - might look into using [constrain()](https://www.arduino.cc/reference/en/language/functions/math/constrain/)).

The real question remains: did you properly calibrate your ESC?

The joyFuck array has 32 elements, 0-31. So

    Joy == joyFuck[32];

has two errors. == is comparison not assignment and [32] is past the end of the array. But you never use Joy so it may make little difference.

What do your Serial.prints show for the values of joyFuck[0] and potValue? Since you haven't posted the transmitter code we have no idea what you're sending. But if joyFuck[0] is in the range 0-1023 then the map() and so the ESC writes are fine.

Steve

Perhaps I need to re-calibrate the ESCs, I did calibrate them a while back.

So it sounds like the function itself as written, should step up as expected, It just seems like the joystick is not all the way up and the motors are full speed ahead which would go back to calibration. I may have a lead thumb as well and am fully up on the joystick when I don’t think I am, serial monitor should show that as well, I’ll look into that closer and re-calibrate.

joyFuck[0] does show up within the range of 0-1023, then pot value does as it should printing 0-180, I think it was my misunderstanding of the function as I thought maybe if I changed or increased the 0-180 in the line of code that the joystick would seem more accurate to the motor speed. Comes down to re-calibrate as previously mentioned.

Before I got the TX/RX working I was throwing some different things out there…Joy == joyFuck[32]; was a remnant of that, slipped through the cracks and didn’t throw any errors so didn’t bother. I could probably slow down a little bit once I get something working to clean things up a bit.

Thanks for all the info.

Edit with TX code for full picture

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


#define CE_PIN   7
#define CSN_PIN 8

const byte slaveAddress[5] = {'R','x','A','A','A'};


RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char dataToSend[10] = "Message 0";
char txNum = '0';

int joyFuck[32];


unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second


void setup() {

    Serial.begin(9600);

    Serial.println("SimpleTx Starting");

    radio.begin();
    radio.setRetries(1,5); // delay, count
    radio.openWritingPipe(slaveAddress);
    radio.stopListening();
}

//====================

void loop() {

  joyFuck[0] = analogRead(A0);
             currentMillis = millis();
            if (currentMillis - prevMillis >= txIntervalMillis) {
            send();
     prevMillis = millis();
 }
}

//====================

void send() {

    bool rslt;
    rslt = radio.write( &joyFuck, sizeof(joyFuck) );
        // Always use sizeof() as it gives the size as the number of bytes.
        // For example if dataToSend was an int sizeof() would correctly return 2

    Serial.print("Data Sent ");
    Serial.print(joyFuck[0]);
    if (rslt) {
        Serial.println("  Acknowledge received");
        updateMessage();
    }
    else {
        Serial.println("  Tx failed");
    }
}

//================

void updateMessage() {
        // so you can see that new data is being sent
    txNum += 1;
    if (txNum > '9') {
        txNum = '0';
    }
    dataToSend[8] = txNum;
}

You need to learn how to properly use tabs and whitespace. Readability matters in software and will save you time in the long run.

jbarth200:
I think it was my misunderstanding of the function as I thought maybe if I changed or increased the 0-180 in the line of code that the joystick would seem more accurate to the motor speed.

Changing 0 or 180 will only change the ranges of output to the servo, not changing accuracy. If you want more accuracy, you can use "Servo.writeMicroseconds()"

Tbh, though, I still don't understand exactly what's going on wrong. Can you explain the problem a little clearer? Is the ESC not responding to the Arduino at all?

I will be sure to manage tabs and whitespace better as it does cause a lot of confusion.

No, all ESCs are responding as they should, it just didn’t seem like I could keep a stable mid range and either the motors were hardly spinning or going to the max. I am going to re-calibrate my ESCs today.

I may try the “Servo.writeMicrosecoonds()” instead of just the ESC.write.

Thanks for all the info!