I am a beginner on programming and I am working on a project in which I have to control a brushless DC motor. I have already bought the brushless motor, the ESC and the LiPo battery.
I have connected the circuit with arduino and run the motor with the following code:
#include <Wire.h>
#include <Servo.h>
Servo esc_signal;
int velocity = 0;
void setup() {
esc_signal.attach(9);
esc_signal.write(velocity);
delay(2000);
}
void loop()
{
for (velocity = 0; velocity <= 10; velocity += 1)
{
esc_signal.write(velocity);
delay(100);
}
for (velocity = 10; velocity > 0; velocity -= 1)
{
esc_signal.write(velocity);
delay(100);
}
esc_signal.write(0);
delay(5000);
}
}
As it is obvious the motor starts spinning from 0 - 10 and then comes back down to 0.
My problem is that I want to control the speed of the motor. For example I want to run from 0 - 10 and then stay for a while at that speed and after get down to 0 again. How can I do this? Most of the projects are using potentiometers but I want to control the speed through the code.
Also, I want the motor to start spinning after some time. Is it possible to turn off the Arduino or set the speed of motor to be kept at 0 after some time?