本文介绍了Arduino的多任务处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,那下面code未多任务。
我如何才能实现呢?
我的code工作的时刻如下:
我开始我的Andr​​oid应用程序,有我确认USB请求。
开始按钮的preSS后,我的应用程序发送一个字节数组到我的Arduino板。
问题是,stepper2(ingredient1value);只能启动时stepper1 ......结束了。

我知道Arduino是不是多线程的权利plattform,但我看到了一些解决方案,但我不能将它们融入到我的code。

感谢您帮助我!

 的#include< Max3421e.h>
#包括LT&;&Usb.h GT;
#包括LT&;&AndroidAccessory.h GT;#定义VALUE_OFF为0x0
#定义VALUE_ON为0x1
#定义COMMAND_LED 0X2
#定义TARGET_PIN_12 0×12INT stepperPin1 = 9;
INT stepperPin2 = 10;
INT stepperPin3 = 11;
INT stepperPin4 = 12;
INT stepperPin5 = 13;//改变这步数上的马达
的#define STEPS 48AndroidAccessory ACC(制造商,型号,说明,1.0,URI,串行);
字节ingredient1value,ingredient2value,ingredient3value,ingredient4value,ingredient5value;
字节RCVMSG [8];无效设置(){
  Serial.begin(115200);
 pinMode(stepperPin1,OUTPUT);
 pinMode(stepperPin2,OUTPUT);
 pinMode(stepperPin3,OUTPUT);
 pinMode(stepperPin4,OUTPUT);
 pinMode(stepperPin5,OUTPUT);
  acc.powerOn();
}
无效stepper1(INT turns1){
 的for(int i = 0; I< turns1 *步骤;我++){
   digitalWrite(stepperPin1,HIGH);
   delayMicroseconds(800);
   digitalWrite(stepperPin1,LOW);
   delayMicroseconds(800);
  }
 }无效stepper2(INT turns2){
 的for(int i = 0; I< turns2 *步骤;我++){
   digitalWrite(stepperPin2,HIGH);
   delayMicroseconds(800);
   digitalWrite(stepperPin2,LOW);
   delayMicroseconds(800);
  }
 }无效stepper3(INT turns3){
 的for(int i = 0; I< turns3 *步骤;我++){
   digitalWrite(stepperPin3,HIGH);
   delayMicroseconds(800);
   digitalWrite(stepperPin3,LOW);
   delayMicroseconds(800);
  }
 }
无效stepper4(INT turns4){
 的for(int i = 0; I< turns4 *步骤;我++){
   digitalWrite(stepperPin4,HIGH);
   delayMicroseconds(800);
   digitalWrite(stepperPin4,LOW);
   delayMicroseconds(800);
  }
 }
无效stepper5(INT turns5){
 的for(int i = 0; I< turns5;我++){
   digitalWrite(stepperPin5,HIGH);
   延迟(1000);
   digitalWrite(stepperPin5,LOW);
   延迟(1000);
  }
 }空隙环(){
  延迟(50);
  如果(acc.isConnected()){
    acc.read(RCVMSG,sizeof的(RCVMSG),1);    如果(RCVMSG [0] == COMMAND_LED&放大器;&放大器; RCVMSG [1] == TARGET_PIN_12){
      字节值= RCVMSG [2];      如果(价值== VALUE_ON){
        ingredient1value = RCVMSG [3];
        ingredient2value = RCVMSG [4];
        ingredient3value = RCVMSG [5];
        ingredient4value = RCVMSG [6];
        ingredient5value = RCVMSG [7];         stepper1(ingredient1value);
         stepper2(ingredient2value);
         stepper3(ingredient3value);
         stepper4(ingredient4value);
         stepper5(5);      }
    }
  }
}


解决方案

一个办法是,以避免延迟()(或 delayMicroseconds() 在code)和替换code图案,这些code通道,它不中断主循环,让Arduino的在特定频率或时间执行命令。

下面是一个例子,你如何执行功能每秒,而无需使用延迟()(并没有阻止其他命令,它应该具有更高的频率执行):

// 1秒。频率
无符号长间隔= 1000; //我们需要等待的时间
无符号长previousMillis = 0; //米利斯()返回一个unsigned long。// 3秒。频率
无符号长区间1 = 3000; //我们需要等待的时间
无符号长previousMillis1 = 0; //米利斯()返回一个unsigned long。无效设置(){
   // ...
}空隙环(){ 如果((无符号长)(米利斯() - previousMillis)GT =间隔){
    previousMillis =米利斯();
    // 每一秒
    // ...
 } 如果((无符号长)(米利斯() - previousMillis1)GT =区间1){
    previousMillis1 =米利斯();
    //每三个第二
    // ...
 } //其他CMD的...
}

编辑:

