Arduino多任务

时间:2018-10-24 17:24:40

标签: arduino

我试图查看是否可以将当前由两个单独的Arduino Unos处理的任务组合到一个Uno中。向我展示了Arduino(Link)上有关“多任务”的Adafruit指南,并认为我会尝试一下。

我觉得我确实缺少一些明显的东西,但是我的代码无法正常工作...

我正在控制一系列螺线管。他们需要根据各自的时间来采取行动。我创建了一个螺线管类,该类负责告诉螺线管何时启动,然后监视何时将其关闭。对于我一生,我看不到我的错误在哪里,但我永远都无法满足自己的最终条件。

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();
}

我只是在安装程序中运行所有内容,所以我可以看到一些运行情况。

您能帮我解决我更明显的愚蠢错误吗?

2 个答案:

答案 0 :(得分:0)

整个setup()空隙的执行速度可能会超过70ms,这意味着在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()
}

其余的对我来说很好;如果仍然有问题,请尝试使用显式变量声明而不是构造函数来解决问题。

答案 1 :(得分:0)

结果是我通过设置previousMillis = currentMillis来擦除自己的计数器。一旦我杀死了它,它就开始起作用。从那以后,我添加了其他功能,例如延迟启动,但这基本上是相同的代码。

tr///

}