SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By frostfrostson
#198219
Hi Everybody,
I'm trying to control a brushless ESC using arduino mega but i can't findout how to set starting signal.
I'am using Racerstar RS30A Lite 30A Blheli_S BB1 2-4S Brushless ESC.
This is my code but i have no clue what's wrong:
Code: Select all
#include <Servo.h>

// Servo objekt s regulátorem
Servo esc;
// Regulátor najdeme na pinu D9
int esc_pin = 9;
// Minimální hodnota pulzu
int arm = 1000;
// arm + steady = od jaké hodnoty začíná reagovat motor
int steady = 250;

void setup() {
  // Připojíme Servo objekt na pin D9
  esc.attach(esc_pin);
  // Nastavíme pulz 1000 microsekund na výstupu
  esc.writeMicroseconds(arm);
  // a počkáme 1 sekundu
  delay(1000);
  // Regulátor by měl rozpoznat náš 1 ms pulz a tak začneme pomalu roztáčet motor
  // Toto nesmíme udělat moc rychle, jinak dojde k chybovému stavu jako na videu níže
  throttle_up();
}

void loop() {
int i;
// Postupně budeme do regulátoru posílat pulzy o šířce 1250 mikrosekund až 1350 mikrosekund
for(i = 0; i < 20; i++) {
  esc.writeMicroseconds(arm + steady + 5 * i);
  delay(500);
}
delay(2000);
// a zase motor zpomalíme
for(i = 19; i >= 0; i--) {
  esc.writeMicroseconds(arm + steady + 5 * i);
  delay(500);
}
delay(2000);
}

// funkce pro postupné nastavení šířky pulzu na 1250 mikrosekund
void throttle_up() {
  for(int i = 0; i <= steady; i++) {
esc.writeMicroseconds(arm + i);
delay(20);
  }
} 
Probably i miss there a maximum value of signal (in normall startup proces there is needed minimum and maximum value, i have only minimum value it is what i found on internet) at the start but i don't know how to do it.
By Valen
#198246
I'm sorry. Those comment are useless for me. I've put them through Google Translate and got the following.

First, what is the problem? Does it not work? Then, how not?

If you can provide a link to documentation on the ESC library then I'll have a look at the code.
Code: Select all
#include <Servo.h>

// Servo object with controller
Servo esc;
// We find the controller at D9
int esc_pin = 9;
// Minimum pulse value
int arm = 1000;
// arm + steady = from what value the engine starts to respond
int steady = 250;

void setup () {
  // Connect the Servo object to pin D9
  esc.attach (esc_pin);
  // Set pulse 1000 microseconds at output
  esc.writeMicroseconds (arm);
  // and wait for 1 second
  delay (1000);
  // The controller should recognize our 1 ms pulse and so we start to slowly rotate the engine
  // We do not have to do this too fast, otherwise the error status will be as in the video below
  throttle_up ();
}

void loop () {
int i;
// Gradually we will send pulses with a width of 1250 microseconds to 1350 microseconds to the controller
for (i = 0; i <20; i ++) {
  esc.writeMicroseconds (arm + steady + 5 * i);
  delay (500);
}
delay (2000);
// and slow down the engine again
for (i = 19; i> = 0; i--) {
  esc.writeMicroseconds (arm + steady + 5 * i);
  delay (500);
}
delay (2000);
}

// function for stepwise adjustment of pulse width to 1250 microseconds
void throttle_up () {
  for (int i = 0; i <= steady; i ++) {
esc.writeMicroseconds (arm + i);
delay (20);
  }
}