Hi I am doing a project and trying to figure out how i can run two motors in sync. The issue is they use an esc. I figured how to control 1 motor with 1 esc but if i try to replicate the code so that I can run multiple on different pins it brakes and neither work. I am using the easy way which is use it like a servo and control it that way
Heres the code:
#include <Servo.h>
Servo esc; //Making the variable
Servo esc2;
int ControllerPin = A7; //This is for a 5 button controller
int ControllerValue; //For Reading the button value 0-1023
void setup()
{
Serial.begin(9600);
}
void loop()
{
ControllerValue = analogRead(ControllerPin); //Reading the controller pin and putting that value in ControllerValue
Serial.print("Cont Val is: "); //For Monitoring
Serial.println(ControllerValue); // Outputs The number when button is pressed
if (ControllerValue <= 50) //Looking For leftbutton pressed. Only using it for control purposes
{
Serial.println("Left");
esc.attach(9, 100, 100); //Attach to this pin and have two intergers. It doesnt work otherwise.
esc.write(100); //Speed
}
else if (ControllerValue >= 700 && ControllerValue <= 800) //this is the 5th button again i use for control
{
Serial.println("Extra");
esc.attach(9);//Attaches Again
delay(1); //this helps it to be stable
esc.write(40);//This arms the esc after a little the esc disarms its self
}
else
{
Serial.println("No Key");
}
}
You attach the ESCs in the loop(). I usually see those being attached in the setup().
The code you posted only writes to 1 ESC.
Can we see the code you are using to try to work with 2 ESCs?
Side note, many ESCs require an arming sequence before they will supply power to the motor. Something like power up with the throttle at zero. Wait some time. Set the throttle at full and wait some time. Set the throttle back to zero.
Then the ESC will be armed.
Here is the code with two esc that ends up controlling 1
#include <Servo.h>
Servo esc; //Making the variable
Servo esc2;
int ControllerPin = A7; //This is for a 5 button controller
int ControllerValue; //For Reading the button value 0-1023
void setup()
{
Serial.begin(9600);
}
void loop()
{
ControllerValue = analogRead(ControllerPin); //Reading the controller pin and putting that value in ControllerValue
Serial.print("Cont Val is: "); //For Monitoring
Serial.println(ControllerValue); // Outputs The number when button is pressed
if (ControllerValue <= 50) //Looking For leftbutton pressed. Only using it for control purposes
{
Serial.println("Left");
esc.attach(9, 100, 100); //Attach to this pin and have two intergers. It doesnt work otherwise.
esc.write(100); //Speed
esc2.attach(10, 100, 100); //Attach to this pin and have two intergers. It doesnt work otherwise.
esc2.write(100); //Speed
}
else if (ControllerValue >= 700 && ControllerValue <= 800) //this is the 5th button again i use for control
{
Serial.println("Extra");
esc.attach(9);//Attaches Again
delay(1); //this helps it to be stable
esc.write(40);//This arms the esc after a little the esc disarms its self
esc2.attach(10);//Attaches Again
delay(1); //this helps it to be stable
esc2.write(40);//This arms the esc after a little the esc disarms its self
}
else
{
Serial.println("No Key");
}
}
Thank You For the help you helped me relize what was wrong you were write i should put the attach in the setup cause when i did then both motors worked fine! Ian
//Made By Ian Hickler
//I will update it on the forums when needed
//To control the esc you have to find the three wires that come off the esc(not the one that go to the motor! The ones that come out from where the positive and negative lines go in.)
//The white/yellow line is for data/pwm input connect that line to a pwm header(should look like a number with ~ that in the front) you then want to connect the ground (black wire) to a ground pin. Leave the red wire alone and do not bother to touch it as it can or will fry your arduino.
//For every esc attached you must use a pwm.
//This is for a button controller you will have to change the code as needed depending on what you are using.
#include <Servo.h> //include this libary
Servo esc; //Making the variable for the first esc
Servo esc2; //making the variable for the second esc
//this following two ints ARE NOT NEEDED UNLESS YOU ARE USING A 5 BUTTON CONTROLLER WITH ANALOG INPUT
int ControllerPin = A7; //This is for a 5 button controller
int ControllerValue; //For Reading the button value 0-1023
void setup()
{
Serial.begin(9600);
esc.attach(9); //This shows the variable what pin to connect to for the first esc. Repeat the procces for each esc used. Dont forgot to add Servo theNameOfYourVariable; so that every esc has its own variable
esc2.attach(10);
}
void loop()
{
ControllerValue = analogRead(ControllerPin); //Reading the controller pin and putting that value in ControllerValue
Serial.print("Cont Val is: "); //For Monitoring
Serial.println(ControllerValue); // Outputs The number when button is pressed
if (ControllerValue <= 50) //Looking For leftbutton pressed. Only using it for control purposes
{
Serial.println("Left");
esc.write(100); //Telling the esc to run the motor at 100 Speed 85-180 will work any less than 85 will not arm the motor and not turn. Anything above 180 wont affect speed already hit max speed at 180
esc2.write(100);
}
else if (ControllerValue >= 700 && ControllerValue <= 800) //this is the 5th button again i use for control
{
Serial.println("Extra");
esc.write(40);//This arms the esc but will not turn motor on
esc2.write(40);
}
else
{
Serial.println("No Key");
}
}