这方法不整合一个真正的多任务,但为您提供了一个伪并行在项目整合的可能性。

我添加第二个条件(每执行第三秒)体现的原则。

I got the problem, that the following code ain't multitasked.How can I realize that?My code works at the moment as follow:I start my Android app, there I confirm the USB request.After the press of the "start button", my app sends a byte array to my arduino board.The problem is that "stepper2(ingredient1value);" can only start when "stepper1..." finished.

I know that arduino ain't the right plattform for multithreading, but I saw some solutions, but I can't integrated them into my code.

Thanks for helping me!

#include <Max3421e.h>
#include <Usb.h>
#include <AndroidAccessory.h>



#define VALUE_OFF 0x0
#define VALUE_ON 0x1
#define COMMAND_LED 0x2
#define TARGET_PIN_12 0x12

int stepperPin1 = 9;
int stepperPin2 = 10;
int stepperPin3 = 11;
int stepperPin4 = 12;
int stepperPin5 = 13;



//change this to the number of steps on your motor
#define STEPS 48

AndroidAccessory acc("Manufacturer", "Model", "Description", "1.0", "URI","Serial");
byte ingredient1value, ingredient2value, ingredient3value, ingredient4value, ingredient5value;
byte rcvmsg[8];



void setup() {
  Serial.begin(115200);


 pinMode(stepperPin1, OUTPUT);
 pinMode(stepperPin2, OUTPUT);
 pinMode(stepperPin3, OUTPUT);
 pinMode(stepperPin4, OUTPUT);
 pinMode(stepperPin5, OUTPUT);
  acc.powerOn();
}


void stepper1(int turns1){
 for(int i=0;i<turns1*STEPS;i++){
   digitalWrite(stepperPin1, HIGH);
   delayMicroseconds(800);
   digitalWrite(stepperPin1, LOW);
   delayMicroseconds(800);
  }
 }



void stepper2(int turns2){
 for(int i=0;i<turns2*STEPS;i++){
   digitalWrite(stepperPin2, HIGH);
   delayMicroseconds(800);
   digitalWrite(stepperPin2, LOW);
   delayMicroseconds(800);
  }
 }



void stepper3(int turns3){
 for(int i=0;i<turns3*STEPS;i++){
   digitalWrite(stepperPin3, HIGH);
   delayMicroseconds(800);
   digitalWrite(stepperPin3, LOW);
   delayMicroseconds(800);
  }
 }




void stepper4(int turns4){
 for(int i=0;i<turns4*STEPS;i++){
   digitalWrite(stepperPin4, HIGH);
   delayMicroseconds(800);
   digitalWrite(stepperPin4, LOW);
   delayMicroseconds(800);
  }
 }




void stepper5(int turns5){
 for(int i=0;i<turns5;i++){
   digitalWrite(stepperPin5, HIGH);
   delay(1000);
   digitalWrite(stepperPin5, LOW);
   delay(1000);
  }
 }



void loop() {
  delay(50);
  if (acc.isConnected()) {
    acc.read(rcvmsg, sizeof(rcvmsg), 1);

    if (rcvmsg[0] == COMMAND_LED && rcvmsg[1] == TARGET_PIN_12) {
      byte value = rcvmsg[2];

      if (value == VALUE_ON){
        ingredient1value=rcvmsg[3] ;
        ingredient2value=rcvmsg[4] ;
        ingredient3value=rcvmsg[5] ;
        ingredient4value=rcvmsg[6] ;
        ingredient5value=rcvmsg[7] ;

         stepper1(ingredient1value);
         stepper2(ingredient2value);
         stepper3(ingredient3value);
         stepper4(ingredient4value);
         stepper5(5);

      }
    }
  }


}
解决方案

One approach could be to avoid delay() (or delayMicroseconds()) in your code and replace these code passages with code patterns, which does not interrupt the main loop and allow the Arduino to execute commands in specific frequencies or times.

Here is an example how you can execute a function every second without using delay() (and without block other commands, which should be executed with a higher frequency):

// 1 sec. frequency
unsigned long interval=1000;    // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.

// 3 sec. frequency
unsigned long interval1=3000;    // the time we need to wait
unsigned long previousMillis1=0; // millis() returns an unsigned long.

void setup() {
   //...
}

void loop() {

 if ((unsigned long)(millis() - previousMillis) >= interval) {
    previousMillis = millis();
    // every second
    // ...
 }

 if ((unsigned long)(millis() - previousMillis1) >= interval1) {
    previousMillis1 = millis();
    // every third second
    // ...
 }

 // other CMD's...
}

EDIT:

This approach does not integrate a real multi-task, but gives you the possibility to integrate a pseudo-parallelism in your project.

I added a second condition (execution every third second)to exemplify the principle.

这篇关于Arduino的多任务处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 02:32