我怎样才能有一个执行两个任务的条件语句?第一个然后另一个循环 [英] How can I have a conditional statement that performs two tasks? first one and then the other in a loop

查看:21
本文介绍了我怎样才能有一个执行两个任务的条件语句?第一个然后另一个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图在 arduino 中编写一个程序,该程序执行 2 分钟的任务,然后执行 5 分钟的另一个任务.循环中的第一个,然后另一个,直到满足不同的条件.

So I am trying to code a program in arduino that does a task for 2 minutes and then another task for 5 mins. first one and then the other in a loop until a different condition is met.

我一直在尝试用 if 语句和 while 循环来做到这一点,但我在时间部分迷失了我认为

Ive been trying to do this with if statements and while loops but im getting lost in the time part i think

//Pneumatic feed system

    double minute = 1000*60;
    double vibTime = 2 * minute;   //2 mins
    //double vibTime = 5000;
    double execTime = millis();
    double waitTime = 5 * minute; //time waits between vibrating 
    //double waitTime = 10000;

void DoPneuFeed() {
    //Serial.print("Delta:");
    //Serial.println(millis()-execTime);

    if(Temp_Data[T_9] < 500)
    {

          if(execTime + vibTime < millis())
          {
              //turn on vibration for 2 mins
              execTime = millis();
              Serial.println("VIBRATING");
          }

          if(execTime + waitTime < millis())
          {
                //turn off vibration for 5 mins
                execTime = millis();
                Serial.println("WAITING");
          }

}

    if(Temp_Data[T_9] > 500)
    {
          relayOff(3);
          relayOff(7);

    }
}

void PneuFeed()
{
  if(execTime + vibTime > millis())
  {
    //air pressure open
    relayOn(3);
    //vibrator on
    relayOn(7); 
  }
  else if(execTime + waitTime > millis())
  {
    relayOff(3);
    relayOff(7);
  }
}

我想开启振动模式 2 分钟,然后关闭 5 分钟.只要 Temp_Data(T_9) <500. 如果大于 500 则应保持关闭.

I want to turn on the vibration mode for 2 mins and then turn it off for 5 mins. as long as the Temp_Data(T_9) < 500. if it is greater than 500 it should stay off.

推荐答案

没有深入了解你的代码的细节(因为我无法构建它来测试.)我只有一个快速建议:

Without going into the specifics of your code (since I cannot build it to test.) I only have one quick suggestion:

不要使用一系列 if-then-else(或其变体),而是考虑使用状态机.实现通常包括在 while(expression){...} 循环内使用 switch().以下是一个非常简单的示例,说明您可能如何执行此构造中所需的步骤:
(注意,这是用于说明的 C 和伪代码的混合.它接近可编译,但包含一些未定义的项目.)

Instead of using a series of if-then-else (or variations of it) consider using a state machine. Implementations often include use of a switch() inside a while(expression){...} loop. The following is a very simple example of how you might be able to do the steps you need in this construct:
(Note, this is a mix of C and pseudo code for illustration. It is close to compilable , but contains a few undefined items.)

typedef enum {
    START,      
    SET_VIB,
    CHECK_TEMP,
    GET_TIME,
    CLEANUP,
    SLEEP,
    MAX_STATE
}STATE;

enum {
    IN_WORK,
    FAILED,
    SUCCESS
};

enum {// durations in minutes
    MIN_2,
    MIN_5,
    MIN_10,
    MAX_DUR// change/add durations as profile needs
};
const int dur[MAX_DUR]  = {120, 300, 600};

int RunProcess(STATE state);

int main(void)
{
    STATE state = START;

    RunProcess(state);

    return 0;
}

int RunProcess(STATE state)//Add arguments here for temperature, 
{                          //Sleep duration, Cycles, etc. so they are not hardcoded.
    int status;
    time_t elapsed = 0; //s
    BOOL running = TRUE;
    double temp, setpoint;
    int duration;
    time_t someMsValue;


    while(running)
    switch (state) {
        case START:
            //Power to oven
            //Power to vibe
            //initialize state and status variables
            duration = dur[MIN_2];
            state = SLEEP;
            status = IN_WORK;
            break;
        case SET_VIB:
            //test and control vibe
            if(duration == dur[MIN_2]) 
            {
                duration = dur[MIN_5];
                //& turn off vibe
            }
            else
            {
                duration = dur[MIN_2];
                //& turn on vibe
            }
            //init elapsed
            elapsed = 0;           
            status = IN_WORK;            
            state = CHECK_TEMP;
            break;
        case CHECK_TEMP:
            //read temperature
            if(temp < setpoint) 
            {
                state = SLEEP;
                status = IN_WORK;
            }
            else 
            {
                state = CLEANUP;
                status = SUCCESS;            
            }
            break;
        case GET_TIME:
            elapsed = getTime();
            if(elapsed > duration) state = SET_VIB;
            else state = SLEEP;
            status = IN_WORK;            
            break;
        case CLEANUP:
            //turn off heat
            //turn off vibe
            running = FALSE;
            break;
        case SLEEP:
            Sleep(someMsValue);
            state = GET_TIME;
            status = IN_WORK;
            break;
        default:
            // called with incorrect state value
            // error message and exit
            status = FAILED;
            state = CLEANUP;
            break;
    }
    return status;
}

改进此插图的建议:展开此代码以读取配置文件"文件.它可以包括诸如典型的过程参数值之类的东西,例如温度曲线、振动曲线、循环次数等.有了这些信息,这里用作说明的所有硬编码都可以替换为运行时可配置参数, 允许系统使用许多不同的配置文件无需每次都重新编译可执行文件.

Suggestion for improvement to this illustration: Expand this code to read in a "profile" file. It could include such things as parameter values typical to your process, such as temperature profiles, vibration profiles, number of cycles, etc. With such information, all of the hard-coding used here as illustration could be replaced with run-time configurable parameters, allowing the system to use many different profiles without having to recompile the executable each time.

另一个状态机示例.

这篇关于我怎样才能有一个执行两个任务的条件语句?第一个然后另一个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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