Taking 1 value from a pot and controlling 2 ESCs with it

okay, I have been able to control 1 motor with a pot but when I try to control 2 motors, the motors only spin for a little and then stop completely. What is wrong with my code?

#include <Servo.h> 
 
Servo myservo1;  // first motor to be controlled by potentiometer
Servo myservo2;  // second motor to be controlled by potentiometer
 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
 
void setup() 
{ 
  Serial.begin(9600);          //  setup serial
  myservo1.attach(6);  // attaches the servo on pin 9 to the servo object
 myservo2.attach(9); 
} 

void loop() 
{ 
  val = analogRead(potpin);    // read the input pin
  Serial.println(val);             // debug value
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 480 and 990) 
  val = map(val, 0, 1023, 28, 179);     // scale it to use it with the motor (value between 28 and 179) 
  myservo1.write(val);        // sets the motor speed according to the scaled value 
  delay(125);                           // waits for the motor to gain speed 
  myservo2.write(val);
  delay(125);
}

First, you only need to take one analogRead per loop currently you are doing two reads per loop.

Next you need to use "map" a little different, something like the AnalogInOutSerial example:

// read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);  
  // change the analog out value:

Notice there is a new variable to = map.

I am not sure if that will fix your problem but, it is a start

Hmm, that didnt work either, it did the same thing, do you have any other ideas?

GamecubePerson111: okay, I have been able to control 1 motor with a pot but when I try to control 2 motors, the motors only spin for a little and then stop completely. What is wrong with my code?

It looks like your code is intend for "Continuous Rotation" servos (basically: gear motor with built-in controller). If you use normal hobby servos they will stop when they get to the chosen position.

I am actually using electronic speed controllers with brushless motors. I am trying to control the speed of them with the pot.

GamecubePerson111: Hmm, that didnt work either, it did the same thing, do you have any other ideas?

Show your new code with the changes. Also, serialPrint the sensorValue and the outputValue and show us the results

// read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);  
  // change the analog out value:

Here’s the code, Ill post the results tomorrow.

#include <Servo.h> 
 
Servo myservo1;  // first motor to be controlled by potentiometer
Servo myservo2;  // second motor to be controlled by potentiometer
 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
int val2;
 
void setup() 
{ 
  Serial.begin(9600);          //  setup serial
  myservo1.attach(6);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(9);
} 

void loop() 
{ 
  val = analogRead(potpin);    // read the input pin
  Serial.println(val);             // debug value
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 480 and 990) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the motor (value between 28 and 179) 
  myservo1.write(val);        // sets the motor speed according to the scaled value 
  delay(125);                           // waits for the motor to gain speed 
  val2 = analogRead(potpin);
  Serial.println(val2);
  val2 = analogRead(potpin);
  val2 = map(val2, 0, 1023, 0, 179);
  myservo2.write(val2);
  delay(125);
}

Because you shown us your code, I can see you are making an effort, good job! However, you did not make the changes the way I was trying to tell you.

Here is your code with the changes that I wanted you to make. Notice there is only one call to analogRead per loop and I changed the variable names so that they are more logical.

Look this code over:

#include <Servo.h> 

Servo myservo1;  // first motor to be controlled by potentiometer
Servo myservo2;  // second motor to be controlled by potentiometer

int potpin = A0;  // analog pin used to connect the potentiometer
int potValue = 0;    // variable to read the value from the analog pin 
int mappedValue = 0;    //variable to hold "mapped" value

void setup() 
{ 
  Serial.begin(9600);          //  setup serial
  myservo1.attach(6);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(9);
} 

void loop() 
{ 
  potValue = analogRead(potpin);    // reads the value of the potentiometer (value between 480 and 990)

  Serial.print("potValue = ");
  Serial.println(potValue);             // debug value

  mappedValue = map(potValue, 0, 1023, 0, 179);     // scale it to use it with the motor (value between 28 and 179) 

  Serial.print("mappedValue = ");
  Serial.println(mappedValue);
  Serial.println(" "); 

  myservo1.write(mappedValue);        // sets the motor speed according to the scaled value 
  delay(125);                           // waits for the motor to gain speed 
  myservo2.write(mappedValue);
  delay(125);
}

Oh, I see, sorry, Im new to all this stuff. As a result, in the serial monitor, the values continue to be correct as I turn the pot varying linearly, but the motors still stop after one second. Why could this be?

Btw it’s this code:

#include <Servo.h> 

