Таймер для мотопланера

Тема в разделе "Моторы, сервоприводы, робототехника", создана пользователем Anton30, 21 ноя 2018.

  1. Anton30

    Anton30 Нуб

    Здравствуйте! Недавно нашёл на просторах интернета про таймер для кордовой модели, собрал его, но он оказался не ах-ти, решил модернизировать, сделав управление временем с помощи потенциометра и страт стоп с кнопки. После пары попыток понял что я тупой в написании скеча. Найдя это форум решил обратится за помощью. Заранее спасибо!

    #include <Servo.h>
    // create servo object to control a servo
    Servo esc;

    int maxThrottle = 120;

    int curThrottle = 0;

    // will store last time
    unsigned long previousMillis = 0;

    unsigned long currentInterval = 0;

    // time to prepare (milliseconds)
    const long prepareTime = 30000;
    // time to fly (milliseconds)
    const long flyingTime = 90000;

    /*
    Current state
    0 - prepare to fly
    1 - fly
    2 - landing
    */
    int currentState = 0;

    void setup() {
    // attaches the servo on pin 9 to the servo object
    esc.attach(9, 1000, 2000);
    esc.write(curThrottle);
    currentInterval = prepareTime;
    }

    void loop() {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= currentInterval) {
    if (currentState == 0) {
    // we are ready to fly, throttle max!
    for (curThrottle = 0; curThrottle <= maxThrottle; curThrottle += 1) {
    esc.write(curThrottle);
    delay(20); // Throlle up delay
    }
    if (curThrottle < maxThrottle) {
    curThrottle = maxThrottle;
    }
    esc.write(curThrottle);
    currentInterval = flyingTime;
    previousMillis = millis();
    currentState = 1;
    } else if (currentState == 1) {
    // flying time is gone, time to landing
    for (curThrottle = maxThrottle; curThrottle > 0; curThrottle -= 1) {
    esc.write(curThrottle);
    delay(100);// Throlle down delay
    }
    if (curThrottle < 0) {
    curThrottle = 0;
    }
    esc.write(curThrottle);
    currentState = 2;
    } else {
    // done
    }
    }
    }
     
  2. Airbus

    Airbus Радиохулиган Модератор

    [​IMG]
     
    Daniil и Anton30 нравится это.
  3. Anton30

    Anton30 Нуб

    Код (C++):
    #include <Servo.h>
    // create servo object to control a servo
    Servo esc;

    int maxThrottle = 120;

    int curThrottle = 0;

    // will store last time
    unsigned long previousMillis = 0;

    unsigned long currentInterval = 0;

    // time to prepare (milliseconds)
    const long prepareTime = 30000;
    // time to fly (milliseconds)
    const long flyingTime = 90000;

    /*
        Current state
        0 - prepare to fly
        1 - fly
        2 - landing
    */

    int currentState = 0;

    void setup() {
        // attaches the servo on pin 9 to the servo object
        esc.attach(9, 1000, 2000);
        esc.write(curThrottle);
        currentInterval = prepareTime;
    }

    void loop() {
        unsigned long currentMillis = millis();
        if (currentMillis - previousMillis >= currentInterval) {
            if (currentState == 0) {
                // we are ready to fly, throttle max!
                for (curThrottle = 0; curThrottle <= maxThrottle; curThrottle += 1) {
                    esc.write(curThrottle);
                    delay(20); // Throlle up delay
                }
                if (curThrottle < maxThrottle) {
                    curThrottle = maxThrottle;
                }
                esc.write(curThrottle);
                currentInterval = flyingTime;
                previousMillis = millis();
                currentState = 1;
            } else if (currentState == 1) {
                // flying time is gone, time to landing
                for (curThrottle = maxThrottle; curThrottle > 0; curThrottle -= 1) {
                    esc.write(curThrottle);
                    delay(100);// Throlle down delay
                }
                if (curThrottle < 0) {
                    curThrottle = 0;
                }
                esc.write(curThrottle);
                currentState = 2;
            } else {
                // done
            }
        }
    }
     
  4. Daniil

    Daniil Гуру

    И в чем проблема?
     
  5. DetSimen

    DetSimen Guest

    Хорошо, принято к сведению. Как появятся вопросы, абращайся, чо.
     
    Anton30 нравится это.
  6. Anton30

    Anton30 Нуб

    Дописать скеч, включив потенциометр, и кнопку.
     
  7. Daniil

    Daniil Гуру

    На амперка вики есть статьи как работать с кнопкой и потенциометром
     
  8. Airbus

    Airbus Радиохулиган Модератор

    А они там зачем?В коде все делается само собой.Плавно добавляется газ потом держит режим потом убирает газья и садиться.
     
  9. Anton30

    Anton30 Нуб

    Потенциометром изменять время полёта, а кнопкой запустить, или остановить и сбросить время.
     
  10. Anton30

    Anton30 Нуб

    Я тупой в этом деле.
     
  11. Daniil

    Daniil Гуру

    это в "закажу проект"