Arduino:中断昂贵的功能并恢复另一个



作为一个第一个项目,我打算制作具有不同灯模式的Teensyduino环境光,在大开关语句中检查它们 - 现在我想通过按下一个模式到另一种模式到另一种模式按钮。

谷歌搜索使我使用中断,但是有一个点不清楚 - 如果我在昂贵的功能中按下按钮,这需要很长时间,并且使用了许多变量,如果我从中断,剩余的状态保留在RAM中,如果我确实切换了太多次或已清除,则会导致堆叠。

在这里一些代码:

const int speed = 30 //milliseconds
const int modes = 11; //maximum number of modes
const int red   = 15;
const int green = 14;
const int blue  = 12;
volatile int mode  = 0;
void setup() {
    pinMode(red   , OUTPUT);
    pinMode(green , OUTPUT);
    pinMode(blue  , OUTPUT);
    randomSeed(analogRead(0));
    Serial.begin(9600);
    attachInterrupt(0,incMode,CHANGE); // 0 -> digital pin 2
}
void loop()  {
    switch(mode){
        case 0:{
            Serial.println("powerdown");
            setAll(0);
            delay(1000);
            break;
        }
        \...
        case modes:{
            \ expensive long function
        }
    }
}
void blinkAll(int times){
    for(int i=1;i <= times;i++){
        setAll(255);
        delay(speed*17);
        setAll(0);
        delay(speed*17);
    }
}
void setAll(int bright){
        analogWrite(red   , bright);
        analogWrite(green , bright);
        analogWrite(blue  , bright);
}
void incMode(){
    delay(speed);
    blinkAll(2); //to indicate mode has changed
    mode = (mode+1) % (modes+1); //switch starts with 0 so use "% modes+1"!
    Serial.println("mode increased");
    //--> loop();
    //--> would resume the main loop but lead to a stackoverflow i presume
}

我将如何脱离运行功能而不会延迟和堆叠污染。我知道我可以设置模式并等到功能结束,但是如果我有一个模式需要几分钟才能结束,我希望能够立即切换。

ps。:尽管我正在使用Teensyduino,但我将使用Arduino标签,并且由于我不知道Arduinio使用哪种语言使用标签C/C 。如果不合适,请更改此。

,如果您多次从中断处理程序中重新进入主管,则最终会溢出堆栈。此外,由于就硬件而言,您仍然处于中断处理程序中,因此您将拥有各种各样的怪异 - 尤其是,当您已经在中断时,中断会被阻止,这意味着delay()不会工作和millis()不会计算,除非您想出某种手动重新启用中断的方式,否则其他各种事情也会被打破。

解决此问题的一种更好的方法是使您的"昂贵的长功能"成为由廉价,短函数驱动的状态机器,被称为经常被称为。然后,您的中断处理程序可以简单地设置进入此功能进入此功能的标志,此时,当前模式(即当前状态机)已更改。

这种方法还可以更轻松地定义新的照明模式。例如,您可以定义这样的内容:

struct phase {
  unsigned char r, g, b, delay;
};
unsigned long t_nextPhase;
volatile struct phase *forceMode = NULL;
struct phase *mode = blinkAll;
int nextPhase = 0;
struct phase blinkAll[] = {
  { 255, 255, 255, 17 },
  { 0, 0, 0, 17 },
  { 0, 0, 0, 255 } // loop sentinel
};
void lighting_kernel() {
    noInterrupts(); // ensure we don't race with interrupts
    if (forceMode) {
        mode = forceMode;
        forceMode = NULL;
        t_nextPhase = millis();
        nextPhase = 0;
    }
    interrupts();
    if (t_nextPhase > millis()) {
        return;
    }
    struct phase *cur_phase;
    do {
        cur_phase = mode[nextPhase++];
        if (cur_phase->delay == 255) {
            nextPhase = 0;
        }
    } while (cur_phase->delay == 255);
    analogWrite(red   , cur_phase->r);
    analogWrite(green , cur_phase->g);
    analogWrite(blue  , cur_phase->b);
    t_nextPhase = millis() + cur_phase->delay;    
}

现在要定义一种新的照明模式,您只需要一系列新的颜色和时间,而不是编写新代码。添加诸如彩色坡道和其他此类效果之类的东西是读者的练习。

最新更新