【问题标题】:Arduino Multi-taskingArduino 多任务处理
【发布时间】:2018-10-24 17:24:40
【问题描述】:

我正在尝试查看是否可以将当前由两个单独的 Arduino Unos 处理的任务合并到一个 Uno 上。我在 Arduino (Link)) 上看到了 Adafruit 的“多任务处理”指南,我想我会试一试。

我觉得我遗漏了一些非常明显的东西,但我的代码无法正常工作......

我正在控制一系列螺线管。他们需要根据个人的时间安排每项行动。我创建了一个 Solenoid 类来处理告诉螺线管何时启动,然后监视何时关闭它。在我的一生中,我看不到我的错误在哪里,但我永远不会满足我的最终条件。

class Solenoid {
  //Class Variables
  int solenoidPin;
  long chargeTime;
  long ventTime;
  bool started = false;
  unsigned long startMillis;
  unsigned long endMillis;
  unsigned long previousMillis;
  int solenoidState;

  //Constructor
public:
  Solenoid(int pin, long charge, long vent) {
    solenoidPin = pin;
    pinMode(solenoidPin, OUTPUT);
    chargeTime = charge;
    ventTime = vent;
    solenoidState = LOW;
    previousMillis = 0;
}
  void Start() {
    Serial.println("Start called");
    started = true;
  }

  void Update() {
    //Check to see if it is time to change the state of the solenoid
    unsigned long currentMillis = millis();
    Serial.println("Update");
    if((started == true) && (currentMillis-previousMillis <= chargeTime)) {
      //Run
      Serial.print("Run: Charge Time=");
      Serial.print(chargeTime);
      Serial.print("  current-previous=");
      Serial.println(currentMillis-previousMillis);
      previousMillis = currentMillis;
    } else if ((started == true) && (currentMillis-previousMillis >= chargeTime)){
      //Stop
      Serial.println("Stop");
    }
  }
};

//Setup the solenoids
Solenoid solenoid1(13, 70, 70);

void setup() {
  Serial.begin(115200);
  Serial.println("Ready");

    solenoid1.Start();
    solenoid1.Update();
    solenoid1.Update();
    solenoid1.Update();
    solenoid1.Update();
    solenoid1.Update();
}

我只是在设置中运行所有东西,所以我可以看到一些运行。

你能帮我解决我更清楚的愚蠢错误吗?

【问题讨论】:

    标签: arduino


    【解决方案1】:

    有可能整个 setup() void 的执行速度超过 70 毫秒,这意味着在 5 次对螺线管 1.Update() 的调用完成之前,您永远无法到达终点位置。

    试试这个:

    class Solenoid {
      //Class Variables
      int solenoidPin;
      long chargeTime;
      long ventTime;
      bool started = false;
      unsigned long startMillis;
      unsigned long endMillis;
      unsigned long previousMillis;
      int solenoidState;
    
      //Constructor
    public:
      Solenoid(int pin, long charge, long vent) {
        solenoidPin = pin;
        pinMode(solenoidPin, OUTPUT);
        chargeTime = charge;
        ventTime = vent;
        solenoidState = LOW;
        previousMillis = 0;
    }
      void Start() {
        Serial.println("Start called");
        started = true;
      }
    
      void Update() {
        //Check to see if it is time to change the state of the solenoid
        unsigned long currentMillis = millis();
        Serial.println("Update");
        if((started == true) && (currentMillis-previousMillis <= chargeTime)) {
          //Run
          Serial.print("Run: Charge Time=");
          Serial.print(chargeTime);
          Serial.print("  current-previous=");
          Serial.println(currentMillis-previousMillis);
          previousMillis = currentMillis;
        } else if ((started == true) && (currentMillis-previousMillis >= chargeTime)){
          //Stop
          Serial.println("Stop");
        }
      }
    };
    
    //Setup the solenoids
    Solenoid solenoid1(13, 70, 70);
    
    void setup() {
      Serial.begin(115200);
      Serial.println("Ready");
      solenoid1.Start();
    }
    
    void loop(){
      solenoid1.update()
    }
    

    我觉得其余的都不错;如果您仍然遇到问题,请尝试使用显式变量声明而不是构造函数来解决问题。

    【讨论】:

    • 谢谢,我也想知道。我最终在前面增加了一些延迟时间以进行补偿,但它仍然发生了。原来问题是我设置了previousMillis = currentMillis。我将发布我的工作(和简化)代码。
    【解决方案2】:

    原来我通过设置 previousMillis = currentMillis 来擦除我自己的计数器。一旦我杀了它,它就开始工作了。从那以后我添加了一些其他功能,比如延迟启动,但这主要是相同的代码。

    void Update() {
    //Check to see if it is time to change the state of the solenoid
    unsigned long currentMillis = millis();
    
     if((started == true) && (currentMillis - startMillis <= startDelay)) {
      //keep waiting
      Serial.println("Waiting");
     } else if((started == true) && (currentMillis - startMillis < (chargeTime + startDelay) )) {
      //Run
      Serial.print("Run ");
      Serial.println(currentMillis - startMillis);
      digitalWrite(solenoidPin, HIGH);
    } else if ((started == true) && (currentMillis - startMillis >= (chargeTime + startDelay) )){
      //Stop
      Serial.print("Stop ");
      Serial.println(currentMillis - startMillis);
      digitalWrite(solenoidPin, LOW);
      started = false;
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多