Arduino 多任务处理 [英] Arduino Multitasking

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

问题描述

我遇到了问题,以下代码不是多任务处理的.我怎么能意识到这一点?我的代码目前工作如下:我启动了我的 Android 应用程序,在那里我确认了 USB 请求.按下开始按钮"后,我的应用程序向我的 arduino 板发送一个字节数组.问题是stepper2(ingredient1value);"只能在stepper1..."完成后开始.

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.

我知道 arduino 不是适合多线程的平台,但我看到了一些解决方案,但我无法将它们集成到我的代码中.

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.

谢谢你帮助我!

#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);

      }
    }
  }


}

推荐答案

一种方法可能是在您的代码中避免 delay()(或 delayMicroseconds())和用代码模式替换这些代码段落,这不会中断主循环并允许 Arduino 以特定频率或时间执行命令.

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.

这里有一个例子,你可以在不使用 delay() 的情况下每秒执行一个函数(并且不阻塞其他命令,这些命令应该以更高的频率执行):

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...
}

这种方法并没有集成真正的多任务,但可以让您在项目中集成伪并行.

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 多任务处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