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.

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;

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");

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);

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() {
newData = true;
}
}

void showData() {
if (newData == true) {
//    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;
const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

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");

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);

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()
{
{
newData = true;
}
}

void showData()
{
if (newData == true)
{
//    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

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");

}

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

void loop() {

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) {
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!