Controlling the speed of brushless DC motor WITHOUT potentiometer

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?

You're already doing that with your delay(2000), wait 2 seconds before starting the motor ramping up, and the delay(5000), wait five seconds before starting the ramp up again. To stay at speed just put another delay of however long you want after the first for loop when the motor is up to speed.

If you mean you want all that to happen just once then move all the code out of loop() into setup().

Steve

Thanks for the help! That worked :slight_smile:

Who can post edited code for brushless without potentiometer?thanks

mohsenhrt:
Who can post edited code for brushless without potentiometer?thanks

I suggest that you start your own topic explaining exactly what you want to do