Servo myservo1;  // first motor to be controlled by potentiometer
Servo myservo2;  // second motor to be controlled by potentiometer

int potpin = A0;  // analog pin used to connect the potentiometer
int potValue = 0;    // variable to read the value from the analog pin 
int mappedValue = 0;    //variable to hold "mapped" value

void setup() 
{ 
  Serial.begin(9600);          //  setup serial
  myservo1.attach(6);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(9);
} 

void loop() 
{ 
  potValue = analogRead(potpin);    // reads the value of the potentiometer (value between 480 and 990)

  Serial.print("potValue = ");
  Serial.println(potValue);             // debug value

  mappedValue = map(potValue, 0, 1023, 0, 179);     // scale it to use it with the motor (value between 28 and 179) 

  Serial.print("mappedValue = ");
  Serial.println(mappedValue);
  Serial.println(" "); 

  myservo1.write(mappedValue);        // sets the motor speed according to the scaled value 
  delay(125);                           // waits for the motor to gain speed 
  myservo2.write(mappedValue);
  delay(125);
}

How are you powering it all?

Next you need to use “map” a little different

There is nothing inherently wrong in using the same variable to hold the value returned by the map function as held the value that was copied in the call to the map function.

Sometimes it makes sense to use a different variable. Sometimes it makes sense to use the same variable. In this case, using the same variable makes sense.

I think you are saying that the serial monitor keeps printing values but the ESCs stop after a second?

If so try this code with a count down. If the motors run before the count down is over, we have a different problem to look for.

Also, you need to tell us how it is all powered as suggested above.

#include <Servo.h> 

Servo myservo1;  // first motor to be controlled by potentiometer
Servo myservo2;  // second motor to be controlled by potentiometer

int potpin = A0;  // analog pin used to connect the potentiometer
int potValue = 0;    // variable to read the value from the analog pin 
int mappedValue = 0;    //variable to hold "mapped" value

void setup() 
{ 
  Serial.begin(9600);          //  setup serial
  myservo1.attach(6);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(9);
  Serial.println("The count down");
  Serial.println("5");
  delay(1000);
  Serial.println("4");
  delay(1000);
  Serial.println("3");
  delay(1000);
  Serial.println("2");
  delay(1000);
  Serial.println("1");
  delay(1000);
  Serial.println("0");
} 

void loop() 
{ 
  potValue = analogRead(potpin);    // reads the value of the potentiometer (value between 480 and 990)

  Serial.print("potValue = ");
  Serial.println(potValue);             // debug value

    mappedValue = map(potValue, 0, 1023, 0, 179);     // scale it to use it with the motor (value between 28 and 179) 

  Serial.print("mappedValue = ");
  Serial.println(mappedValue);
  Serial.println(" "); 

  myservo1.write(mappedValue);        // sets the motor speed according to the scaled value 
  delay(125);                           // waits for the motor to gain speed 
  myservo2.write(mappedValue);
  delay(125);
}

Okay, I will try this code and yup that is exactly what's happening. I am powering the motors via a 14.8v 5A lipo pack.

I don't know what kind of speed controllers you are using but mine need to be armed before they will function properly. I say RTM and see if they require a specialized arming sequence. Sounds to me like the ESC is waiting for a certain user input.

Well, when I turn the potentiometer, the escs do arm. They arm at about the value ‘28’ from 0-180. I am able to do this fine with one speed controller and one motor but connecting any more motors seems to cause this issue.

Is it possible that your battery is not capable of the current required to run more than one motor(maybe ESC is sensing low voltage)? Are you monitoring voltage on the battery side? Is there maybe some kind of noise that is causing the arduino/ESC to reset? You said your self that you are arming them manually with the pot.

Why not arm them in setup so that if the ESCs reset, it arms them for you? That way your pot position won’t be incorrect for arming if/when it resets. If they hiccup and continue going then you know what is wrong.

Perhaps your power wiring isn't up to the task - you need thick wires for ESCs. The LiPo sounds plenty powerful enough. Expect 10+ A pulses - if the wiring loses more than a volt or so at those current levels the ESCs may cut out. High current connectors are a requirement.

Does a single ESC/motor actually work under a proper load BTW? If so that would verify the wiring/connectors/battery are OK.

Yes, everything is fine with one ESC and one motor, so it's gonna have to be the capability of the batteries that is the issue isn't it?

Would it help if I gave links to the motors and ESCs that I'm using?