1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- #include <Servo.h>
-
- #define PIN_POT A0
- #define PIN_BTN 2
- #define PIN_ESC 5
- #define MIN_SPEED 1000
- #define MAX_SPEED 1500
- #define DELAY 10
-
- Servo esc;
-
- void setup() {
- Serial.begin(115200);
-
- pinMode(PIN_BTN, INPUT_PULLUP);
- pinMode(PIN_POT, INPUT);
-
- esc.attach(PIN_ESC);
- esc.writeMicroseconds(MIN_SPEED);
- }
-
- float speedFunc(float t) {
- return 0.15 + 0.000000005 * t * t * t;
- }
-
- void loop() {
- int pressed = 0;
- int t = 0;
- int reachedMax = 0;
- while (digitalRead(PIN_BTN) == LOW) {
- pressed = 1;
- int pot = analogRead(PIN_POT);
- int speed = map(pot, 0, 1023, 0, MAX_SPEED - MIN_SPEED);
-
- int actualSpeed;
- if (!reachedMax) {
- float multiplier = min(speedFunc(t), 1);
- if (multiplier > 1) {
- multiplier = 1;
- reachedMax = 1;
- }
- actualSpeed = speed * multiplier + MIN_SPEED;
- } else {
- actualSpeed = speed + MIN_SPEED;
- }
-
- Serial.println(actualSpeed);
- esc.writeMicroseconds(actualSpeed);
-
- delay(DELAY);
- t += DELAY;
- }
-
- if (pressed)
- esc.writeMicroseconds(0);
- }
|